78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package router
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"templ-tests/app/handlers"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
)
|
|
|
|
const (
|
|
APPHOST string = "127.0.0.1"
|
|
APPPORT int = 8080
|
|
STATIC_PATH string = "./static"
|
|
)
|
|
|
|
func initApp() *fiber.App {
|
|
app := fiber.New()
|
|
return app
|
|
}
|
|
|
|
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",
|
|
}
|
|
|
|
app.Use(cors.New(cors.Config{
|
|
AllowOrigins: "*",
|
|
AllowHeaders: strings.Join(allowHeaders[:], ", "),
|
|
}))
|
|
}
|
|
|
|
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)
|
|
|
|
app := initApp()
|
|
initLogging(app)
|
|
initCorsMiddleware(app)
|
|
initRouter(app)
|
|
|
|
app.Listen(serverAddr)
|
|
}
|