summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authormihna123 <mihailonvojinovic@gmail.com>2026-09-04 01:29:39 +0200
committermihna123 <mihailonvojinovic@gmail.com>2026-09-04 01:29:39 +0200
commit668c151379ea1d9ff77505d74f751ed83a8af029 (patch)
treec19ed88cef013e34aea1ada97baff516cfbe2f73 /internal
parent2e227424ba3a5bcc703936c35bcadba8cda0931f (diff)
downloadopenstore-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')
-rw-r--r--internal/server/handlers/handlers.go1
-rw-r--r--internal/server/handlers/handlers_test.go32
-rw-r--r--internal/web/css/style.css4
-rw-r--r--internal/web/web.go2
4 files changed, 38 insertions, 1 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"
diff --git a/internal/web/css/style.css b/internal/web/css/style.css
new file mode 100644
index 0000000..c2805e2
--- /dev/null
+++ b/internal/web/css/style.css
@@ -0,0 +1,4 @@
+body {
+ font-family: sans-serif;
+ line-height: 1.6;
+}
diff --git a/internal/web/web.go b/internal/web/web.go
index 391aa2a..5db188e 100644
--- a/internal/web/web.go
+++ b/internal/web/web.go
@@ -2,5 +2,5 @@ package web
import "embed"
-//go:embed templates
+//go:embed templates css
var Files embed.FS