0.1.6 11bec3a

router.go RawHistoryFolder

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}
110
111// bindHTMXEndpoints binds all HTMX endpoints
112func bindHTMXEndpoints(e *echo.Echo, a *server.App) {
113	// Create a  group for /htmx endpoints
114	htmxGroup := e.Group("/htmx")
115
116	// Apply middleware to the group, so all endpoints are protected
117	htmxGroup.Use(middleware.RequireLogin(a))
118
119	// List all images component
120	htmxGroup.GET("/image-manager", func(c echo.Context) error {
121		return handler.ImageManagerComponent(c, a)
122	})
123	// Delete an image
124	htmxGroup.DELETE("/image-manager/delete/:ID", func(c echo.Context) error {
125		return handler.ImageManagerDelete(c, a)
126	})
127
128	// List all containers
129	htmxGroup.GET("/container-manager", func(c echo.Context) error {
130		return handler.ContainerManagerComponent(c, a)
131	})
132	// Stop a container
133	htmxGroup.POST("/container-manager/stop/:ID", func(c echo.Context) error {
134		return handler.ContainerManagerStop(c, a)
135	})
136	// Delete a container
137	htmxGroup.DELETE("/container-manager/delete/:ID", func(c echo.Context) error {
138		return handler.ContainerManagerDelete(c, a)
139	})
140	// Start a container
141	htmxGroup.POST("/container-manager/start/:ID", func(c echo.Context) error {
142		return handler.ContainerManagerStart(c, a)
143	})
144	// Edit a container view
145	htmxGroup.GET("/container-manager/edit/:ID", func(c echo.Context) error {
146		return handler.ContainerManagerEditGET(c, a)
147	})
148	// Edit a container action
149	htmxGroup.POST("/container-manager/edit/:ID", func(c echo.Context) error {
150		return handler.ContainerManagerEditPOST(c, a)
151	})
152
153	// Display upload-image component
154	htmxGroup.GET("/upload-image", func(c echo.Context) error {
155		return handler.UploadImageGETHandler(c, a)
156	})
157	// Upload image
158	htmxGroup.POST("/upload-image", func(c echo.Context) error {
159		return handler.UploadImagePOSTHandler(c, a)
160	})
161	// Display create-container component
162	htmxGroup.GET("/create-container/:ID", func(c echo.Context) error {
163		return handler.CreateContainerGET(c, a)
164	})
165	// Create container
166	htmxGroup.POST("/create-container/:ID", func(c echo.Context) error {
167		return handler.CreateContainerPOST(c, a)
168	})
169}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close