1package httpserve
2
3import (
4 "github.com/bnema/gordon/internal/httpserve/handler"
5 "github.com/bnema/gordon/internal/httpserve/middleware"
6 "github.com/bnema/gordon/internal/server"
7 _ "github.com/joho/godotenv/autoload"
8 "github.com/labstack/echo/v4"
9 echomid "github.com/labstack/echo/v4/middleware"
10)
11
12// RegisterRoutes registers all routes and middlewares
13func RegisterRoutes(e *echo.Echo, a *server.App) *echo.Echo {
14 // e.Use(echomid.Logger()) too verbose
15 e.Use(echomid.Recover())
16 // Initiate the session middleware
17 e.Use(middleware.InitSessionMiddleware(a))
18
19 // Language detection
20 e.Use(middleware.LanguageDetection)
21
22 AdminPath := a.Config.Admin.Path
23 bindLoginRoute(e, a, AdminPath)
24 bindAPIEndpoints(e, a)
25
26 // SetCommonDataMiddleware will pass data to the renderer
27 e.Use(middleware.SetCommonDataMiddleware(a))
28
29 // Error handler middleware
30 e.Use(middleware.ErrorHandler)
31
32 // Use middlewares
33 e.Use(middleware.SecureRoutes(a))
34
35 // Color scheme detection for dark/light mode
36 e.Use(middleware.ColorSchemeDetection)
37
38 bindAdminRoute(e, a, AdminPath)
39 bindStaticRoute(e, a, "/*")
40 bindHTMXEndpoints(e, a)
41
42 // Protect the root path with a 403
43 e.GET("/", func(c echo.Context) error {
44 return c.String(403, "403 Forbidden")
45 })
46
47 return e
48}
49
50// bindAPIEndpoints expose /api endpoints and speaks only JSON
51func bindAPIEndpoints(e *echo.Echo, a *server.App) {
52 apiGroup := e.Group("/api")
53
54 // Device flow endpoints (without RequireToken middleware)
55 apiGroup.POST("/device/code", func(c echo.Context) error {
56 return handler.DeviceCodeRequest(c, a)
57 })
58 apiGroup.POST("/device/token", func(c echo.Context) error {
59 return handler.DeviceTokenRequest(c, a)
60 })
61
62 // Other API endpoints that require a token
63 protectedApiGroup := apiGroup.Group("", middleware.RequireToken(a))
64 protectedApiGroup.GET("/ping", func(c echo.Context) error {
65 return handler.GetInfos(c, a)
66 })
67 protectedApiGroup.POST("/deploy", func(c echo.Context) error {
68 return handler.PostDeploy(c, a)
69 })
70 protectedApiGroup.POST("/push", func(c echo.Context) error {
71 return handler.PostPush(c, a)
72 })
73}
74
75// bindStaticRoute bind static path
76func bindStaticRoute(e *echo.Echo, a *server.App, path string) {
77 e.GET(path, func(c echo.Context) error {
78 return handler.StaticRoute(c, a)
79 }, echomid.Gzip())
80}
81
82// bindLoginRoute binds all login routes
83func bindLoginRoute(e *echo.Echo, a *server.App, adminPath string) {
84 e.GET(adminPath+"/login", func(c echo.Context) error {
85 return handler.RenderLoginPage(c, a)
86 })
87 e.GET(adminPath+"/login/oauth/github", func(c echo.Context) error {
88 return handler.StartOAuthGithub(c, a)
89 })
90 e.GET(adminPath+"/login/oauth/callback", func(c echo.Context) error {
91 return handler.OAuthCallback(c, a)
92 })
93 e.GET(adminPath+"/logout", func(c echo.Context) error {
94 return handler.Logout(c, a)
95 })
96}
97
98// bindAdminRoute binds all admin routes
99func bindAdminRoute(e *echo.Echo, a *server.App, adminPath string) {
100 adminGroup := e.Group(adminPath)
101 // Since login is behind the admin path, we cannot use group middleware
102 adminGroup.GET("", func(c echo.Context) error {
103 return c.Redirect(302, adminPath+"/manager")
104 }, middleware.RequireLogin(a))
105
106 adminGroup.GET("/manager", func(c echo.Context) error {
107 return handler.AdminManagerRoute(c, a)
108 }, middleware.RequireLogin(a))
109 adminGroup.GET("/create-container/:ID", func(c echo.Context) error {
110 return handler.CreateContainerFullGET(c, a)
111 }, middleware.RequireLogin(a))
112}
113
114// bindHTMXEndpoints binds all HTMX endpoints
115func bindHTMXEndpoints(e *echo.Echo, a *server.App) {
116 // Create a group for /htmx endpoints
117 htmxGroup := e.Group("/htmx")
118
119 // Apply middleware to the group, so all endpoints are protected
120 htmxGroup.Use(middleware.RequireLogin(a))
121
122 // List all images component
123 htmxGroup.GET("/image-manager", func(c echo.Context) error {
124 return handler.ImageManagerComponent(c, a)
125 })
126 // Delete an image
127 htmxGroup.DELETE("/image-manager/delete/:ID", func(c echo.Context) error {
128 return handler.ImageManagerDelete(c, a)
129 })
130
131 // List all containers
132 htmxGroup.GET("/container-manager", func(c echo.Context) error {
133 return handler.ContainerManagerComponent(c, a)
134 })
135 // Stop a container
136 htmxGroup.POST("/container-manager/stop/:ID", func(c echo.Context) error {
137 return handler.ContainerManagerStop(c, a)
138 })
139 // Delete a container
140 htmxGroup.DELETE("/container-manager/delete/:ID", func(c echo.Context) error {
141 return handler.ContainerManagerDelete(c, a)
142 })
143 // Start a container
144 htmxGroup.POST("/container-manager/start/:ID", func(c echo.Context) error {
145 return handler.ContainerManagerStart(c, a)
146 })
147 // Edit a container view
148 htmxGroup.GET("/container-manager/edit/:ID", func(c echo.Context) error {
149 return handler.ContainerManagerEditGET(c, a)
150 })
151 // Edit a container action
152 htmxGroup.POST("/container-manager/edit/:ID", func(c echo.Context) error {
153 return handler.ContainerManagerEditPOST(c, a)
154 })
155
156 // Display upload-image component
157 htmxGroup.GET("/upload-image", func(c echo.Context) error {
158 return handler.UploadImageGETHandler(c, a)
159 })
160 // Upload image
161 htmxGroup.POST("/upload-image", func(c echo.Context) error {
162 return handler.UploadImagePOSTHandler(c, a)
163 })
164 // Display create-container component
165 htmxGroup.GET("/create-container/:ID", func(c echo.Context) error {
166 return handler.CreateContainerGET(c, a)
167 })
168 // Create container
169 htmxGroup.POST("/create-container/:ID", func(c echo.Context) error {
170 return handler.CreateContainerPOST(c, a)
171 })
172}