opengist/internal/web/server/handler.go

41 lines
917 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"
)
type Handler func(ctx *context.OGContext) error
type Middleware func(next Handler) Handler
func (h Handler) ToEcho() echo.HandlerFunc {
return func(c echo.Context) error {
return h(c.(*context.OGContext))
}
}
func (m Middleware) ToEcho() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return m(func(c *context.OGContext) error {
return next(c)
}).ToEcho()
}
}
2024-12-16 00:09:32 +00:00
func (h Handler) toEchoHandler() echo.HandlerFunc {
return func(c echo.Context) error {
if ogc, ok := c.(*context.OGContext); ok {
return h(ogc)
}
// Could also add error handling for incorrect context type
return h(c.(*context.OGContext))
}
}
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
}