diff --git a/cmd/backrest/backrest.go b/cmd/backrest/backrest.go index 3a5d7b6a..8ff696d7 100644 --- a/cmd/backrest/backrest.go +++ b/cmd/backrest/backrest.go @@ -158,9 +158,23 @@ func main() { mux.Handle("/metrics", auth.RequireAuthentication(metric.GetRegistry().Handler(), authenticator)) // Serve the HTTP gateway + var handler http.Handler = mux + if version == "unknown" { // dev build, enable CORS for local development + handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Connect-Protocol-Version, Connect-Timeout-Ms, Authorization") + if r.Method == "OPTIONS" { + w.WriteHeader(http.StatusOK) + return + } + mux.ServeHTTP(w, r) + }) + } + server := &http.Server{ Addr: env.BindAddress(), - Handler: h2c.NewHandler(mux, &http2.Server{}), // h2c is HTTP/2 without TLS for grpc-connect support. + Handler: h2c.NewHandler(handler, &http2.Server{}), // h2c is HTTP/2 without TLS for grpc-connect support. } zap.S().Infof("starting web server %v", server.Addr) diff --git a/webui/src/api.ts b/webui/src/api.ts index 7f24dfcc..06666042 100644 --- a/webui/src/api.ts +++ b/webui/src/api.ts @@ -3,6 +3,7 @@ import { createConnectTransport } from "@connectrpc/connect-web"; import { createClient } from "@connectrpc/connect"; import { Authentication } from "../gen/ts/v1/authentication_pb"; import { Backrest } from "../gen/ts/v1/service_pb"; +import { backendUrl } from "./state/buildcfg"; const tokenKey = "backrest-ui-authToken"; @@ -24,7 +25,7 @@ const fetch = ( }; const transport = createConnectTransport({ - baseUrl: "./", + baseUrl: backendUrl, useBinaryFormat: true, fetch: fetch as typeof globalThis.fetch, }); diff --git a/webui/src/state/buildcfg.ts b/webui/src/state/buildcfg.ts index e754efe7..51a21a70 100644 --- a/webui/src/state/buildcfg.ts +++ b/webui/src/state/buildcfg.ts @@ -4,4 +4,6 @@ export const uiBuildVersion = ( process.env.BACKREST_BUILD_VERSION || "dev-snapshot-build" ).trim(); export const isDevBuild = uiBuildVersion === "dev-snapshot-build"; -export const pathSeparator = isWindows ? "\\" : "/"; \ No newline at end of file +export const pathSeparator = isWindows ? "\\" : "/"; +export const backendUrl = process.env.UI_BACKEND_URL || "./"; +console.log(`UI OS: ${uios}, Build Version: ${uiBuildVersion}, Backend URL: ${backendUrl}`); \ No newline at end of file