shrink
This commit is contained in:
parent
84ef53ae05
commit
21471c69ba
6 changed files with 179 additions and 29 deletions
135
main.go
135
main.go
|
@ -14,6 +14,8 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"path"
|
||||
|
||||
"github.com/google/uuid"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
@ -486,6 +488,114 @@ func deleteItem(uuid string, id int) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// ==== Static File Server with Compression Support ====
|
||||
|
||||
// compressionFileServer serves static files with compression support
|
||||
func compressionFileServer(fsys fs.FS) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Check if this is a UUID path (starts with / followed by 36 characters)
|
||||
isUUIDPath := len(r.URL.Path) == 37 && r.URL.Path[0] == '/' &&
|
||||
!strings.Contains(r.URL.Path[1:], "/")
|
||||
|
||||
if r.URL.Path == "/" || isUUIDPath {
|
||||
// Serve index.html at root or for routes with /{uuid}
|
||||
content, err := fs.ReadFile(fsys, "frontend/dist/index.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Not found", 404)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
w.Write(content)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the requested file path relative to frontend/dist
|
||||
filePath := strings.TrimPrefix(r.URL.Path, "/")
|
||||
if filePath == "" {
|
||||
filePath = "index.html"
|
||||
}
|
||||
fullPath := "frontend/dist/" + filePath
|
||||
|
||||
// Check if client accepts compression
|
||||
acceptEncoding := r.Header.Get("Accept-Encoding")
|
||||
acceptsGzip := strings.Contains(acceptEncoding, "gzip")
|
||||
acceptsBrotli := strings.Contains(acceptEncoding, "br")
|
||||
|
||||
// Try to serve compressed version if client supports it
|
||||
if acceptsBrotli {
|
||||
if compressedContent, err := fs.ReadFile(fsys, fullPath+".br"); err == nil {
|
||||
w.Header().Set("Content-Encoding", "br")
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(compressedContent)))
|
||||
w.Header().Set("Vary", "Accept-Encoding")
|
||||
// Set appropriate content type based on file extension
|
||||
setContentType(w, filePath)
|
||||
w.Write(compressedContent)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if acceptsGzip {
|
||||
if compressedContent, err := fs.ReadFile(fsys, fullPath+".gz"); err == nil {
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(compressedContent)))
|
||||
w.Header().Set("Vary", "Accept-Encoding")
|
||||
// Set appropriate content type based on file extension
|
||||
setContentType(w, filePath)
|
||||
w.Write(compressedContent)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to uncompressed file
|
||||
if content, err := fs.ReadFile(fsys, fullPath); err == nil {
|
||||
w.Header().Set("Vary", "Accept-Encoding")
|
||||
// Set appropriate content type based on file extension
|
||||
setContentType(w, filePath)
|
||||
w.Write(content)
|
||||
return
|
||||
}
|
||||
|
||||
// File not found
|
||||
http.NotFound(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// setContentType sets the appropriate Content-Type header based on file extension
|
||||
func setContentType(w http.ResponseWriter, filePath string) {
|
||||
ext := strings.ToLower(path.Ext(filePath))
|
||||
switch ext {
|
||||
case ".html":
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
case ".css":
|
||||
w.Header().Set("Content-Type", "text/css; charset=utf-8")
|
||||
case ".js":
|
||||
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
|
||||
case ".json":
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
case ".svg":
|
||||
w.Header().Set("Content-Type", "image/svg+xml")
|
||||
case ".png":
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
case ".jpg", ".jpeg":
|
||||
w.Header().Set("Content-Type", "image/jpeg")
|
||||
case ".gif":
|
||||
w.Header().Set("Content-Type", "image/gif")
|
||||
case ".ico":
|
||||
w.Header().Set("Content-Type", "image/x-icon")
|
||||
case ".woff":
|
||||
w.Header().Set("Content-Type", "font/woff")
|
||||
case ".woff2":
|
||||
w.Header().Set("Content-Type", "font/woff2")
|
||||
case ".ttf":
|
||||
w.Header().Set("Content-Type", "font/ttf")
|
||||
case ".eot":
|
||||
w.Header().Set("Content-Type", "application/vnd.ms-fontobject")
|
||||
default:
|
||||
// Default to octet-stream for unknown types
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
}
|
||||
}
|
||||
|
||||
// ==== SSE Broadcast Logic ====
|
||||
|
||||
func broadcast(uuid string, msg interface{}) {
|
||||
|
@ -894,30 +1004,7 @@ func main() {
|
|||
})
|
||||
|
||||
// Serve static files from embedded filesystem (register last)
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Check if this is a UUID path (starts with / followed by 36 characters)
|
||||
isUUIDPath := len(r.URL.Path) == 37 && r.URL.Path[0] == '/' &&
|
||||
!strings.Contains(r.URL.Path[1:], "/")
|
||||
|
||||
if r.URL.Path == "/" || isUUIDPath {
|
||||
// Serve index.html at root or for routes with /{uuid}
|
||||
content, err := staticFiles.ReadFile("frontend/dist/index.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Not found", 404)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
w.Write(content)
|
||||
} else {
|
||||
// Serve other static files from the static/ subdirectory
|
||||
subFS, err := fs.Sub(staticFiles, "frontend/dist")
|
||||
if err != nil {
|
||||
http.Error(w, "Not found", 404)
|
||||
return
|
||||
}
|
||||
http.StripPrefix("/", http.FileServer(http.FS(subFS))).ServeHTTP(w, r)
|
||||
}
|
||||
})
|
||||
http.Handle("/", compressionFileServer(staticFiles))
|
||||
|
||||
port := strings.TrimSpace(os.Getenv("PORT"))
|
||||
if port == "" {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue