ai cooking
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

move static serve files to a package (#221)

* move static serve files to a package

* move favicon

authored by

Paul Miller and committed by
GitHub
b664b036 10a5dfe3

+55 -42
cmd/careme/favicon.png internal/static/favicon.png
cmd/careme/static/htmx@2.0.8.js internal/static/htmx@2.0.8.js
cmd/careme/static/tailwind.css internal/static/tailwind.css
+2 -40
cmd/careme/web.go
··· 9 9 "careme/internal/logsink" 10 10 "careme/internal/recipes" 11 11 "careme/internal/seasons" 12 + "careme/internal/static" 12 13 "careme/internal/templates" 13 14 "careme/internal/users" 14 15 utypes "careme/internal/users/types" 15 16 "context" 16 - "crypto/sha256" 17 - _ "embed" 18 17 "errors" 19 18 "fmt" 20 19 "html/template" ··· 26 25 "time" 27 26 ) 28 27 29 - //go:embed favicon.png 30 - var favicon []byte 31 - 32 - //go:embed static/tailwind.css 33 - var tailwindCSS []byte 34 - 35 - //go:embed static/htmx@2.0.8.js 36 - var htmx208JS []byte 37 - 38 28 func runServer(cfg *config.Config, logsinkCfg logsink.Config, addr string) error { 39 29 cache, err := cache.MakeCache() 40 30 if err != nil { ··· 48 38 49 39 mux := http.NewServeMux() 50 40 authClient.Register(mux) 41 + static.Register(mux) 51 42 52 43 userStorage := users.NewStorage(cache) 53 44 ··· 56 47 return fmt.Errorf("failed to create recipe generator: %w", err) 57 48 } 58 49 59 - //make filename use hash to force cach invalidation (etag really failed here) 60 - tailwindHash := fmt.Sprintf("%x", sha256.Sum256(tailwindCSS)) 61 - tailwindAssetPath := fmt.Sprintf("/static/tailwind.%s.css", tailwindHash[:12]) 62 - templates.SetTailwindAssetPath(tailwindAssetPath) 63 - mux.HandleFunc(tailwindAssetPath, func(w http.ResponseWriter, r *http.Request) { 64 - w.Header().Set("Content-Type", "text/css; charset=utf-8") 65 - w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") 66 - if _, err := w.Write(tailwindCSS); err != nil { 67 - slog.ErrorContext(r.Context(), "failed to write tailwind css", "error", err) 68 - } 69 - }) 70 - 71 - //intentionally versioned so that we can cache aggressively 72 - mux.HandleFunc("/static/htmx@2.0.8.js", func(w http.ResponseWriter, r *http.Request) { 73 - w.Header().Set("Content-Type", "application/javascript; charset=utf-8") 74 - w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") 75 - if _, err := w.Write(htmx208JS); err != nil { 76 - slog.ErrorContext(r.Context(), "failed to write htmx js", "error", err) 77 - } 78 - }) 79 - 80 50 locationStorage, err := locations.New(cfg) 81 51 if err != nil { 82 52 return fmt.Errorf("failed to create location server: %w", err) ··· 134 104 ro.Add(generator, locationServer) 135 105 136 106 mux.Handle("/ready", ro) 137 - 138 - mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { 139 - w.Header().Set("Content-Type", "image/png") // <= without this, many UAs ignore it 140 - w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") 141 - if _, err := w.Write(favicon); err != nil { 142 - slog.ErrorContext(r.Context(), "failed to write favicon", "error", err) 143 - } 144 - }) 145 107 146 108 server := &http.Server{ 147 109 Addr: addr,
+1 -1
deploy/deploy.yaml
··· 21 21 runAsGroup: 65532 22 22 containers: 23 23 - name: careme 24 - image: ghcr.io/paulgmiller/careme:d838f8a # was 3ef7ea6 24 + image: ghcr.io/paulgmiller/careme:10a5dfe # was 3ef7ea6 25 25 imagePullPolicy: IfNotPresent 26 26 ports: 27 27 - containerPort: 8080
+51
internal/static/static.go
··· 1 + package static 2 + 3 + import ( 4 + "careme/internal/templates" 5 + "crypto/sha256" 6 + _ "embed" 7 + "fmt" 8 + "log/slog" 9 + "net/http" 10 + ) 11 + 12 + //go:embed tailwind.css 13 + var tailwindCSS []byte 14 + 15 + //go:embed htmx@2.0.8.js 16 + var htmx208JS []byte 17 + 18 + //go:embed favicon.png 19 + var favicon []byte 20 + 21 + // Register serves static assets and wires template asset paths. 22 + func Register(mux *http.ServeMux) { 23 + tailwindHash := fmt.Sprintf("%x", sha256.Sum256(tailwindCSS)) 24 + tailwindAssetPath := fmt.Sprintf("/static/tailwind.%s.css", tailwindHash[:12]) 25 + templates.SetTailwindAssetPath(tailwindAssetPath) 26 + 27 + mux.HandleFunc(tailwindAssetPath, func(w http.ResponseWriter, r *http.Request) { 28 + w.Header().Set("Content-Type", "text/css; charset=utf-8") 29 + w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") 30 + if _, err := w.Write(tailwindCSS); err != nil { 31 + slog.ErrorContext(r.Context(), "failed to write tailwind css", "error", err) 32 + } 33 + }) 34 + 35 + // Intentionally versioned so that we can cache aggressively. 36 + mux.HandleFunc("/static/htmx@2.0.8.js", func(w http.ResponseWriter, r *http.Request) { 37 + w.Header().Set("Content-Type", "application/javascript; charset=utf-8") 38 + w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") 39 + if _, err := w.Write(htmx208JS); err != nil { 40 + slog.ErrorContext(r.Context(), "failed to write htmx js", "error", err) 41 + } 42 + }) 43 + 44 + mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { 45 + w.Header().Set("Content-Type", "image/png") 46 + w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") 47 + if _, err := w.Write(favicon); err != nil { 48 + slog.ErrorContext(r.Context(), "failed to write favicon", "error", err) 49 + } 50 + }) 51 + }
+1 -1
tailwind/generate.sh
··· 8 8 -v "$(pwd)":/workspace \ 9 9 -w /workspace/tailwind \ 10 10 careme-tailwind:local \ 11 - sh -c '"$TAILWIND_BIN" -i ./input.css -o ../cmd/careme/static/tailwind.css --minify' 11 + sh -c '"$TAILWIND_BIN" -i ./input.css -o ../internal/static/tailwind.css --minify'