This commit is contained in:
Eden Kirin
2024-02-01 21:50:21 +01:00
parent 725e127881
commit 15ba332ae5
6 changed files with 102 additions and 25 deletions

View File

@ -2,10 +2,12 @@ package router
import (
"fmt"
"strings"
"templ-tests/app/handlers"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
)
const (
@ -14,31 +16,62 @@ const (
STATIC_PATH string = "./static"
)
func initRouter() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.Use(corsMiddleware())
func initApp() *fiber.App {
app := fiber.New()
return app
}
router.GET("/", handlers.Home)
router.GET("/about", handlers.About)
interactiveRouter := router.Group("/interactive")
{
interactiveRouter.GET("", handlers.Interactive)
interactiveRouter.GET("/swap-content", handlers.InteractiveSwapContent)
interactiveRouter.GET("/filter-cat-breeds", handlers.FilterCatBreeds)
interactiveRouter.POST("/validate-form", handlers.ValidateForm)
func initCorsMiddleware(app *fiber.App) {
allowHeaders := []string{
"Content-Type",
"Content-Length",
"Accept-Encoding",
"X-CSRF-Token",
"Authorization",
"accept",
"origin",
"Cache-Control",
"X-Requested-With",
"x-timezone",
"Access-Control-Allow-Origin",
"Access-Control-Max-Age",
}
router.Use(static.Serve("/static", static.LocalFile(STATIC_PATH, false)))
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowHeaders: strings.Join(allowHeaders[:], ", "),
}))
}
return router
func initLogging(app *fiber.App) {
app.Use(
logger.New(), // add Logger middleware
)
}
func initRouter(app *fiber.App) {
app.Get("/", handlers.Home)
app.Get("/about", handlers.About)
// interactiveRouter := router.Group("/interactive")
// {
// interactiveRouter.GET("", handlers.Interactive)
// interactiveRouter.GET("/swap-content", handlers.InteractiveSwapContent)
// interactiveRouter.GET("/filter-cat-breeds", handlers.FilterCatBreeds)
// interactiveRouter.POST("/validate-form", handlers.ValidateForm)
// }
app.Static("/static", STATIC_PATH)
}
func Serve() {
serverAddr := fmt.Sprintf("%s:%d", APPHOST, APPPORT)
fmt.Printf("Starting serving on http://%s\n", serverAddr)
router := initRouter()
router.Run(serverAddr)
app := initApp()
initLogging(app)
initCorsMiddleware(app)
initRouter(app)
app.Listen(serverAddr)
}