Skip to content

Commit 7bdad4b

Browse files
committed
add static handlers as well...
1 parent 0f776ed commit 7bdad4b

2 files changed

Lines changed: 41 additions & 8 deletions

File tree

tsunami/app/serverhandlers.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ func NewHTTPHandlers(client *Client) *HTTPHandlers {
3737
}
3838
}
3939

40-
func (h *HTTPHandlers) RegisterHandlers(mux *http.ServeMux, embeddedFS *embed.FS) {
40+
func (h *HTTPHandlers) RegisterHandlers(mux *http.ServeMux, assetsFS *embed.FS, staticFS *embed.FS) {
4141
mux.HandleFunc("/api/render", h.handleRender)
4242
mux.HandleFunc("/api/updates", h.handleSSE)
4343
mux.HandleFunc("/files/", h.handleAssetsUrl)
44-
44+
45+
// Add handler for static files at /static/ path
46+
if staticFS != nil {
47+
mux.HandleFunc("/static/", h.handleStaticPathFiles(staticFS))
48+
}
49+
4550
// Add fallback handler for embedded static files in production mode
46-
if embeddedFS != nil {
47-
mux.HandleFunc("/", h.handleStaticFiles(embeddedFS))
51+
if assetsFS != nil {
52+
mux.HandleFunc("/", h.handleStaticFiles(assetsFS))
4853
}
4954
}
5055

@@ -238,7 +243,7 @@ func (h *HTTPHandlers) handleSSE(w http.ResponseWriter, r *http.Request) {
238243
func (h *HTTPHandlers) handleStaticFiles(embeddedFS *embed.FS) http.HandlerFunc {
239244
// Create a file server from the embedded FS
240245
fileServer := http.FileServer(http.FS(embeddedFS))
241-
246+
242247
return func(w http.ResponseWriter, r *http.Request) {
243248
defer func() {
244249
panicErr := util.PanicHandler("handleStaticFiles", recover())
@@ -262,3 +267,26 @@ func (h *HTTPHandlers) handleStaticFiles(embeddedFS *embed.FS) http.HandlerFunc
262267
fileServer.ServeHTTP(w, r)
263268
}
264269
}
270+
271+
func (h *HTTPHandlers) handleStaticPathFiles(staticFS *embed.FS) http.HandlerFunc {
272+
// Create a file server from the embedded FS
273+
fileServer := http.FileServer(http.FS(staticFS))
274+
275+
return func(w http.ResponseWriter, r *http.Request) {
276+
defer func() {
277+
panicErr := util.PanicHandler("handleStaticPathFiles", recover())
278+
if panicErr != nil {
279+
http.Error(w, fmt.Sprintf("internal server error: %v", panicErr), http.StatusInternalServerError)
280+
}
281+
}()
282+
283+
// Strip /static/ prefix from the path
284+
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/static")
285+
if r.URL.Path == "" {
286+
r.URL.Path = "/"
287+
}
288+
289+
// Serve the file using Go's file server
290+
fileServer.ServeHTTP(w, r)
291+
}
292+
}

tsunami/app/tsunamiapp.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const TsunamiListenAddrEnvVar = "TSUNAMI_LISTENADDR"
3030
const DefaultListenAddr = "localhost:0"
3131

3232
var assetsFS *embed.FS
33+
var staticFS *embed.FS
3334

3435
type SSEvent struct {
3536
Event string
@@ -135,7 +136,7 @@ func (c *Client) runMainE() error {
135136
if c.SetupFn != nil {
136137
c.SetupFn()
137138
}
138-
err := c.ListenAndServe(context.Background(), assetsFS)
139+
err := c.ListenAndServe(context.Background())
139140
if err != nil {
140141
return err
141142
}
@@ -155,13 +156,13 @@ func (c *Client) RunMain() {
155156
}
156157
}
157158

158-
func (c *Client) ListenAndServe(ctx context.Context, embeddedFS *embed.FS) error {
159+
func (c *Client) ListenAndServe(ctx context.Context) error {
159160
// Create HTTP handlers
160161
handlers := NewHTTPHandlers(c)
161162

162163
// Create a new ServeMux and register handlers
163164
mux := http.NewServeMux()
164-
handlers.RegisterHandlers(mux, embeddedFS)
165+
handlers.RegisterHandlers(mux, assetsFS, staticFS)
165166

166167
// Determine listen address from environment variable or use default
167168
listenAddr := os.Getenv(TsunamiListenAddrEnvVar)
@@ -457,3 +458,7 @@ func (c *Client) RegisterFileHandler(path string, option FileHandlerOption) {
457458
func RegisterAssetsFS(fs embed.FS) {
458459
assetsFS = &fs
459460
}
461+
462+
func RegisterStaticFS(fs embed.FS) {
463+
staticFS = &fs
464+
}

0 commit comments

Comments
 (0)