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 apiGroup.Use(middleware.RequireToken(a))
54 apiGroup.GET("/ping", func(c echo.Context) error {
55 return handler.GetInfos(c, a)
56 })
57 apiGroup.POST("/push", func(c echo.Context) error {
58 return handler.PostDeploy(c, a)
59 })
60}
61
62// bindStaticRoute bind static path
63func bindStaticRoute(e *echo.Echo, a *server.App, path string) {
64 e.GET(path, func(c echo.Context) error {
65 return handler.StaticRoute(c, a)
66 }, echomid.Gzip())
67}
68
69// bindLoginRoute binds all login routes
70func bindLoginRoute(e *echo.Echo, a *server.App, adminPath string) {
71 e.GET(adminPath+"/login", func(c echo.Context) error {
72 return handler.RenderLoginPage(c, a)
73 })
74 e.GET(adminPath+"/login/oauth/github", func(c echo.Context) error {
75 return handler.StartOAuthGithub(c, a)
76 })
77 e.GET(adminPath+"/login/oauth/callback", func(c echo.Context) error {
78 return handler.OAuthCallback(c, a)
79 })
80 e.GET(adminPath+"/logout", func(c echo.Context) error {
81 return handler.Logout(c, a)
82 })
83}
84
85// bindAdminRoute binds all admin routes
86func bindAdminRoute(e *echo.Echo, a *server.App, adminPath string) {
87 adminGroup := e.Group(adminPath)
88 // Since login is behind the admin path, we cannot use group middleware
89 adminGroup.GET("", func(c echo.Context) error {
90 return c.Redirect(302, adminPath+"/manager")
91 }, middleware.RequireLogin(a))
92
93 adminGroup.GET("/manager", func(c echo.Context) error {
94 return handler.AdminManagerRoute(c, a)
95 }, middleware.RequireLogin(a))
96}
97
98// bindHTMXEndpoints binds all HTMX endpoints
99func bindHTMXEndpoints(e *echo.Echo, a *server.App) {
100 // Create a group for /htmx endpoints
101 htmxGroup := e.Group("/htmx")
102
103 // Apply middleware to the group, so all endpoints are protected
104 htmxGroup.Use(middleware.RequireLogin(a))
105
106 // List all images component
107 htmxGroup.GET("/image-manager", func(c echo.Context) error {
108 return handler.ImageManagerComponent(c, a)
109 })
110 // Delete an image
111 htmxGroup.DELETE("/image-manager/delete/:ID", func(c echo.Context) error {
112 return handler.ImageManagerDelete(c, a)
113 })
114
115 // List all containers
116 htmxGroup.GET("/container-manager", func(c echo.Context) error {
117 return handler.ContainerManagerComponent(c, a)
118 })
119 // Stop a container
120 htmxGroup.POST("/container-manager/stop/:ID", func(c echo.Context) error {
121 return handler.ContainerManagerStop(c, a)
122 })
123 // Delete a container
124 htmxGroup.DELETE("/container-manager/delete/:ID", func(c echo.Context) error {
125 return handler.ContainerManagerDelete(c, a)
126 })
127 // Start a container
128 htmxGroup.POST("/container-manager/start/:ID", func(c echo.Context) error {
129 return handler.ContainerManagerStart(c, a)
130 })
131 // Edit a container view
132 htmxGroup.GET("/container-manager/edit/:ID", func(c echo.Context) error {
133 return handler.ContainerManagerEditGET(c, a)
134 })
135 // Edit a container action
136 htmxGroup.POST("/container-manager/edit/:ID", func(c echo.Context) error {
137 return handler.ContainerManagerEditPOST(c, a)
138 })
139
140 // Display upload-image component
141 htmxGroup.GET("/upload-image", func(c echo.Context) error {
142 return handler.UploadImageGETHandler(c, a)
143 })
144 // Upload image
145 htmxGroup.POST("/upload-image", func(c echo.Context) error {
146 return handler.UploadImagePOSTHandler(c, a)
147 })
148 // Display create-container component
149 htmxGroup.GET("/create-container/:ID", func(c echo.Context) error {
150 return handler.CreateContainerGET(c, a)
151 })
152 // Create container
153 htmxGroup.POST("/create-container/:ID", func(c echo.Context) error {
154 return handler.CreateContainerPOST(c, a)
155 })
156}