2023-09-10 17:39:34 +02:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
|
2023-10-04 13:36:54 +02:00
|
|
|
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
|
2023-09-10 17:39:34 +02:00
|
|
|
"git.ekzyis.com/ekzyis/delphi.market/server/router/handler"
|
|
|
|
"git.ekzyis.com/ekzyis/delphi.market/server/router/middleware"
|
|
|
|
)
|
|
|
|
|
2023-10-04 13:36:54 +02:00
|
|
|
type ServerContext = context.ServerContext
|
|
|
|
|
|
|
|
type MiddlewareFunc func(sc ServerContext) echo.MiddlewareFunc
|
|
|
|
type HandlerFunc = func(sc ServerContext) echo.HandlerFunc
|
|
|
|
|
|
|
|
func AddRoutes(e *echo.Echo, sc ServerContext) {
|
|
|
|
mountMiddleware(e, sc)
|
|
|
|
addFrontendRoutes(e, sc)
|
|
|
|
addBackendRoutes(e, sc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mountMiddleware(e *echo.Echo, sc ServerContext) {
|
|
|
|
Use(e, sc, middleware.Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addFrontendRoutes(e *echo.Echo, sc ServerContext) {
|
|
|
|
GET(e, sc, "/user",
|
|
|
|
handler.HandleUser,
|
|
|
|
middleware.SessionGuard)
|
|
|
|
GET(e, sc, "/market/:id",
|
|
|
|
handler.HandleMarket,
|
|
|
|
middleware.SessionGuard)
|
|
|
|
POST(e, sc, "/market/:id/order",
|
2023-11-09 02:17:51 +01:00
|
|
|
handler.HandleOrder,
|
2023-10-04 13:36:54 +02:00
|
|
|
middleware.SessionGuard,
|
|
|
|
middleware.LNDGuard)
|
|
|
|
GET(e, sc, "/invoice/:id",
|
|
|
|
handler.HandleInvoice,
|
|
|
|
middleware.SessionGuard)
|
|
|
|
}
|
|
|
|
|
|
|
|
func addBackendRoutes(e *echo.Echo, sc ServerContext) {
|
2023-11-08 03:00:47 +01:00
|
|
|
GET(e, sc, "/api/markets", handler.HandleMarkets)
|
2023-11-20 02:47:16 +01:00
|
|
|
POST(e, sc, "/api/market",
|
|
|
|
handler.HandleCreateMarket,
|
|
|
|
middleware.SessionGuard,
|
|
|
|
middleware.LNDGuard)
|
2023-11-08 23:52:28 +01:00
|
|
|
GET(e, sc, "/api/market/:id", handler.HandleMarket)
|
2023-11-09 02:17:51 +01:00
|
|
|
POST(e, sc, "/api/order",
|
|
|
|
handler.HandleOrder,
|
|
|
|
middleware.SessionGuard,
|
|
|
|
middleware.LNDGuard)
|
2023-11-04 14:31:48 +01:00
|
|
|
GET(e, sc, "/api/login", handler.HandleLogin)
|
|
|
|
GET(e, sc, "/api/login/callback", handler.HandleLoginCallback)
|
2023-11-07 13:51:33 +01:00
|
|
|
POST(e, sc, "/api/logout", handler.HandleLogout)
|
2023-10-04 13:36:54 +02:00
|
|
|
GET(e, sc, "/api/session", handler.HandleCheckSession)
|
|
|
|
GET(e, sc, "/api/invoice/:id",
|
2023-11-03 23:43:56 +01:00
|
|
|
handler.HandleInvoiceStatus,
|
2023-10-04 13:36:54 +02:00
|
|
|
middleware.SessionGuard)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GET(e *echo.Echo, sc ServerContext, path string, scF HandlerFunc, scM ...MiddlewareFunc) *echo.Route {
|
|
|
|
return e.GET(path, scF(sc), toMiddlewareFunc(sc, scM...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func POST(e *echo.Echo, sc ServerContext, path string, scF HandlerFunc, scM ...MiddlewareFunc) *echo.Route {
|
|
|
|
return e.POST(path, scF(sc), toMiddlewareFunc(sc, scM...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Use(e *echo.Echo, sc ServerContext, scM ...MiddlewareFunc) {
|
|
|
|
e.Use(toMiddlewareFunc(sc, scM...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func toMiddlewareFunc(sc ServerContext, scM ...MiddlewareFunc) []echo.MiddlewareFunc {
|
|
|
|
var m []echo.MiddlewareFunc
|
|
|
|
for _, m_ := range scM {
|
|
|
|
m = append(m, m_(sc))
|
2023-09-10 17:39:34 +02:00
|
|
|
}
|
2023-10-04 13:36:54 +02:00
|
|
|
return m
|
2023-09-10 17:39:34 +02:00
|
|
|
}
|