Files
tutor-service/internal/webapp/assets.go
2026-04-26 18:39:09 +09:00

42 lines
854 B
Go

package webapp
import (
"embed"
"io/fs"
"net/http"
"strings"
)
//go:embed static/*
var assets embed.FS
func Handler() http.Handler {
static, err := fs.Sub(assets, "static")
if err != nil {
panic(err)
}
files := http.FileServer(http.FS(static))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
serveIndex(w, r, static)
return
}
if strings.HasPrefix(r.URL.Path, "/assets/") {
http.StripPrefix("/assets/", files).ServeHTTP(w, r)
return
}
http.NotFound(w, r)
})
}
func serveIndex(w http.ResponseWriter, r *http.Request, static fs.FS) {
content, err := fs.ReadFile(static, "index.html")
if err != nil {
http.Error(w, "web app unavailable", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write(content)
}