aboutsummaryrefslogtreecommitdiff
path: root/routes.go
blob: 3759382ad41dc80bc881825cae74d2613ebdaa41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	log "github.com/sirupsen/logrus"
	"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()),
					})
					log.WithError(err).WithField("rule", rule).Panic("oops")
					return // exit on first error
				}
			}

			c.JSON(200, rule)
		})
	}
}