v0.1.13 516c45b

router.go RawHistoryFolder

1package httpserve
2
3import (
4	"github.com/bnema/gordon/internal/httpserve/handlers"
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 handlers.DeviceCodeRequest(c, a)
57	})
58	apiGroup.POST("/device/token", func(c echo.Context) error {
59		return handlers.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 handlers.GetInfos(c, a)
66	})
67	protectedApiGroup.POST("/deploy", func(c echo.Context) error {
68		return handlers.PostDeploy(c, a)
69	})
70	protectedApiGroup.POST("/push", func(c echo.Context) error {
71		return handlers.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 handlers.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 handlers.RenderLoginPage(c, a)
86	})
87	e.GET(adminPath+"/login/oauth/github", func(c echo.Context) error {
88		return handlers.StartOAuthGithub(c, a)
89	})
90	e.GET(adminPath+"/login/oauth/callback", func(c echo.Context) error {
91		return handlers.OAuthCallback(c, a)
92	})
93	e.GET(adminPath+"/logout", func(c echo.Context) error {
94		return handlers.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 handlers.AdminManagerRoute(c, a)
108	}, middleware.RequireLogin(a))
109	adminGroup.GET("/cc/:ID", func(c echo.Context) error {
110		return handlers.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 handlers.ImageManagerComponent(c, a)
125	})
126	// Delete an image
127	htmxGroup.DELETE("/image-manager/delete/:ID", func(c echo.Context) error {
128		return handlers.ImageManagerDelete(c, a)
129	})
130
131	// List all containers
132	htmxGroup.GET("/container-manager", func(c echo.Context) error {
133		return handlers.ContainerManagerComponent(c, a)
134	})
135	// Stop a container
136	htmxGroup.POST("/container-manager/stop/:ID", func(c echo.Context) error {
137		return handlers.ContainerManagerStop(c, a)
138	})
139	// Delete a container
140	htmxGroup.DELETE("/container-manager/delete/:ID", func(c echo.Context) error {
141		return handlers.ContainerManagerDelete(c, a)
142	})
143	// Start a container
144	htmxGroup.POST("/container-manager/start/:ID", func(c echo.Context) error {
145		return handlers.ContainerManagerStart(c, a)
146	})
147	// Edit a container view
148	htmxGroup.GET("/container-manager/edit/:ID", func(c echo.Context) error {
149		return handlers.ContainerManagerEditGET(c, a)
150	})
151	// Edit a container action
152	htmxGroup.POST("/container-manager/edit/:ID", func(c echo.Context) error {
153		return handlers.ContainerManagerEditPOST(c, a)
154	})
155
156	// Display upload-image component
157	htmxGroup.GET("/upload-image", func(c echo.Context) error {
158		return handlers.UploadImageGETHandler(c, a)
159	})
160	// Upload image
161	htmxGroup.POST("/upload-image", func(c echo.Context) error {
162		return handlers.UploadImagePOSTHandler(c, a)
163	})
164	// Display create-container component
165	htmxGroup.GET("/create-container/:ID", func(c echo.Context) error {
166		return handlers.CreateContainerGET(c, a)
167	})
168	// Create container
169	htmxGroup.POST("/create-container/:ID", func(c echo.Context) error {
170		return handlers.CreateContainerPOST(c, a)
171	})
172}

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