Skip to content

Commit 14bd4ee

Browse files
committed
hook up cors for the controller/builder get config/data tab working
1 parent 40ada9c commit 14bd4ee

4 files changed

Lines changed: 86 additions & 10 deletions

File tree

frontend/builder/tabs/builder-configdatatab.tsx

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import { BuilderAppPanelModel } from "@/builder/store/builder-apppanel-model";
5+
import { CopyButton } from "@/element/copybutton";
56
import { atoms } from "@/store/global";
67
import { cn } from "@/util/util";
78
import { useAtomValue } from "jotai";
@@ -130,6 +131,18 @@ const BuilderConfigDataTab = memo(() => {
130131
await fetchData();
131132
}, [fetchData]);
132133

134+
const handleCopyConfig = useCallback(() => {
135+
if (state.config) {
136+
navigator.clipboard.writeText(JSON.stringify(state.config, null, 2));
137+
}
138+
}, [state.config]);
139+
140+
const handleCopyData = useCallback(() => {
141+
if (state.data) {
142+
navigator.clipboard.writeText(JSON.stringify(state.data, null, 2));
143+
}
144+
}, [state.data]);
145+
133146
useEffect(() => {
134147
if (isRunning) {
135148
fetchData();
@@ -174,23 +187,29 @@ const BuilderConfigDataTab = memo(() => {
174187
<div className="flex-1 overflow-auto p-4">
175188
<div className="flex flex-col gap-6">
176189
<div className="flex flex-col gap-2">
177-
<h4 className="text-base font-semibold text-primary flex items-center gap-2">
178-
<i className="fa fa-gear" />
179-
Config
180-
</h4>
190+
<div className="flex items-center justify-between">
191+
<h4 className="text-base font-semibold text-primary flex items-center gap-2">
192+
<i className="fa fa-gear" />
193+
Config
194+
</h4>
195+
<CopyButton title="Copy Config" onClick={handleCopyConfig} />
196+
</div>
181197
<div className="bg-panel border border-border rounded-lg p-4 overflow-auto">
182-
<pre className="text-sm text-primary font-mono whitespace-pre">
198+
<pre className="text-xs text-primary font-mono whitespace-pre">
183199
{JSON.stringify(state.config, null, 2)}
184200
</pre>
185201
</div>
186202
</div>
187203
<div className="flex flex-col gap-2">
188-
<h4 className="text-base font-semibold text-primary flex items-center gap-2">
189-
<i className="fa fa-database" />
190-
Data
191-
</h4>
204+
<div className="flex items-center justify-between">
205+
<h4 className="text-base font-semibold text-primary flex items-center gap-2">
206+
<i className="fa fa-database" />
207+
Data
208+
</h4>
209+
<CopyButton title="Copy Data" onClick={handleCopyData} />
210+
</div>
192211
<div className="bg-panel border border-border rounded-lg p-4 overflow-auto">
193-
<pre className="text-sm text-primary font-mono whitespace-pre">
212+
<pre className="text-xs text-primary font-mono whitespace-pre">
194213
{JSON.stringify(state.data, null, 2)}
195214
</pre>
196215
</div>

pkg/blockcontroller/tsunamicontroller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ func runTsunamiAppBinary(ctx context.Context, appBinPath string, appPath string,
292292
cmd := exec.Command(appBinPath)
293293
cmd.Env = append(os.Environ(), "TSUNAMI_CLOSEONSTDIN=1")
294294

295+
if wavebase.IsDevMode() {
296+
cmd.Env = append(cmd.Env, "TSUNAMI_CORS=http://localhost:5173,http://localhost:5174")
297+
}
298+
295299
// Add TsunamiEnv variables if configured
296300
tsunamiEnv := blockMeta.GetMap(waveobj.MetaKey_TsunamiEnv)
297301
for key, value := range tsunamiEnv {

pkg/buildercontroller/buildercontroller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ func (bc *BuilderController) runBuilderApp(ctx context.Context, appId string, ap
324324
cmd := exec.Command(appBinPath)
325325
cmd.Env = append(os.Environ(), "TSUNAMI_CLOSEONSTDIN=1")
326326

327+
if wavebase.IsDevMode() {
328+
cmd.Env = append(cmd.Env, "TSUNAMI_CORS=http://localhost:5173,http://localhost:5174")
329+
}
330+
327331
for key, value := range builderEnv {
328332
cmd.Env = append(cmd.Env, key+"="+value)
329333
}

tsunami/engine/serverhandlers.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@ func setNoCacheHeaders(w http.ResponseWriter) {
5050
w.Header().Set("Expires", "0")
5151
}
5252

53+
func setCORSHeaders(w http.ResponseWriter, r *http.Request) bool {
54+
corsOriginsStr := os.Getenv("TSUNAMI_CORS")
55+
if corsOriginsStr == "" {
56+
return false
57+
}
58+
59+
origin := r.Header.Get("Origin")
60+
if origin == "" {
61+
return false
62+
}
63+
64+
allowedOrigins := strings.Split(corsOriginsStr, ",")
65+
for _, allowedOrigin := range allowedOrigins {
66+
allowedOrigin = strings.TrimSpace(allowedOrigin)
67+
if allowedOrigin == origin {
68+
w.Header().Set("Access-Control-Allow-Origin", origin)
69+
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
70+
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
71+
w.Header().Set("Access-Control-Allow-Credentials", "true")
72+
return true
73+
}
74+
}
75+
return false
76+
}
77+
5378
func (h *httpHandlers) registerHandlers(mux *http.ServeMux, opts handlerOpts) {
5479
mux.HandleFunc("/api/render", h.handleRender)
5580
mux.HandleFunc("/api/updates", h.handleSSE)
@@ -200,8 +225,14 @@ func (h *httpHandlers) handleData(w http.ResponseWriter, r *http.Request) {
200225
}
201226
}()
202227

228+
setCORSHeaders(w, r)
203229
setNoCacheHeaders(w)
204230

231+
if r.Method == http.MethodOptions {
232+
w.WriteHeader(http.StatusOK)
233+
return
234+
}
235+
205236
if r.Method != http.MethodGet {
206237
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
207238
return
@@ -224,8 +255,14 @@ func (h *httpHandlers) handleConfig(w http.ResponseWriter, r *http.Request) {
224255
}
225256
}()
226257

258+
setCORSHeaders(w, r)
227259
setNoCacheHeaders(w)
228260

261+
if r.Method == http.MethodOptions {
262+
w.WriteHeader(http.StatusOK)
263+
return
264+
}
265+
229266
switch r.Method {
230267
case http.MethodGet:
231268
h.handleConfigGet(w, r)
@@ -293,8 +330,14 @@ func (h *httpHandlers) handleSchemas(w http.ResponseWriter, r *http.Request) {
293330
}
294331
}()
295332

333+
setCORSHeaders(w, r)
296334
setNoCacheHeaders(w)
297335

336+
if r.Method == http.MethodOptions {
337+
w.WriteHeader(http.StatusOK)
338+
return
339+
}
340+
298341
if r.Method != http.MethodGet {
299342
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
300343
return
@@ -506,8 +549,14 @@ func (h *httpHandlers) handleManifest(manifestFileBytes []byte) http.HandlerFunc
506549
}
507550
}()
508551

552+
setCORSHeaders(w, r)
509553
setNoCacheHeaders(w)
510554

555+
if r.Method == http.MethodOptions {
556+
w.WriteHeader(http.StatusOK)
557+
return
558+
}
559+
511560
if r.Method != http.MethodGet {
512561
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
513562
return

0 commit comments

Comments
 (0)