Skip to content

Commit 624746f

Browse files
committed
Fix 404 handler route
1 parent 2b395a9 commit 624746f

File tree

3 files changed

+79
-20
lines changed

3 files changed

+79
-20
lines changed

cmd/cc-backend/server.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ func (s *Server) init() error {
279279
}
280280
}
281281

282-
// Custom 404 handler for unmatched routes
283-
s.router.NotFound(func(rw http.ResponseWriter, r *http.Request) {
282+
// 404 handler for pages and API routes
283+
notFoundHandler := func(rw http.ResponseWriter, r *http.Request) {
284284
if strings.HasPrefix(r.URL.Path, "/api/") || strings.HasPrefix(r.URL.Path, "/userapi/") ||
285285
strings.HasPrefix(r.URL.Path, "/frontend/") || strings.HasPrefix(r.URL.Path, "/config/") {
286286
rw.Header().Set("Content-Type", "application/json")
@@ -291,14 +291,13 @@ func (s *Server) init() error {
291291
})
292292
return
293293
}
294+
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
294295
rw.WriteHeader(http.StatusNotFound)
295-
web.RenderTemplate(rw, "message.tmpl", &web.Page{
296-
Title: "Not Found",
297-
MsgType: "alert-warning",
298-
Message: "The requested page was not found.",
299-
Build: buildInfo,
296+
web.RenderTemplate(rw, "404.tmpl", &web.Page{
297+
Title: "Page Not Found",
298+
Build: buildInfo,
300299
})
301-
})
300+
}
302301

303302
if config.Keys.EmbedStaticFiles {
304303
if i, err := os.Stat("./var/img"); err == nil {
@@ -307,9 +306,26 @@ func (s *Server) init() error {
307306
s.router.Handle("/img/*", http.StripPrefix("/img/", http.FileServer(http.Dir("./var/img"))))
308307
}
309308
}
310-
s.router.Handle("/*", http.StripPrefix("/", web.ServeFiles()))
309+
fileServer := http.StripPrefix("/", web.ServeFiles())
310+
s.router.Handle("/*", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
311+
if web.StaticFileExists(r.URL.Path) {
312+
fileServer.ServeHTTP(rw, r)
313+
return
314+
}
315+
notFoundHandler(rw, r)
316+
}))
311317
} else {
312-
s.router.Handle("/*", http.FileServer(http.Dir(config.Keys.StaticFiles)))
318+
staticDir := http.Dir(config.Keys.StaticFiles)
319+
fileServer := http.FileServer(staticDir)
320+
s.router.Handle("/*", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
321+
f, err := staticDir.Open(r.URL.Path)
322+
if err == nil {
323+
f.Close()
324+
fileServer.ServeHTTP(rw, r)
325+
return
326+
}
327+
notFoundHandler(rw, r)
328+
}))
313329
}
314330

315331
return nil

web/templates/404.tmpl

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
1-
{{template "base.tmpl" .}}
2-
{{define "content"}}
3-
<div class="row">
4-
<div class="col">
5-
<div class="alert alert-error" role="alert">
6-
404: Not found
7-
</div>
8-
</div>
9-
</div>
10-
{{end}}
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
5+
<meta name='viewport' content='width=device-width,initial-scale=1'>
6+
<title>{{.Title}}</title>
7+
<link rel='icon' type='image/png' href='/favicon.png'>
8+
<link rel="stylesheet" href="/bootstrap.min.css">
9+
</head>
10+
<body>
11+
<header>
12+
<nav class="navbar navbar-expand-lg navbar-light fixed-top bg-light">
13+
<div class="container-fluid">
14+
<a class="navbar-brand" href="/">
15+
<img style="height: 30px;" alt="ClusterCockpit Logo" src="/img/logo.png" class="d-inline-block align-top">
16+
</a>
17+
</div>
18+
</nav>
19+
</header>
20+
<main>
21+
<section class="content-section">
22+
<div class="container">
23+
<div class="row justify-content-center">
24+
<div class="col-6 text-center">
25+
<div class="card mt-5">
26+
<div class="card-body p-5">
27+
<h1 class="display-1 text-muted">404</h1>
28+
<h2 class="mb-3">Page Not Found</h2>
29+
<p class="text-muted mb-4">The page you are looking for does not exist or has been moved.</p>
30+
<a href="/" class="btn btn-primary">Back to Home</a>
31+
</div>
32+
</div>
33+
</div>
34+
</div>
35+
</div>
36+
</section>
37+
</main>
38+
</body>
39+
</html>

web/web.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ func ServeFiles() http.Handler {
186186
return http.FileServer(http.FS(publicFiles))
187187
}
188188

189+
// StaticFileExists checks whether a static file exists in the embedded frontend FS.
190+
func StaticFileExists(path string) bool {
191+
path = strings.TrimPrefix(path, "/")
192+
if path == "" {
193+
return false
194+
}
195+
_, err := fs.Stat(frontendFiles, "frontend/public/"+path)
196+
return err == nil
197+
}
198+
189199
//go:embed templates/*
190200
var templateFiles embed.FS
191201

@@ -201,6 +211,10 @@ func init() {
201211
return nil
202212
}
203213

214+
if path == "templates/404.tmpl" {
215+
templates[strings.TrimPrefix(path, "templates/")] = template.Must(template.ParseFS(templateFiles, path))
216+
return nil
217+
}
204218
if path == "templates/login.tmpl" {
205219
if util.CheckFileExists("./var/login.tmpl") {
206220
cclog.Info("overwrite login.tmpl with local file")

0 commit comments

Comments
 (0)