Initial
This commit is contained in:
1
app/router/helpers.go
Normal file
1
app/router/helpers.go
Normal file
@ -0,0 +1 @@
|
||||
package router
|
||||
80
app/router/router.go
Normal file
80
app/router/router.go
Normal file
@ -0,0 +1,80 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"fiber-sessions/app/handlers"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"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(fiber.Config{
|
||||
CaseSensitive: true,
|
||||
StrictRouting: true,
|
||||
})
|
||||
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)
|
||||
|
||||
interactiveGroup := app.Group("/interactive")
|
||||
{
|
||||
interactiveGroup.Get("", handlers.Interactive)
|
||||
interactiveGroup.Get("/swap-content", handlers.InteractiveSwapContent)
|
||||
interactiveGroup.Get("/filter-cat-breeds", handlers.FilterCatBreeds)
|
||||
interactiveGroup.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)
|
||||
}
|
||||
Reference in New Issue
Block a user