diff --git a/server/main.go b/server/main.go index 8186a19..d66655c 100644 --- a/server/main.go +++ b/server/main.go @@ -29,7 +29,7 @@ func main() { router.Static("/assets", "client/assets") // Only allow CORS in development mode - if server.Config.Mode == "development" { + if server.Config.Mode == "debug" { router.Use(middleware.CorsMiddleware()) } @@ -57,6 +57,11 @@ func main() { loginProtected.GET("/api/alerts/:alertId", routes.GetAlert(server)) loginProtected.PUT("/api/alerts/:alertId", routes.PutAlert(server)) loginProtected.DELETE("/api/alerts/:alertId", routes.DeleteAlert(server)) + loginProtected.GET("/api/contact-points", routes.GetContactPoints(server)) + loginProtected.POST("/api/contact-points", routes.PostContactPoints(server)) + loginProtected.GET("/api/contact-points/:contactPointId", routes.GetContactPoint(server)) + loginProtected.PUT("/api/contact-points/:contactPointId", routes.PutContactPoint(server)) + loginProtected.DELETE("/api/contact-points/:contactPointId", routes.DeleteContactPoint(server)) loginProtected.POST("/api/logout", routes.Logout(server)) // Routes accessible using auth key diff --git a/server/middleware/cors.go b/server/middleware/cors.go index 1e28715..65bb5e2 100644 --- a/server/middleware/cors.go +++ b/server/middleware/cors.go @@ -1,6 +1,8 @@ package middleware -import "github.com/gin-gonic/gin" +import ( + "github.com/gin-gonic/gin" +) func CorsMiddleware() gin.HandlerFunc { return func(c *gin.Context) { @@ -8,5 +10,9 @@ func CorsMiddleware() gin.HandlerFunc { c.Header("Access-Control-Allow-Credentials", "true") // 2 hours c.Header("Access-Control-Max-Age", "7200") + + if c.Request.Method == "OPTIONS" { + c.AbortWithStatus(200) + } } } diff --git a/server/routes/contact_points.go b/server/routes/contact_points.go index 0db51ae..b673d20 100644 --- a/server/routes/contact_points.go +++ b/server/routes/contact_points.go @@ -1 +1,127 @@ package routes + +import ( + "basic-sensor-receiver/app" + "net/http" + "strconv" + + "github.com/gin-gonic/gin" +) + +type postContactPointsBody struct { + Name string `json:"name"` + Type string `json:"type"` + TypeConfig string `json:"typeConfig"` +} + +type putContactPointsBody struct { + Name string `json:"name"` + Type string `json:"type"` + TypeConfig string `json:"typeConfig"` +} + +func GetContactPoints(s *app.Server) gin.HandlerFunc { + return func(c *gin.Context) { + contactPoints, err := s.Services.ContactPoints.GetList() + + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + c.JSON(http.StatusOK, contactPoints) + } +} + +func PostContactPoints(s *app.Server) gin.HandlerFunc { + return func(c *gin.Context) { + body := postContactPointsBody{} + + if err := c.BindJSON(&body); err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + contactPoint, err := s.Services.ContactPoints.Create(body.Name, body.Type, body.TypeConfig) + + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + c.JSON(http.StatusOK, contactPoint) + } +} + +func PutContactPoint(s *app.Server) gin.HandlerFunc { + return func(c *gin.Context) { + body := putContactPointsBody{} + + contactPointId, err := getContactPointId(c) + + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + if err := c.BindJSON(&body); err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + contactPoint, err := s.Services.ContactPoints.Update(contactPointId, body.Name, body.Type, body.TypeConfig) + + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + c.JSON(http.StatusOK, contactPoint) + } +} + +func GetContactPoint(s *app.Server) gin.HandlerFunc { + return func(c *gin.Context) { + contactPointId, err := getContactPointId(c) + + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + contactPoint, err := s.Services.ContactPoints.GetById(contactPointId) + + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + c.JSON(http.StatusOK, contactPoint) + } +} + +func DeleteContactPoint(s *app.Server) gin.HandlerFunc { + return func(c *gin.Context) { + contactPointId, err := getContactPointId(c) + + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + err = s.Services.ContactPoints.Delete(contactPointId) + + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + c.JSON(http.StatusOK, gin.H{}) + } +} + +func getContactPointId(c *gin.Context) (int64, error) { + sensor := c.Param("contactPointId") + + return strconv.ParseInt(sensor, 10, 64) +} diff --git a/server/services/contact_points.go b/server/services/contact_points_service.go similarity index 100% rename from server/services/contact_points.go rename to server/services/contact_points_service.go