mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-07-24 13:38:48 +02:00
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package apis
|
|
|
|
import (
|
|
"datamanager/backend/core"
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/labstack/echo/v5/middleware"
|
|
"github.com/pocketbase/pocketbase/tools/rest"
|
|
"strings"
|
|
)
|
|
|
|
const fieldsQueryParam = "fields"
|
|
|
|
func InitApi(app core.App) (*echo.Echo, error) {
|
|
e := echo.New()
|
|
e.Debug = false
|
|
e.Binder = &rest.MultiBinder{}
|
|
e.JSONSerializer = &rest.Serializer{
|
|
FieldsParam: fieldsQueryParam,
|
|
}
|
|
|
|
// configure a custom router
|
|
e.ResetRouterCreator(func(ec *echo.Echo) echo.Router {
|
|
return echo.NewRouter(echo.RouterConfig{
|
|
UnescapePathParamValues: true,
|
|
AllowOverwritingRoute: true,
|
|
})
|
|
})
|
|
|
|
// default middlewares
|
|
e.Pre(middleware.RemoveTrailingSlashWithConfig(middleware.RemoveTrailingSlashConfig{
|
|
Skipper: func(c echo.Context) bool {
|
|
// enable by default only for the API routes
|
|
return !strings.HasPrefix(c.Request().URL.Path, "/api/")
|
|
},
|
|
}))
|
|
e.Use(middleware.Recover())
|
|
e.Use(middleware.Secure())
|
|
|
|
// custom error handler
|
|
e.HTTPErrorHandler = func(c echo.Context, err error) {
|
|
if err == nil {
|
|
return // no error
|
|
}
|
|
|
|
if c.Response().Committed {
|
|
return // already committed
|
|
}
|
|
|
|
}
|
|
|
|
return e, nil
|
|
}
|