2025-11-03 22:13:11 +00:00
|
|
|
package main
|
2025-11-06 22:31:51 +00:00
|
|
|
|
2025-11-03 22:13:11 +00:00
|
|
|
import (
|
|
|
|
|
"embed"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/fs"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//go:embed static
|
|
|
|
|
var embeddedStaticFS embed.FS
|
|
|
|
|
|
|
|
|
|
// FileServer conveniently sets up a http.FileServer handler to serve
|
|
|
|
|
// static files from a http.FileSystem.
|
|
|
|
|
func FileServer(r chi.Router, path string, root http.FileSystem, embeddedFS embed.FS, embeddedPath string) {
|
2025-11-06 22:31:51 +00:00
|
|
|
if strings.ContainsAny(path, "{}*") {
|
|
|
|
|
panic("FileServer does not permit any URL parameters.")
|
|
|
|
|
}
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
if path != "/" && path[len(path)-1] != '/' {
|
|
|
|
|
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
|
|
|
|
|
path += "/"
|
|
|
|
|
}
|
|
|
|
|
path += "*"
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
rctx := chi.RouteContext(r.Context())
|
|
|
|
|
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
// Determine the actual file path
|
|
|
|
|
requestedPath := strings.TrimPrefix(r.URL.Path, pathPrefix)
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
// Try to open from local filesystem first for development
|
|
|
|
|
localFile, localErr := root.Open(requestedPath)
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
var fileToServe http.File
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
if localErr == nil {
|
|
|
|
|
// File found in local filesystem
|
|
|
|
|
fileToServe = localFile
|
|
|
|
|
} else {
|
|
|
|
|
// If not found locall, try embedded filesystem
|
|
|
|
|
embeddedFilePath := filepath.Join(embeddedPath, requestedPath)
|
|
|
|
|
embeddedFile, err := embeddedFS.Open(embeddedFilePath)
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
// Wrap the embedded file to implement http.File interface
|
|
|
|
|
fileToServe = &embeddedFileWrapper{embeddedFile}
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
}
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
// Create a custom ResponseWriter that allows us to modify headers
|
|
|
|
|
crw := &customResponseWriter{ResponseWriter: w}
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
// Serve the file
|
|
|
|
|
http.ServeContent(crw, r, requestedPath, time.Time{}, fileToServe)
|
2025-11-03 22:13:11 +00:00
|
|
|
|
2025-11-06 22:31:51 +00:00
|
|
|
// Close the file
|
|
|
|
|
fileToServe.Close()
|
|
|
|
|
})
|
2025-11-03 22:13:11 +00:00
|
|
|
}
|
2025-11-06 22:31:51 +00:00
|
|
|
|
2025-11-03 22:13:11 +00:00
|
|
|
type embeddedFileWrapper struct {
|
2025-11-06 22:31:51 +00:00
|
|
|
file fs.File
|
2025-11-03 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *embeddedFileWrapper) Close() error {
|
2025-11-06 22:31:51 +00:00
|
|
|
return e.file.Close()
|
2025-11-03 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *embeddedFileWrapper) Read(p []byte) (n int, err error) {
|
2025-11-06 22:31:51 +00:00
|
|
|
return e.file.Read(p)
|
2025-11-03 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Seeker interface {
|
2025-11-06 22:31:51 +00:00
|
|
|
Seek(offset int64, whence int) (int64, error)
|
2025-11-03 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *embeddedFileWrapper) Seek(offset int64, whence int) (int64, error) {
|
2025-11-06 22:31:51 +00:00
|
|
|
if seeker, ok := e.file.(Seeker); ok {
|
|
|
|
|
return seeker.Seek(offset, whence)
|
|
|
|
|
}
|
|
|
|
|
return 0, fmt.Errorf("Seek not supported")
|
2025-11-03 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *embeddedFileWrapper) Readdir(count int) ([]os.FileInfo, error) {
|
2025-11-06 22:31:51 +00:00
|
|
|
// This is a bit tricky with embedded files
|
|
|
|
|
if dirFile, ok := e.file.(fs.ReadDirFile); ok {
|
|
|
|
|
entries, err := dirFile.ReadDir(count)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileInfos := make([]os.FileInfo, len(entries))
|
|
|
|
|
for i, entry := range entries {
|
|
|
|
|
fileInfos[i], err = entry.Info()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fileInfos, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("Readdir not supported")
|
2025-11-03 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *embeddedFileWrapper) Stat() (os.FileInfo, error) {
|
2025-11-06 22:31:51 +00:00
|
|
|
return e.file.Stat()
|
2025-11-03 22:13:11 +00:00
|
|
|
}
|