Cleanup whitebox server code

master
Bas Kloosterman 2 years ago
parent 43a47b41fd
commit 77e07832db
  1. 108
      whiteboxservice/srv.go

@ -57,7 +57,7 @@ func (srv *UIService) init() {
r.Use(srv.Authenticate) r.Use(srv.Authenticate)
r.GET("/", func(c *gin.Context) { r.GET("/", func(c *gin.Context) {
c.Redirect(301, "/ui") c.Redirect(http.StatusMovedPermanently, "/ui")
}) })
r.GET("/ui", srv.GetIndex) r.GET("/ui", srv.GetIndex)
r.GET("/ui/*page", srv.GetIndex) r.GET("/ui/*page", srv.GetIndex)
@ -66,8 +66,6 @@ func (srv *UIService) init() {
r.GET("/api/connections/:connID/:serviceID", srv.GetSubscriptions) r.GET("/api/connections/:connID/:serviceID", srv.GetSubscriptions)
r.GET("/api/connections/:connID/:serviceID/:patientID", srv.GetPatient) r.GET("/api/connections/:connID/:serviceID/:patientID", srv.GetPatient)
r.GET("/api/registrations", srv.GetRegistrations) r.GET("/api/registrations", srv.GetRegistrations)
// r.GET("/api/systems/:sysid/patients", srv.GetPatients)
// r.GET("/api/systems/:sysid/patients/:patid", srv.GetPatient)
srv.inited = true srv.inited = true
} }
@ -79,14 +77,14 @@ func (srv *UIService) GetConnection(c *gin.Context) {
connID := c.Param("connID") connID := c.Param("connID")
connection := &sharedmodel.Connection{} connection := &sharedmodel.Connection{}
srv.data.Where("id = ?", connID).Find(&connection) srv.data.Where("id = ?", connID).Find(&connection)
c.JSON(200, connection) c.JSON(http.StatusOK, connection)
} }
func (srv *UIService) GetSubscriptions(c *gin.Context) { func (srv *UIService) GetSubscriptions(c *gin.Context) {
connID := c.Param("connID") connID := c.Param("connID")
serviceID := c.Param("serviceID") serviceID := c.Param("serviceID")
serviceConfig := &sharedmodel.ServiceConfig{} serviceConfig := &sharedmodel.ServiceConfig{}
srv.data.Preload("Service").Preload("Subscriptions").Where("connection_id = ? and id = ?", connID, serviceID).Find(&serviceConfig) srv.data.Preload("Service").Preload("Subscriptions").Where("connection_id = ? and id = ?", connID, serviceID).Find(&serviceConfig)
c.JSON(200, serviceConfig) c.JSON(http.StatusOK, serviceConfig)
} }
func (srv *UIService) GetPatient(c *gin.Context) { func (srv *UIService) GetPatient(c *gin.Context) {
@ -96,7 +94,7 @@ func (srv *UIService) GetPatient(c *gin.Context) {
patient := &sharedmodel.Subscription{} patient := &sharedmodel.Subscription{}
serviceConfig := &sharedmodel.ServiceConfig{} serviceConfig := &sharedmodel.ServiceConfig{}
if err := srv.data.Preload("FetchProtocol").Preload("FetchProtocol.AuthConfig").Preload("Service").Where("connection_id = ? and id = ?", connID, serviceID).Find(&serviceConfig).Error; err != nil { if err := srv.data.Preload("FetchProtocol").Preload("FetchProtocol.AuthConfig").Preload("Service").Where("connection_id = ? and id = ?", connID, serviceID).Find(&serviceConfig).Error; err != nil {
c.AbortWithError(500, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
srv.data.Where("service_config_id = ? and id = ?", serviceID, patientID).Find(&patient) srv.data.Where("service_config_id = ? and id = ?", serviceID, patientID).Find(&patient)
@ -106,16 +104,21 @@ func (srv *UIService) GetPatient(c *gin.Context) {
err := serviceConfig.FetchProtocol.UnmarshalConfig(&protoconfig) err := serviceConfig.FetchProtocol.UnmarshalConfig(&protoconfig)
log.Println(err, protoconfig) if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
err = patient.GetProtocolMeta(&protometa) err = patient.GetProtocolMeta(&protometa)
log.Println(err, protometa) if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
url := fmt.Sprintf("%v/%v/%v", protoconfig["url"], "patients", protometa["patientID"]) url := fmt.Sprintf("%v/%v/%v", protoconfig["url"], "patients", protometa["patientID"])
req, _ := http.NewRequest("GET", url, nil) req, _ := http.NewRequest("GET", url, nil)
// req.Header.Set("Authorization", serviceConfig.FetchProtocol.AuthConfig.Raw)
client := &http.Client{ client := &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
@ -129,7 +132,7 @@ func (srv *UIService) GetPatient(c *gin.Context) {
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
c.AbortWithError(500, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
@ -142,7 +145,7 @@ func (srv *UIService) GetPatient(c *gin.Context) {
sin, err := cmd.StdinPipe() sin, err := cmd.StdinPipe()
if err != nil { if err != nil {
log.Println("[ediviewer] Failed to open stdin pipe:", err) log.Println("[ediviewer] Failed to open stdin pipe:", err)
c.AbortWithError(500, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
defer sin.Close() defer sin.Close()
@ -150,7 +153,7 @@ func (srv *UIService) GetPatient(c *gin.Context) {
serr, err := cmd.StderrPipe() serr, err := cmd.StderrPipe()
if err != nil { if err != nil {
log.Println("[ediviewer] Failed to open stderr pipe:", err) log.Println("[ediviewer] Failed to open stderr pipe:", err)
c.AbortWithError(500, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
defer serr.Close() defer serr.Close()
@ -158,14 +161,14 @@ func (srv *UIService) GetPatient(c *gin.Context) {
sout, err := cmd.StdoutPipe() sout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
log.Println("[ediviewer] Failed to open stdout pipe:", err) log.Println("[ediviewer] Failed to open stdout pipe:", err)
c.AbortWithError(500, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
defer sout.Close() defer sout.Close()
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
log.Println("[ediviewer] Failed to start:", err) log.Println("[ediviewer] Failed to start:", err)
c.AbortWithError(500, err) c.AbortWithError(http.StatusInternalServerError, err)
return return
} }
@ -176,7 +179,7 @@ func (srv *UIService) GetPatient(c *gin.Context) {
defer wg.Done() defer wg.Done()
if _, err := io.Copy(sin, resp.Body); err != nil { if _, err := io.Copy(sin, resp.Body); err != nil {
log.Println("[ediviewer] Error reading EDIFACT:", err) log.Println("[ediviewer] Error reading EDIFACT:", err)
c.AbortWithError(500, err) c.AbortWithError(http.StatusInternalServerError, err)
} }
}() }()
@ -184,7 +187,7 @@ func (srv *UIService) GetPatient(c *gin.Context) {
defer wg.Done() defer wg.Done()
if _, err := io.Copy(c.Writer, sout); err != nil { if _, err := io.Copy(c.Writer, sout); err != nil {
log.Println("[ediviewer] Error output EDIFACT:", err) log.Println("[ediviewer] Error output EDIFACT:", err)
c.AbortWithError(500, err) c.AbortWithError(http.StatusInternalServerError, err)
} }
}() }()
@ -192,7 +195,7 @@ func (srv *UIService) GetPatient(c *gin.Context) {
defer wg.Done() defer wg.Done()
if _, err := io.Copy(os.Stderr, serr); err != nil { if _, err := io.Copy(os.Stderr, serr); err != nil {
log.Println("[ediviewer] Error stderr EDIFACT:", err) log.Println("[ediviewer] Error stderr EDIFACT:", err)
c.AbortWithError(500, err) c.AbortWithError(http.StatusInternalServerError, err)
} }
}() }()
@ -200,89 +203,26 @@ func (srv *UIService) GetPatient(c *gin.Context) {
if err := cmd.Wait(); err != nil { if err := cmd.Wait(); err != nil {
log.Println("[ediviewer] Failed:", err) log.Println("[ediviewer] Failed:", err)
c.AbortWithError(500, err) c.AbortWithError(http.StatusInternalServerError, err)
} }
} }
func (srv *UIService) Authenticate(c *gin.Context) { func (srv *UIService) Authenticate(c *gin.Context) {
// authHeader := c.Request.Header.Get("Authorization") // Maybe authenticate user
// log.Printf("authHeader: %v", authHeader)
// if authHeader != "1111" {
// c.Status(401)
// c.Abort()
// }
} }
func (srv *UIService) GetConnections(c *gin.Context) { func (srv *UIService) GetConnections(c *gin.Context) {
connections := []*sharedmodel.Connection{} connections := []*sharedmodel.Connection{}
srv.data.Preload("Services").Preload("Services.Service").Preload("Services.Subscriptions").Find(&connections) srv.data.Preload("Services").Preload("Services.Service").Preload("Services.Subscriptions").Find(&connections)
c.JSON(200, connections) c.JSON(http.StatusOK, connections)
} }
func (srv *UIService) GetRegistrations(c *gin.Context) { func (srv *UIService) GetRegistrations(c *gin.Context) {
registrations := []*sharedmodel.Registration{} registrations := []*sharedmodel.Registration{}
srv.data.Where("status = ?", sharedmodel.RegistrationStatusPending).Find(&registrations) srv.data.Where("status = ?", sharedmodel.RegistrationStatusPending).Find(&registrations)
c.JSON(200, registrations) c.JSON(http.StatusOK, registrations)
} }
// func (srv *UIService) GetSystems(c *gin.Context) {
// id := c.Param("id")
// for _, p := range patients {
// if p.PatientID == id {
// f, err := os.Open(path.Join("./data/patients", p.EDI))
// if err != nil {
// c.Error(err)
// return
// }
// io.Copy(c.Writer, f)
// return
// }
// }
// c.JSON(404, nil)
// }
// func (srv *UIService) GetPatients(c *gin.Context) {
// id := c.Param("id")
// for _, p := range patients {
// if p.PatientID == id {
// f, err := os.Open(path.Join("./data/patients", p.EDI))
// if err != nil {
// c.Error(err)
// return
// }
// io.Copy(c.Writer, f)
// return
// }
// }
// c.JSON(404, nil)
// }
// func (srv *UIService) GetPatient(c *gin.Context) {
// id := c.Param("id")
// for _, p := range patients {
// if p.PatientID == id {
// f, err := os.Open(path.Join("./data/patients", p.EDI))
// if err != nil {
// c.Error(err)
// return
// }
// io.Copy(c.Writer, f)
// return
// }
// }
// c.JSON(404, nil)
// }
func NewUIServer(addr string) *UIService { func NewUIServer(addr string) *UIService {
cert := loadCert() cert := loadCert()

Loading…
Cancel
Save