Skip to content

Commit 0f776ed

Browse files
committed
deal with static files (for prod builds)
1 parent e29e47f commit 0f776ed

9 files changed

Lines changed: 54 additions & 7 deletions

File tree

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ tasks:
441441

442442
tsunami:demo:todo:
443443
desc: Run the tsunami todo demo application
444-
cmd: go run demo/todo/main-todo.go
444+
cmd: go run demo/todo/*.go
445445
dir: tsunami
446446
env:
447447
TSUNAMI_LISTENADDR: "localhost:12026"

public/logos/wave-logo-256.png

9.56 KB
Loading

tsunami/app/serverhandlers.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
package app
55

66
import (
7+
"embed"
78
"encoding/json"
89
"fmt"
910
"io"
1011
"log"
12+
"mime"
1113
"net/http"
1214
"strings"
1315
"sync"
@@ -19,6 +21,11 @@ import (
1921

2022
const SSEKeepAliveDuration = 5 * time.Second
2123

24+
func init() {
25+
// Add explicit mapping for .json files
26+
mime.AddExtensionType(".json", "application/json")
27+
}
28+
2229
type HTTPHandlers struct {
2330
Client *Client
2431
renderLock sync.Mutex
@@ -30,10 +37,15 @@ func NewHTTPHandlers(client *Client) *HTTPHandlers {
3037
}
3138
}
3239

33-
func (h *HTTPHandlers) RegisterHandlers(mux *http.ServeMux) {
40+
func (h *HTTPHandlers) RegisterHandlers(mux *http.ServeMux, embeddedFS *embed.FS) {
3441
mux.HandleFunc("/api/render", h.handleRender)
3542
mux.HandleFunc("/api/updates", h.handleSSE)
36-
mux.HandleFunc("/assets/", h.handleAssetsUrl)
43+
mux.HandleFunc("/files/", h.handleAssetsUrl)
44+
45+
// Add fallback handler for embedded static files in production mode
46+
if embeddedFS != nil {
47+
mux.HandleFunc("/", h.handleStaticFiles(embeddedFS))
48+
}
3749
}
3850

3951
func (h *HTTPHandlers) handleRender(w http.ResponseWriter, r *http.Request) {
@@ -222,3 +234,31 @@ func (h *HTTPHandlers) handleSSE(w http.ResponseWriter, r *http.Request) {
222234
}
223235
}
224236
}
237+
238+
func (h *HTTPHandlers) handleStaticFiles(embeddedFS *embed.FS) http.HandlerFunc {
239+
// Create a file server from the embedded FS
240+
fileServer := http.FileServer(http.FS(embeddedFS))
241+
242+
return func(w http.ResponseWriter, r *http.Request) {
243+
defer func() {
244+
panicErr := util.PanicHandler("handleStaticFiles", recover())
245+
if panicErr != nil {
246+
http.Error(w, fmt.Sprintf("internal server error: %v", panicErr), http.StatusInternalServerError)
247+
}
248+
}()
249+
250+
// Skip if this is an API or files request (already handled by other handlers)
251+
if strings.HasPrefix(r.URL.Path, "/api/") || strings.HasPrefix(r.URL.Path, "/files/") {
252+
http.NotFound(w, r)
253+
return
254+
}
255+
256+
// Handle root "/" => "/index.html"
257+
if r.URL.Path == "/" {
258+
r.URL.Path = "/index.html"
259+
}
260+
261+
// Serve the file using Go's file server
262+
fileServer.ServeHTTP(w, r)
263+
}
264+
}

tsunami/app/tsunamiapp.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package app
55

66
import (
77
"context"
8+
"embed"
89
"fmt"
910
"io"
1011
"io/fs"
@@ -28,6 +29,8 @@ import (
2829
const TsunamiListenAddrEnvVar = "TSUNAMI_LISTENADDR"
2930
const DefaultListenAddr = "localhost:0"
3031

32+
var assetsFS *embed.FS
33+
3134
type SSEvent struct {
3235
Event string
3336
Data []byte
@@ -132,7 +135,7 @@ func (c *Client) runMainE() error {
132135
if c.SetupFn != nil {
133136
c.SetupFn()
134137
}
135-
err := c.ListenAndServe(context.Background())
138+
err := c.ListenAndServe(context.Background(), assetsFS)
136139
if err != nil {
137140
return err
138141
}
@@ -152,13 +155,13 @@ func (c *Client) RunMain() {
152155
}
153156
}
154157

155-
func (c *Client) ListenAndServe(ctx context.Context) error {
158+
func (c *Client) ListenAndServe(ctx context.Context, embeddedFS *embed.FS) error {
156159
// Create HTTP handlers
157160
handlers := NewHTTPHandlers(c)
158161

159162
// Create a new ServeMux and register handlers
160163
mux := http.NewServeMux()
161-
handlers.RegisterHandlers(mux)
164+
handlers.RegisterHandlers(mux, embeddedFS)
162165

163166
// Determine listen address from environment variable or use default
164167
listenAddr := os.Getenv(TsunamiListenAddrEnvVar)
@@ -450,3 +453,7 @@ func (c *Client) RegisterFileHandler(path string, option FileHandlerOption) {
450453
}
451454
})
452455
}
456+
457+
func RegisterAssetsFS(fs embed.FS) {
458+
assetsFS = &fs
459+
}
105 KB
Binary file not shown.
104 KB
Binary file not shown.
337 KB
Binary file not shown.
9.56 KB
Loading

tsunami/frontend/src/vdom.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ function VDomInnerView({ model }: VDomViewProps) {
432432
return (
433433
<>
434434
{model.backendOpts?.globalstyles ? (
435-
<WaveStyle src="/assets/global.css" model={model} onMount={handleStylesMounted} />
435+
<WaveStyle src="/files/global.css" model={model} onMount={handleStylesMounted} />
436436
) : null}
437437
{styleMounted ? <VDomRoot model={model} /> : null}
438438
</>

0 commit comments

Comments
 (0)