Restructure project
This commit is contained in:
parent
e44e7caa11
commit
16b2f17301
46 changed files with 1744 additions and 1265 deletions
13
internal/router/middleware.go
Normal file
13
internal/router/middleware.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func authHeader(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
c.Response().Header().Set("Remote-Groups", c.Request().Header.Get("Remote-Groups"))
|
||||
c.Response().Header().Set("Remote-Name", c.Request().Header.Get("Remote-Name"))
|
||||
return next(c)
|
||||
}
|
||||
}
|
94
internal/router/router.go
Normal file
94
internal/router/router.go
Normal file
|
@ -0,0 +1,94 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
echoSwagger "github.com/swaggo/echo-swagger"
|
||||
"gitlab.unjx.de/flohoss/cafe-plaetschwiesle/docs"
|
||||
"gitlab.unjx.de/flohoss/cafe-plaetschwiesle/internal/controller"
|
||||
"gitlab.unjx.de/flohoss/cafe-plaetschwiesle/internal/env"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func InitRouter() *echo.Echo {
|
||||
e := echo.New()
|
||||
|
||||
e.HideBanner = true
|
||||
e.HidePort = true
|
||||
|
||||
e.Use(middleware.Recover())
|
||||
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
|
||||
Skipper: func(c echo.Context) bool {
|
||||
return strings.Contains(c.Request().URL.Path, "swagger")
|
||||
},
|
||||
}))
|
||||
e.Pre(middleware.RemoveTrailingSlash())
|
||||
|
||||
e.Validator = &CustomValidator{Validator: newValidator()}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func SetupRoutes(e *echo.Echo, ctrl *controller.Controller, env *env.Config) {
|
||||
api := e.Group("/api")
|
||||
{
|
||||
tableGroup := api.Group("/tables")
|
||||
{
|
||||
tableGroup.GET("", ctrl.GetTables)
|
||||
tableGroup.POST("", ctrl.CreateTable)
|
||||
tableGroup.DELETE("", ctrl.DeleteTable)
|
||||
}
|
||||
orderGroup := api.Group("/orders")
|
||||
{
|
||||
orderGroup.GET("", ctrl.GetOrders)
|
||||
orderGroup.POST("", ctrl.CreateOrder)
|
||||
orderGroup.DELETE("", ctrl.DeleteOrder)
|
||||
orderGroup.PUT("", ctrl.UpdateOrder)
|
||||
orderGroup.GET("/sse", echo.WrapHandler(http.HandlerFunc(ctrl.SSE.ServeHTTP)))
|
||||
orderItemGroup := orderGroup.Group("/items")
|
||||
{
|
||||
orderItemGroup.GET("", ctrl.GetOrderItems)
|
||||
orderItemGroup.POST("", ctrl.CreateOrderItem)
|
||||
orderItemGroup.PUT("", ctrl.UpdateOrderItem)
|
||||
orderItemGroup.DELETE("/:id", ctrl.DeleteOrderItem)
|
||||
}
|
||||
}
|
||||
billGroup := api.Group("/bills")
|
||||
{
|
||||
billGroup.GET("", ctrl.GetBills)
|
||||
billGroup.POST("", ctrl.CreateBill)
|
||||
billGroup.DELETE("/:id", ctrl.DeleteBill)
|
||||
billItemGroup := billGroup.Group("/items")
|
||||
{
|
||||
billItemGroup.GET("", ctrl.GetBillItems)
|
||||
}
|
||||
}
|
||||
userGroup := api.Group("/users")
|
||||
{
|
||||
userGroup.GET("/:username", ctrl.GetUser)
|
||||
userGroup.PUT("", ctrl.UpdateUser)
|
||||
}
|
||||
health := api.Group("/health", authHeader)
|
||||
{
|
||||
health.GET("", func(ctx echo.Context) error {
|
||||
return ctx.String(http.StatusOK, ".")
|
||||
})
|
||||
}
|
||||
|
||||
if env.SwaggerHost != "" {
|
||||
docs.SwaggerInfo.Title = "Cafe"
|
||||
docs.SwaggerInfo.Description = "This is the backend of a cafe"
|
||||
docs.SwaggerInfo.Version = env.Version
|
||||
docs.SwaggerInfo.BasePath = "/api"
|
||||
parsed, _ := url.Parse(env.SwaggerHost)
|
||||
docs.SwaggerInfo.Host = parsed.Host
|
||||
|
||||
api.GET("/swagger/*", echoSwagger.WrapHandler)
|
||||
zap.L().Info("swagger running", zap.String("url", env.SwaggerHost+"/api/swagger/index.html"))
|
||||
}
|
||||
}
|
||||
}
|
17
internal/router/validate.go
Normal file
17
internal/router/validate.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type CustomValidator struct {
|
||||
Validator *validator.Validate
|
||||
}
|
||||
|
||||
func (cv *CustomValidator) Validate(i interface{}) error {
|
||||
return cv.Validator.Struct(i)
|
||||
}
|
||||
|
||||
func newValidator() *validator.Validate {
|
||||
return validator.New()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue