superkit/bootstrap/app/routes.go
2024-06-06 16:02:04 +02:00

61 lines
1.7 KiB
Go

package app
import (
"AABBCCDD/app/handlers"
"AABBCCDD/app/views/errors"
"log/slog"
"github.com/anthdm/gothkit/kit"
"github.com/anthdm/gothkit/kit/middleware"
"github.com/go-chi/chi/v5"
chimiddleware "github.com/go-chi/chi/v5/middleware"
)
// Define your global middleware
func InitializeMiddleware(router *chi.Mux) {
router.Use(chimiddleware.Logger)
router.Use(chimiddleware.Recoverer)
router.Use(middleware.WithRequestURL)
}
// Define your routes in here
func InitializeRoutes(router *chi.Mux) {
// Comment out to configure your authentication
authConfig := kit.AuthenticationConfig{
AuthFunc: handlers.HandleAuthentication,
RedirectURL: "/login",
}
// Routes that "might" have an authenticated user
router.Group(func(app chi.Router) {
app.Use(kit.WithAuthentication(authConfig, false)) // strict set to false
// Routes
app.Get("/", kit.Handler(handlers.HandleLandingIndex))
})
// Authenticated routes
//
// Routes that "must" have an authenticated user or else they
// will be redirected to the configured redirectURL, set in the
// AuthenticationConfig.
router.Group(func(app chi.Router) {
app.Use(kit.WithAuthentication(authConfig, true)) // strict set to true
// Routes
// app.Get("/path", kit.Handler(myHandler.HandleIndex))
})
}
// NotFoundHandler that will be called when the requested path could
// not be found.
func NotFoundHandler(kit *kit.Kit) error {
return kit.Render(errors.Error404())
}
// ErrorHandler that will be called on errors return from application handlers.
func ErrorHandler(kit *kit.Kit, err error) {
slog.Error("internal server error", "err", err.Error(), "path", kit.Request.URL.Path)
kit.Render(errors.Error500())
}