aboutsummaryrefslogtreecommitdiff
path: root/routes.go
diff options
context:
space:
mode:
authorEmiliano Ciavatta2020-04-07 18:46:36 +0000
committerEmiliano Ciavatta2020-04-07 18:46:56 +0000
commit468690c60ee2e57ed2ccb4375e9ada5d2fed9473 (patch)
tree138b9f4c69731abef66e789b0e044417824f5c49 /routes.go
parent590405d948530aecdf7399833c3d0b8585f5601b (diff)
Before storage refactor
Diffstat (limited to 'routes.go')
-rw-r--r--routes.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/routes.go b/routes.go
new file mode 100644
index 0000000..f44cff7
--- /dev/null
+++ b/routes.go
@@ -0,0 +1,33 @@
+package main
+
+import (
+ "fmt"
+ "github.com/gin-gonic/gin"
+ "github.com/go-playground/validator/v10"
+ "log"
+ "net/http"
+)
+
+func ApplicationRoutes(engine *gin.Engine) {
+ engine.Static("/", "./frontend/build")
+
+ api := engine.Group("/api")
+ {
+ api.POST("/rules", func(c *gin.Context) {
+ var rule Rule
+
+ if err := c.ShouldBindJSON(&rule); err != nil {
+ for _, fieldErr := range err.(validator.ValidationErrors) {
+ log.Println(fieldErr)
+ c.JSON(http.StatusBadRequest, gin.H{
+ "error": fmt.Sprintf("field '%v' does not respect the %v(%v) rule",
+ fieldErr.Field(), fieldErr.Tag(), fieldErr.Param()),
+ })
+ return // exit on first error
+ }
+ }
+
+ c.JSON(200, rule)
+ })
+ }
+}