-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.go
More file actions
86 lines (70 loc) · 2.12 KB
/
Copy pathstorage.go
File metadata and controls
86 lines (70 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package client
import (
"net/http"
"os"
"path/filepath"
"sync"
"time"
)
// BuildStorage defines the behavior for compiling and serving the WASM client.
type BuildStorage interface {
// Compile performs the compilation.
// For Memory: compiles to buffer.
// For Disk: compiles to disk.
Compile() error
// RegisterRoutes registers the WASM file handler on the mux.
RegisterRoutes(mux *http.ServeMux)
// Name returns the Storage name for logging/debugging
Name() string
}
// MemoryStorage compiles WASM to memory and serves it directly.
type MemoryStorage struct {
Client *WasmClient // Access to config and logger
Mu sync.RWMutex
WasmContent []byte
LastCompile time.Time
}
func (s *MemoryStorage) Name() string {
return "In-Memory"
}
func (s *MemoryStorage) Compile() error {
// Delegate to active builder's CompileToMemory
// Note: activeSizeBuilder is in WasmClient
content, err := s.Client.activeSizeBuilder.CompileToMemory()
if err != nil {
return err
}
s.Mu.Lock()
s.WasmContent = content
s.LastCompile = time.Now()
s.Mu.Unlock()
return nil
}
// DiskStorage compiles WASM to disk and serves the static file.
type DiskStorage struct {
Client *WasmClient
}
func (s *DiskStorage) Name() string {
return "External"
}
func (s *DiskStorage) Compile() error {
// Ensure directory exists
outDir := filepath.Join(s.Client.AppRootDir, s.Client.Config.OutputDir())
if err := os.MkdirAll(outDir, 0755); err != nil {
return err
}
// Use existing CompileProgram which writes to config.OutputDir
return s.Client.activeSizeBuilder.CompileProgram()
}
func (s *DiskStorage) RegisterRoutes(mux *http.ServeMux) {
routePath := s.Client.wasmRoutePath()
result := filepath.Join(s.Client.Config.OutputDir(), s.Client.OutputName+".wasm")
// Note: Config.OutputDir is relative to AppRootDir usually, but ServeFile needs OS path.
// We need absolute path.
absPath := filepath.Join(s.Client.AppRootDir, result)
mux.HandleFunc(routePath, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/wasm")
http.ServeFile(w, r, absPath)
})
s.Client.LogSuccessState("http route:", routePath)
}