| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- // Package staticspa — estáticos + SPA fallback (paridad app.js:44-60).
- //
- // - Sirve ficheros existentes del root (STATIC_DIR del env si se define;
- // si no, el dist EMBEBIDO — ver abajo).
- // - index.html se cachea en memoria al arranque; si falta → warning y el
- // fallback devuelve 503 texto.
- // - Fallback GET: /api/* → 404 JSON {error:'not_found'}; /assets/* → 404
- // plano; resto → 200 HTML con el index.html cacheado (SPA fallback).
- //
- // EMBED: go:embed no puede referenciar ../../app/dist (fuera del módulo), así
- // que el binario embebe internal/staticspa/dist. CI debe copiar app/dist a
- // server-go/internal/staticspa/dist antes de `go build` (hasta entonces hay
- // un index.html placeholder). STATIC_DIR siempre tiene prioridad sobre el
- // embed (override para desarrollo/despliegue clásico).
- package staticspa
- import (
- "embed"
- "io"
- "io/fs"
- "log"
- "net/http"
- "os"
- "path"
- "strings"
- )
- //go:embed dist
- var embeddedDist embed.FS
- // Handler sirve los estáticos y el SPA fallback.
- type Handler struct {
- files fs.FS
- indexHTML []byte // nil si no hay index.html
- }
- // New crea el handler. Si staticDir != "" sirve ese directorio del disco
- // (override STATIC_DIR); si no, usa el dist embebido en el binario.
- // Lee index.html en memoria al arranque (warning si falta, como app.js:51).
- func New(staticDir string) *Handler {
- h := &Handler{}
- desc := staticDir
- if staticDir != "" {
- h.files = os.DirFS(staticDir)
- } else {
- sub, err := fs.Sub(embeddedDist, "dist")
- if err != nil {
- sub = embeddedDist
- }
- h.files = sub
- desc = "(embed)"
- }
- if data, err := fs.ReadFile(h.files, "index.html"); err == nil {
- h.indexHTML = data
- } else {
- log.Printf("[netpulse] STATIC_DIR=%s has no index.html (missing \"npm run build\" in app/?)", desc)
- }
- return h
- }
- // LocalesFS expone el FS con los catálogos de idioma del dist
- // ("<lang>/translation.json"), embebidos en el binario. Lo usa el paquete
- // alertlang para traducir las notificaciones push server-side (#888/#889).
- func LocalesFS() fs.FS {
- sub, err := fs.Sub(embeddedDist, "dist/locales")
- if err != nil {
- return embeddedDist
- }
- return sub
- }
- // serveFile sirve el fichero `name` si existe y no es directorio.
- func (h *Handler) serveFile(w http.ResponseWriter, r *http.Request, name string) bool {
- if !fs.ValidPath(name) {
- return false
- }
- f, err := h.files.Open(name)
- if err != nil {
- return false
- }
- defer f.Close()
- st, err := f.Stat()
- if err != nil || st.IsDir() {
- return false
- }
- rs, ok := f.(io.ReadSeeker)
- if !ok {
- return false
- }
- http.ServeContent(w, r, st.Name(), st.ModTime(), rs)
- return true
- }
- // notFoundPlain replica el 404 por defecto de Hono (c.notFound()): cuerpo de
- // texto "404 Not Found", no el "404 page not found\n" de net/http.
- func notFoundPlain(w http.ResponseWriter) {
- w.Header().Set("Content-Type", "text/plain; charset=utf-8")
- w.WriteHeader(http.StatusNotFound)
- _, _ = w.Write([]byte("404 Not Found"))
- }
- // ServeHTTP implementa http.Handler: estático si existe; si no, fallback.
- func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- p := r.URL.Path
- // /api/* nunca llega aquí (el mux lo enruta antes), pero por seguridad:
- if strings.HasPrefix(p, "/api/") {
- w.Header().Set("Content-Type", "application/json")
- w.WriteHeader(http.StatusNotFound)
- _, _ = w.Write([]byte(`{"error":"not_found"}`))
- return
- }
- if r.Method != http.MethodGet && r.Method != http.MethodHead {
- notFoundPlain(w)
- return
- }
- // Estático: sirve el fichero si existe (raíz → index.html).
- name := strings.TrimPrefix(path.Clean("/"+p), "/")
- if name == "" {
- name = "index.html"
- }
- // Cache por tipo de recurso (fix pantalla negra tras update):
- // - /assets/* tiene hash de contenido → inmutable 1 año.
- // - index.html y sw.js SIN no-cache se servían desde la caché del
- // navegador apuntando a hashes que el update acababa de borrar →
- // los JS no cargaban y #root quedaba vacío (pantalla negra hasta
- // refrescar a mano). no-cache obliga a revalidar cada navegación.
- if strings.HasPrefix(p, "/assets/") {
- w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
- } else if name == "index.html" || name == "sw.js" {
- w.Header().Set("Cache-Control", "no-cache")
- }
- if h.serveFile(w, r, name) {
- return
- }
- if h.serveFile(w, r, path.Join(name, "index.html")) {
- return
- }
- // Fallback (GET *):
- if strings.HasPrefix(p, "/assets/") {
- notFoundPlain(w) // 404 plano, sin JSON (cuerpo "404 Not Found" como Hono)
- return
- }
- if h.indexHTML == nil {
- w.Header().Set("Content-Type", "text/plain; charset=utf-8")
- w.WriteHeader(http.StatusServiceUnavailable)
- _, _ = w.Write([]byte("NetPulse: frontend no compilado (cd app && npm run build)"))
- return
- }
- w.Header().Set("Content-Type", "text/html; charset=utf-8")
- w.WriteHeader(http.StatusOK)
- _, _ = w.Write(h.indexHTML)
- }
|