1
0
mirror of https://github.com/axzilla/templui.git synced 2025-02-21 00:12:48 +00:00

chore: upgrade to Go 1.23.3 and fix MIME types

- Update go.mod to Go 1.23.3
- Add explicit MIME type handling for static files
This commit is contained in:
axzilla 2024-11-28 14:31:41 +07:00
parent 3059248145
commit 0f6c7d8262
2 changed files with 36 additions and 17 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"net/http"
"path/filepath"
"github.com/a-h/templ"
"github.com/axzilla/goilerplate/assets"
@ -49,23 +50,41 @@ func main() {
func SetupAssetsRoutes(mux *http.ServeMux) {
var isDevelopment = config.AppConfig.GoEnv != "production"
// We need this for Templ to work
disableCacheInDevMode := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if isDevelopment {
w.Header().Set("Cache-Control", "no-store")
}
next.ServeHTTP(w, r)
})
mimeTypes := map[string]string{
".css": "text/css; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".svg": "image/svg+xml",
".html": "text/html; charset=utf-8",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".woff": "font/woff",
".woff2": "font/woff2",
".ttf": "font/ttf",
".ico": "image/x-icon",
}
// Serve static files from the assets directory
var fs http.Handler
if isDevelopment {
fs = http.FileServer(http.Dir("./assets"))
} else {
fs = http.FileServer(http.FS(assets.Assets))
}
assetHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ext := filepath.Ext(r.URL.Path)
mux.Handle("GET /assets/*", disableCacheInDevMode(http.StripPrefix("/assets/", fs)))
if mimeType, ok := mimeTypes[ext]; ok {
w.Header().Set("Content-Type", mimeType)
}
if isDevelopment {
w.Header().Set("Cache-Control", "no-store")
}
var fs http.Handler
if isDevelopment {
fs = http.FileServer(http.Dir("./assets"))
} else {
fs = http.FileServer(http.FS(assets.Assets))
}
fs.ServeHTTP(w, r)
})
mux.Handle("GET /assets/", http.StripPrefix("/assets/", assetHandler))
}

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/axzilla/goilerplate
go 1.22.4
go 1.23.3
require (
github.com/Oudwins/tailwind-merge-go v0.2.0