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

@ -3,7 +3,7 @@ package handlers
import (
"templ-tests/app/templates"
"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
)
var pcAbout templates.PageContext = templates.PageContext{
@ -11,6 +11,7 @@ var pcAbout templates.PageContext = templates.PageContext{
ActivePage: "about",
}
func About(c *gin.Context) {
templates.About(pcAbout).Render(c, c.Writer)
func About(f *fiber.Ctx) error {
Render(f, templates.About(pcAbout))
return nil
}

View File

@ -4,10 +4,10 @@ import (
"templ-tests/app/templates"
"templ-tests/app/types"
"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
)
func Home(c *gin.Context) {
func Home(f *fiber.Ctx) error {
user := types.User{
FirstName: "Pero",
LastName: "Perić",
@ -18,5 +18,6 @@ func Home(c *gin.Context) {
ActivePage: "home",
User: &user,
}
templates.Home(pc).Render(c, c.Writer)
Render(f, templates.Home(pc))
return nil
}

15
app/handlers/render.go Normal file
View File

@ -0,0 +1,15 @@
package handlers
import (
"github.com/a-h/templ"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
)
func Render(c *fiber.Ctx, component templ.Component, options ...func(*templ.ComponentHandler)) error {
componentHandler := templ.Handler(component)
for _, o := range options {
o(componentHandler)
}
return adaptor.HTTPHandler(componentHandler)(c)
}

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)
}