116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
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()}
|
|
e.Renderer = initTemplates()
|
|
|
|
return e
|
|
}
|
|
|
|
func SetupRoutes(e *echo.Echo, ctrl *controller.Controller, env *env.Config) {
|
|
favicon := e.Group("/favicon", longCacheLifetime)
|
|
favicon.Static("/", "web/favicon")
|
|
fonts := e.Group("/fonts", longCacheLifetime)
|
|
fonts.Static("/", "web/fonts")
|
|
img := e.Group("/img", longCacheLifetime)
|
|
img.Static("/", "web/img")
|
|
|
|
e.Static("/css", "web/css")
|
|
e.Static("/js", "web/js")
|
|
|
|
e.GET("/", func(c echo.Context) error {
|
|
return c.Render(http.StatusOK, "index.html", nil)
|
|
})
|
|
|
|
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, env.Version)
|
|
})
|
|
}
|
|
|
|
e.GET("/robots.txt", func(ctx echo.Context) error {
|
|
return ctx.String(http.StatusOK, "User-agent: *\nDisallow: /")
|
|
})
|
|
e.RouteNotFound("*", func(ctx echo.Context) error {
|
|
return ctx.Redirect(http.StatusTemporaryRedirect, "/")
|
|
})
|
|
|
|
if env.SwaggerHost != "" {
|
|
docs.SwaggerInfo.Title = "Café Plätschwiesle"
|
|
docs.SwaggerInfo.Description = "This is the backend of Café Plätschwiesle"
|
|
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"))
|
|
}
|
|
}
|
|
}
|