62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.abbiamoundominio.org/blallo/broadcast/ui"
|
|
"git.sr.ht/~blallo/logz/interface"
|
|
)
|
|
|
|
type assetsHandler struct {
|
|
logger logz.Logger
|
|
fsHandler http.Handler
|
|
}
|
|
|
|
func (h *assetsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
h.logger.Info(map[string]any{
|
|
"msg": "Method not allowed",
|
|
"context": "assets",
|
|
})
|
|
failure(w, http.StatusMethodNotAllowed, map[string]any{
|
|
"status": "failed",
|
|
"err": "method not allowed",
|
|
})
|
|
}
|
|
|
|
h.logger.Debug(map[string]any{
|
|
"msg": "request path",
|
|
"url": r.URL,
|
|
})
|
|
|
|
switch {
|
|
case strings.HasSuffix(r.URL.Path, "html"), r.URL.Path == "/":
|
|
w.Header().Set("content-type", "text/html")
|
|
case strings.HasSuffix(r.URL.Path, "js"):
|
|
w.Header().Set("content-type", "text/javascript")
|
|
case strings.HasSuffix(r.URL.Path, "css"):
|
|
w.Header().Set("content-type", "text/css")
|
|
case strings.HasSuffix(r.URL.Path, "json"):
|
|
w.Header().Set("content-type", "application/json")
|
|
case strings.HasSuffix(r.URL.Path, "png"), strings.HasSuffix(r.URL.Path, "ico"):
|
|
w.Header().Set("content-type", "image/png")
|
|
}
|
|
|
|
h.fsHandler.ServeHTTP(w, r)
|
|
}
|
|
|
|
func newAssetsHandler(logger logz.Logger) (*assetsHandler, error) {
|
|
relAssetsFS, err := fs.Sub(ui.AssetsFS, "build")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
assets := http.FileServer(http.FS(relAssetsFS))
|
|
|
|
return &assetsHandler{
|
|
logger: logger,
|
|
fsHandler: assets,
|
|
}, nil
|
|
}
|