diff options
| author | mihna123 <mihailonvojinovic@gmail.com> | 2026-09-04 01:29:39 +0200 |
|---|---|---|
| committer | mihna123 <mihailonvojinovic@gmail.com> | 2026-09-04 01:29:39 +0200 |
| commit | 668c151379ea1d9ff77505d74f751ed83a8af029 (patch) | |
| tree | c19ed88cef013e34aea1ada97baff516cfbe2f73 /internal/server/handlers | |
| parent | 2e227424ba3a5bcc703936c35bcadba8cda0931f (diff) | |
| download | openstore-668c151379ea1d9ff77505d74f751ed83a8af029.tar.gz openstore-668c151379ea1d9ff77505d74f751ed83a8af029.zip | |
web: serve the stylesheet over a css route
Embed the css directory alongside the templates and serve it with a
FileServerFS mounted on GET /css/. The directory name matches the url
prefix, so the file resolves without an fs.Sub and there is no error
path to handle. Adding js later is the same one line.
Cover the route in the handler tests, including that template sources
stay unreachable and that the href the home page emits resolves to a
file that is actually served.
Diffstat (limited to 'internal/server/handlers')
| -rw-r--r-- | internal/server/handlers/handlers.go | 1 | ||||
| -rw-r--r-- | internal/server/handlers/handlers_test.go | 32 |
2 files changed, 33 insertions, 0 deletions
diff --git a/internal/server/handlers/handlers.go b/internal/server/handlers/handlers.go index 958ca30..f39c6d5 100644 --- a/internal/server/handlers/handlers.go +++ b/internal/server/handlers/handlers.go @@ -20,6 +20,7 @@ func Routes(conf *config.Config) *http.ServeMux { } mux := http.NewServeMux() mux.HandleFunc("GET /{$}", handler.home) + mux.Handle("GET /css/", http.FileServerFS(web.Files)) return mux } diff --git a/internal/server/handlers/handlers_test.go b/internal/server/handlers/handlers_test.go index b2ae43e..75d25a2 100644 --- a/internal/server/handlers/handlers_test.go +++ b/internal/server/handlers/handlers_test.go @@ -37,6 +37,9 @@ func TestRoutes(t *testing.T) { want int }{ {"home", http.MethodGet, "/", http.StatusOK}, + {"stylesheet", http.MethodGet, "/css/style.css", http.StatusOK}, + {"missing stylesheet", http.MethodGet, "/css/nope.css", http.StatusNotFound}, + {"templates not served", http.MethodGet, "/templates/index.html", http.StatusNotFound}, {"unknown path", http.MethodGet, "/nonsense", http.StatusNotFound}, {"wrong method on home", http.MethodPost, "/", http.StatusMethodNotAllowed}, } @@ -51,6 +54,35 @@ func TestRoutes(t *testing.T) { } } +func TestStylesheetIsServed(t *testing.T) { + mux := Routes(newTestConfig()) + + rec := get(t, mux, http.MethodGet, "/css/style.css") + if rec.Code != http.StatusOK { + t.Fatalf("got status %d, want 200", rec.Code) + } + + if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/css") { + t.Errorf("got Content-Type %q, want text/css", ct) + } + if rec.Body.Len() == 0 { + t.Error("stylesheet served empty") + } +} + +func TestHomeLinksServedStylesheet(t *testing.T) { + mux := Routes(newTestConfig()) + + body := get(t, mux, http.MethodGet, "/").Body.String() + if !strings.Contains(body, `href="/css/style.css"`) { + t.Fatalf("home does not link the stylesheet, got:\n%s", body) + } + + if code := get(t, mux, http.MethodGet, "/css/style.css").Code; code != http.StatusOK { + t.Errorf("linked stylesheet returned %d, want 200", code) + } +} + func TestHomeRendersInstanceName(t *testing.T) { cfg := newTestConfig() cfg.Instance.Name = "Prodavnica123" |
