opengist/internal/web/server/handler.go

41 lines
907 B
Go
Raw Normal View History

2024-12-03 01:18:04 +00:00
package server
import (
"github.com/labstack/echo/v4"
"github.com/thomiceli/opengist/internal/web/context"
)
2025-01-06 13:18:31 +00:00
type Handler func(ctx *context.Context) error
2024-12-03 01:18:04 +00:00
type Middleware func(next Handler) Handler
2025-01-06 13:18:31 +00:00
func (h Handler) toEcho() echo.HandlerFunc {
2024-12-03 01:18:04 +00:00
return func(c echo.Context) error {
2025-01-06 13:18:31 +00:00
return h(c.(*context.Context))
2024-12-03 01:18:04 +00:00
}
}
2025-01-06 13:18:31 +00:00
func (m Middleware) toEcho() echo.MiddlewareFunc {
2024-12-03 01:18:04 +00:00
return func(next echo.HandlerFunc) echo.HandlerFunc {
2025-01-06 13:18:31 +00:00
return m(func(c *context.Context) error {
2024-12-03 01:18:04 +00:00
return next(c)
2025-01-06 13:18:31 +00:00
}).toEcho()
2024-12-03 01:18:04 +00:00
}
}
2024-12-16 00:09:32 +00:00
func (h Handler) toEchoHandler() echo.HandlerFunc {
return func(c echo.Context) error {
2025-01-06 13:18:31 +00:00
if ogc, ok := c.(*context.Context); ok {
2024-12-16 00:09:32 +00:00
return h(ogc)
}
// Could also add error handling for incorrect context type
2025-01-06 13:18:31 +00:00
return h(c.(*context.Context))
2024-12-16 00:09:32 +00:00
}
}
2025-01-02 19:55:33 +00:00
func chain(h Handler, middleware ...Middleware) Handler {
2024-12-16 00:09:32 +00:00
for i := len(middleware) - 1; i >= 0; i-- {
h = middleware[i](h)
}
return h
}