From 7304e6b67213bda170c321a4bfdfa85c9d62d7ec Mon Sep 17 00:00:00 2001 From: Adam Shiervani Date: Tue, 25 Feb 2025 16:10:46 +0100 Subject: [PATCH] feat(cloud): Add custom cloud API URL configuration support (#181) * feat(cloud): Add custom cloud API URL configuration support - Implement RPC methods to set, get, and reset cloud URL - Update cloud registration to remove hardcoded cloud API URL - Modify UI to allow configuring custom cloud API URL in developer settings - Remove environment-specific cloud configuration files - Simplify cloud URL configuration in UI config * fix(ui): Update cloud app URL to production environment in device mode * refactor(ui): Remove SIGNAL_API env & Rename to DEVICE_API to make clear distinction between CLOUD_API and DEVICE_API. * feat(ui): Only show Cloud API URL Change on device mode * fix(cloud): Don't override the CloudURL on deregistration from the cloud. --- cloud.go | 3 +- jsonrpc.go | 30 ++++++ ui/.env.cloud-development | 4 + ui/.env.cloud-production | 4 + ui/.env.cloud-staging | 4 + ui/.env.development | 6 -- ui/.env.device | 8 +- ui/.env.production | 6 -- ui/.env.staging | 4 - ui/index.html | 1 - ui/package.json | 6 +- ui/src/components/Header.tsx | 4 +- ui/src/components/MountMediaDialog.tsx | 4 +- ui/src/components/sidebar/settings.tsx | 126 +++++++++++++++++++---- ui/src/routes/adopt.tsx | 16 ++- ui/src/routes/devices.$id.tsx | 16 +-- ui/src/routes/login-local.tsx | 8 +- ui/src/routes/welcome-local.mode.tsx | 6 +- ui/src/routes/welcome-local.password.tsx | 6 +- ui/src/routes/welcome-local.tsx | 4 +- ui/src/ui.config.ts | 26 +---- ui/vite.config.ts | 22 ++-- 22 files changed, 201 insertions(+), 113 deletions(-) create mode 100644 ui/.env.cloud-development create mode 100644 ui/.env.cloud-production create mode 100644 ui/.env.cloud-staging delete mode 100644 ui/.env.development delete mode 100644 ui/.env.production delete mode 100644 ui/.env.staging diff --git a/cloud.go b/cloud.go index fc41a36..5cf00c2 100644 --- a/cloud.go +++ b/cloud.go @@ -96,7 +96,6 @@ func handleCloudRegister(c *gin.Context) { } config.CloudToken = tokenResp.SecretToken - config.CloudURL = req.CloudAPI provider, err := oidc.NewProvider(c, "https://accounts.google.com") if err != nil { @@ -298,8 +297,8 @@ func rpcDeregisterDevice() error { // (e.g., wrong cloud token, already deregistered). Regardless of the reason, we can safely remove it. if resp.StatusCode == http.StatusNotFound || (resp.StatusCode >= 200 && resp.StatusCode < 300) { config.CloudToken = "" - config.CloudURL = "" config.GoogleIdentity = "" + if err := SaveConfig(); err != nil { return fmt.Errorf("failed to save configuration after deregistering: %w", err) } diff --git a/jsonrpc.go b/jsonrpc.go index 619e561..1de55b2 100644 --- a/jsonrpc.go +++ b/jsonrpc.go @@ -730,6 +730,33 @@ func rpcSetSerialSettings(settings SerialSettings) error { return nil } +func rpcSetCloudUrl(url string) error { + if url == "" { + // Reset to default by removing from config + config.CloudURL = defaultConfig.CloudURL + } else { + config.CloudURL = url + } + + if err := SaveConfig(); err != nil { + return fmt.Errorf("failed to save config: %w", err) + } + return nil +} + +func rpcGetCloudUrl() (string, error) { + return config.CloudURL, nil +} + +func rpcResetCloudUrl() error { + // Reset to default by removing from config + config.CloudURL = defaultConfig.CloudURL + if err := SaveConfig(); err != nil { + return fmt.Errorf("failed to reset cloud URL: %w", err) + } + return nil +} + var rpcHandlers = map[string]RPCHandler{ "ping": {Func: rpcPing}, "getDeviceID": {Func: rpcGetDeviceID}, @@ -786,4 +813,7 @@ var rpcHandlers = map[string]RPCHandler{ "setATXPowerAction": {Func: rpcSetATXPowerAction, Params: []string{"action"}}, "getSerialSettings": {Func: rpcGetSerialSettings}, "setSerialSettings": {Func: rpcSetSerialSettings, Params: []string{"settings"}}, + "setCloudUrl": {Func: rpcSetCloudUrl, Params: []string{"url"}}, + "getCloudUrl": {Func: rpcGetCloudUrl}, + "resetCloudUrl": {Func: rpcResetCloudUrl}, } diff --git a/ui/.env.cloud-development b/ui/.env.cloud-development new file mode 100644 index 0000000..471e280 --- /dev/null +++ b/ui/.env.cloud-development @@ -0,0 +1,4 @@ +# No need for VITE_CLOUD_APP it's only needed for the device build + +# We use this for all the cloud API requests from the browser +VITE_CLOUD_API=http://localhost:3000 diff --git a/ui/.env.cloud-production b/ui/.env.cloud-production new file mode 100644 index 0000000..d9895d2 --- /dev/null +++ b/ui/.env.cloud-production @@ -0,0 +1,4 @@ +# No need for VITE_CLOUD_APP it's only needed for the device build + +# We use this for all the cloud API requests from the browser +VITE_CLOUD_API=https://api.jetkvm.com diff --git a/ui/.env.cloud-staging b/ui/.env.cloud-staging new file mode 100644 index 0000000..bc5c14c --- /dev/null +++ b/ui/.env.cloud-staging @@ -0,0 +1,4 @@ +# No need for VITE_CLOUD_APP it's only needed for the device build + +# We use this for all the cloud API requests from the browser +VITE_CLOUD_API=https://staging-api.jetkvm.com diff --git a/ui/.env.development b/ui/.env.development deleted file mode 100644 index 172328c..0000000 --- a/ui/.env.development +++ /dev/null @@ -1,6 +0,0 @@ -VITE_SIGNAL_API=http://localhost:3000 - -VITE_CLOUD_APP=http://localhost:5173 -VITE_CLOUD_API=http://localhost:3000 - -VITE_JETKVM_HEAD= \ No newline at end of file diff --git a/ui/.env.device b/ui/.env.device index 2aaa6a7..252fac4 100644 --- a/ui/.env.device +++ b/ui/.env.device @@ -1,6 +1,2 @@ -VITE_SIGNAL_API= # Uses the KVM device's IP address as the signal API endpoint - -VITE_CLOUD_APP=https://app.jetkvm.com -VITE_CLOUD_API=https://api.jetkvm.com - -VITE_JETKVM_HEAD= \ No newline at end of file +# Used in settings page to know where to link to when user wants to adopt a device to the cloud +VITE_CLOUD_APP=http://localhost:5173 diff --git a/ui/.env.production b/ui/.env.production deleted file mode 100644 index 2587c0c..0000000 --- a/ui/.env.production +++ /dev/null @@ -1,6 +0,0 @@ -VITE_SIGNAL_API=https://api.jetkvm.com - -VITE_CLOUD_APP=https://app.jetkvm.com -VITE_CLOUD_API=https://api.jetkvm.com - -VITE_JETKVM_HEAD= \ No newline at end of file diff --git a/ui/.env.staging b/ui/.env.staging deleted file mode 100644 index 651e5bc..0000000 --- a/ui/.env.staging +++ /dev/null @@ -1,4 +0,0 @@ -VITE_SIGNAL_API=https://staging-api.jetkvm.com - -VITE_CLOUD_APP=https://staging-app.jetkvm.com -VITE_CLOUD_API=https://staging-api.jetkvm.com \ No newline at end of file diff --git a/ui/index.html b/ui/index.html index 72d2594..af9bdfb 100644 --- a/ui/index.html +++ b/ui/index.html @@ -28,7 +28,6 @@ JetKVM - %VITE_JETKVM_HEAD%