7c69f163b6
This reverts commit a1b93c0eba.
145 lines
3.2 KiB
Go
145 lines
3.2 KiB
Go
package ersteller_lib
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"maragu.dev/gomponents"
|
|
hx "maragu.dev/gomponents-htmx"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type Route interface {
|
|
WithParams(params map[string]string) Route
|
|
ToUrl(params ...string) string
|
|
GetHtmx(params ...string) gomponents.Node
|
|
}
|
|
|
|
//type RouteAdder interface {
|
|
// Route
|
|
//}
|
|
|
|
type Router interface {
|
|
GET(path string, handler echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
|
POST(path string, handler echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
|
PUT(path string, handler echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
|
DELETE(path string, handler echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
|
PATCH(path string, handler echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
|
|
}
|
|
|
|
type CommonRoute struct {
|
|
Path string
|
|
Params map[string]string
|
|
Handler echo.HandlerFunc
|
|
}
|
|
|
|
func (r CommonRoute) Add(router Router) {
|
|
router.GET(r.Path, r.Handler)
|
|
}
|
|
|
|
func (r CommonRoute) GetHtmx(params ...string) gomponents.Node {
|
|
return hx.Get(r.ToUrl(params...))
|
|
}
|
|
|
|
func NewCommonRoute(path string, handler echo.HandlerFunc) CommonRoute {
|
|
return CommonRoute{Path: path, Handler: handler, Params: make(map[string]string)}
|
|
}
|
|
|
|
func (r CommonRoute) WithParams(params map[string]string) Route {
|
|
r.Params = params
|
|
return r
|
|
}
|
|
|
|
func (r CommonRoute) ToUrl(params ...string) string {
|
|
for k, v := range r.Params {
|
|
r.Path = strings.ReplaceAll(r.Path, ":"+k, v)
|
|
}
|
|
if len(params) > 0 {
|
|
r.Path += "?" + strings.Join(params, "&")
|
|
}
|
|
return r.Path
|
|
}
|
|
|
|
type GetRoute struct {
|
|
CommonRoute
|
|
}
|
|
|
|
func NewGetRoute(path string, handler echo.HandlerFunc) GetRoute {
|
|
return GetRoute{CommonRoute: NewCommonRoute(path, handler)}
|
|
}
|
|
|
|
func (r GetRoute) Add(router Router) GetRoute {
|
|
router.GET(r.Path, r.Handler)
|
|
return r
|
|
}
|
|
|
|
func (r GetRoute) GetHtmx(params ...string) gomponents.Node {
|
|
return hx.Get(r.ToUrl(params...))
|
|
}
|
|
|
|
type PutRoute struct {
|
|
CommonRoute
|
|
}
|
|
|
|
func NewPutRoute(path string, handler echo.HandlerFunc) PutRoute {
|
|
return PutRoute{CommonRoute: NewCommonRoute(path, handler)}
|
|
}
|
|
|
|
func (r PutRoute) Add(router Router) PutRoute {
|
|
router.PUT(r.Path, r.Handler)
|
|
return r
|
|
}
|
|
|
|
func (r PutRoute) WithParams(params map[string]string) Route {
|
|
r.Params = params
|
|
return r
|
|
}
|
|
|
|
func (r PutRoute) GetHtmx(params ...string) gomponents.Node {
|
|
return hx.Put(r.ToUrl(params...))
|
|
}
|
|
|
|
type PostRoute struct {
|
|
CommonRoute
|
|
}
|
|
|
|
func NewPostRoute(path string, handler echo.HandlerFunc) PostRoute {
|
|
return PostRoute{CommonRoute: NewCommonRoute(path, handler)}
|
|
}
|
|
|
|
func (r PostRoute) Add(router Router) PostRoute {
|
|
router.POST(r.Path, r.Handler)
|
|
return r
|
|
}
|
|
|
|
func (r PostRoute) WithParams(params map[string]string) Route {
|
|
r.Params = params
|
|
return r
|
|
}
|
|
|
|
func (r PostRoute) GetHtmx(params ...string) gomponents.Node {
|
|
return hx.Post(r.ToUrl(params...))
|
|
}
|
|
|
|
type DeleteRoute struct {
|
|
CommonRoute
|
|
}
|
|
|
|
func NewDeleteRoute(path string, handler echo.HandlerFunc) DeleteRoute {
|
|
return DeleteRoute{CommonRoute: NewCommonRoute(path, handler)}
|
|
}
|
|
|
|
func (r DeleteRoute) Add(router Router) DeleteRoute {
|
|
router.DELETE(r.Path, r.Handler)
|
|
return r
|
|
}
|
|
|
|
func (r DeleteRoute) WithParams(params map[string]string) Route {
|
|
r.Params = params
|
|
return r
|
|
}
|
|
|
|
func (r DeleteRoute) GetHtmx(params ...string) gomponents.Node {
|
|
return hx.Delete(r.ToUrl(params...))
|
|
}
|