package router import ( "html/template" "kontor-api-gin/assets" "kontor-api-gin/internal/handlers" "kontor-api-gin/internal/middleware" "kontor-api-gin/templates" "net/http" "log/slog" "github.com/gin-contrib/cors" gin_slog "github.com/gin-contrib/slog" "github.com/gin-gonic/gin" ) func New(comics *handlers.ComicHandler, auth *handlers.AuthHandler, secret []byte) *gin.Engine { router := gin.New() router.Use(gin.Recovery()) router.Use(gin_slog.SetLogger( gin_slog.WithSkipPath([]string{"/health", "/public/style/main.css"}), gin_slog.WithDefaultLevel(slog.LevelInfo), gin_slog.WithClientErrorLevel(slog.LevelInfo), gin_slog.WithServerErrorLevel(slog.LevelInfo), )) router.Use(cors.New(cors.Config{ AllowOrigins: []string{"https://app.example.com"}, AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE"}, AllowHeaders: []string{"Authorization", "Content-Type"}, })) templ := template.Must(template.New("").ParseFS(templates.TemplateFiles, "layouts/*.html", "pages/*.html")) router.SetHTMLTemplate(templ) router.StaticFS("/public", http.FS(assets.StaticFiles)) router.GET("/health", func(c *gin.Context) { c.JSON(200, gin.H{"ok": true}) }) api := router.Group("/api") api.POST("/login", auth.Login) v1 := api.Group("/v1") { secured := v1.Group("") secured.Use(middleware.JWT(secret)) secured.GET("comics/comics", comics.List) } return router }