From 77e07832dbb4baa5c7e41a5c13c8752401fb8eb3 Mon Sep 17 00:00:00 2001 From: Bas Kloosterman Date: Wed, 31 May 2023 17:18:27 +0200 Subject: [PATCH] Cleanup whitebox server code --- whiteboxservice/srv.go | 108 +++++++++++-------------------------------------- 1 file changed, 24 insertions(+), 84 deletions(-) diff --git a/whiteboxservice/srv.go b/whiteboxservice/srv.go index 6981be7..91875fd 100644 --- a/whiteboxservice/srv.go +++ b/whiteboxservice/srv.go @@ -57,7 +57,7 @@ func (srv *UIService) init() { r.Use(srv.Authenticate) r.GET("/", func(c *gin.Context) { - c.Redirect(301, "/ui") + c.Redirect(http.StatusMovedPermanently, "/ui") }) r.GET("/ui", 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/:patientID", srv.GetPatient) 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 } @@ -79,14 +77,14 @@ func (srv *UIService) GetConnection(c *gin.Context) { connID := c.Param("connID") connection := &sharedmodel.Connection{} srv.data.Where("id = ?", connID).Find(&connection) - c.JSON(200, connection) + c.JSON(http.StatusOK, connection) } func (srv *UIService) GetSubscriptions(c *gin.Context) { connID := c.Param("connID") serviceID := c.Param("serviceID") serviceConfig := &sharedmodel.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) { @@ -96,7 +94,7 @@ func (srv *UIService) GetPatient(c *gin.Context) { patient := &sharedmodel.Subscription{} 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 { - c.AbortWithError(500, err) + c.AbortWithError(http.StatusInternalServerError, err) return } 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) - log.Println(err, protoconfig) + if err != nil { + c.AbortWithStatus(http.StatusBadRequest) + return + } 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"]) req, _ := http.NewRequest("GET", url, nil) - // req.Header.Set("Authorization", serviceConfig.FetchProtocol.AuthConfig.Raw) client := &http.Client{ Transport: &http.Transport{ @@ -129,7 +132,7 @@ func (srv *UIService) GetPatient(c *gin.Context) { resp, err := client.Do(req) if err != nil { - c.AbortWithError(500, err) + c.AbortWithError(http.StatusInternalServerError, err) return } @@ -142,7 +145,7 @@ func (srv *UIService) GetPatient(c *gin.Context) { sin, err := cmd.StdinPipe() if err != nil { log.Println("[ediviewer] Failed to open stdin pipe:", err) - c.AbortWithError(500, err) + c.AbortWithError(http.StatusInternalServerError, err) return } defer sin.Close() @@ -150,7 +153,7 @@ func (srv *UIService) GetPatient(c *gin.Context) { serr, err := cmd.StderrPipe() if err != nil { log.Println("[ediviewer] Failed to open stderr pipe:", err) - c.AbortWithError(500, err) + c.AbortWithError(http.StatusInternalServerError, err) return } defer serr.Close() @@ -158,14 +161,14 @@ func (srv *UIService) GetPatient(c *gin.Context) { sout, err := cmd.StdoutPipe() if err != nil { log.Println("[ediviewer] Failed to open stdout pipe:", err) - c.AbortWithError(500, err) + c.AbortWithError(http.StatusInternalServerError, err) return } defer sout.Close() if err := cmd.Start(); err != nil { log.Println("[ediviewer] Failed to start:", err) - c.AbortWithError(500, err) + c.AbortWithError(http.StatusInternalServerError, err) return } @@ -176,7 +179,7 @@ func (srv *UIService) GetPatient(c *gin.Context) { defer wg.Done() if _, err := io.Copy(sin, resp.Body); err != nil { 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() if _, err := io.Copy(c.Writer, sout); err != nil { 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() if _, err := io.Copy(os.Stderr, serr); err != nil { 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 { log.Println("[ediviewer] Failed:", err) - c.AbortWithError(500, err) + c.AbortWithError(http.StatusInternalServerError, err) } } func (srv *UIService) Authenticate(c *gin.Context) { - // authHeader := c.Request.Header.Get("Authorization") - // log.Printf("authHeader: %v", authHeader) - - // if authHeader != "1111" { - // c.Status(401) - // c.Abort() - // } + // Maybe authenticate user } func (srv *UIService) GetConnections(c *gin.Context) { connections := []*sharedmodel.Connection{} 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) { registrations := []*sharedmodel.Registration{} srv.data.Where("status = ?", sharedmodel.RegistrationStatusPending).Find(®istrations) - 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 { cert := loadCert()