From 775f84b6dcb075d21d541e8c29ae358a23dd4569 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 15 Dec 2020 16:41:55 +0000 Subject: [PATCH] Adding feature to allow configured the builder to point at a self hosted system instead of production (for deployment). --- packages/builder/src/builderStore/index.js | 5 +- .../builder/src/builderStore/store/hosting.js | 35 +++++++++++++ .../start/BuilderSettingsButton.svelte | 32 ++++++++++++ .../start/BuilderSettingsModal.svelte | 51 +++++++++++++++++++ packages/builder/src/pages/_layout.svelte | 48 +++++++++-------- .../server/src/api/controllers/hosting.js | 4 +- packages/server/src/api/routes/hosting.js | 4 +- packages/server/src/api/routes/index.js | 2 + 8 files changed, 156 insertions(+), 25 deletions(-) create mode 100644 packages/builder/src/builderStore/store/hosting.js create mode 100644 packages/builder/src/components/start/BuilderSettingsButton.svelte create mode 100644 packages/builder/src/components/start/BuilderSettingsModal.svelte diff --git a/packages/builder/src/builderStore/index.js b/packages/builder/src/builderStore/index.js index 887ef733e4..8b6666f3ab 100644 --- a/packages/builder/src/builderStore/index.js +++ b/packages/builder/src/builderStore/index.js @@ -1,6 +1,8 @@ import { getFrontendStore } from "./store/frontend" import { getBackendUiStore } from "./store/backend" -import { getAutomationStore } from "./store/automation/" +import { getAutomationStore } from "./store/automation" +import { getHostingStore } from "./store/hosting" + import { getThemeStore } from "./store/theme" import { derived } from "svelte/store" import analytics from "analytics" @@ -11,6 +13,7 @@ export const store = getFrontendStore() export const backendUiStore = getBackendUiStore() export const automationStore = getAutomationStore() export const themeStore = getThemeStore() +export const hostingStore = getHostingStore() export const currentAsset = derived(store, $store => { const layout = $store.layouts diff --git a/packages/builder/src/builderStore/store/hosting.js b/packages/builder/src/builderStore/store/hosting.js new file mode 100644 index 0000000000..0e46a42c3b --- /dev/null +++ b/packages/builder/src/builderStore/store/hosting.js @@ -0,0 +1,35 @@ +import { writable } from "svelte/store" +import api from "../api" + +const INITIAL_BACKEND_UI_STATE = { + hostingInfo: {}, + appUrl: "", +} + +export const getHostingStore = () => { + const store = writable({...INITIAL_BACKEND_UI_STATE}) + store.actions = { + fetch: async () => { + const response = await api.get("/api/hosting/") + const info = await response.json() + store.update(state => { + state.hostingInfo = info + return state + }) + return info + }, + save: async hostingInfo => { + const response = await api.post("/api/hosting", hostingInfo) + const revision = (await response.json()).rev + store.update(state => { + state.hostingInfo = { + ...hostingInfo, + _rev: revision, + } + return state + }) + } + } + + return store +} diff --git a/packages/builder/src/components/start/BuilderSettingsButton.svelte b/packages/builder/src/components/start/BuilderSettingsButton.svelte new file mode 100644 index 0000000000..7252ab4ded --- /dev/null +++ b/packages/builder/src/components/start/BuilderSettingsButton.svelte @@ -0,0 +1,32 @@ + + +
+ +
+ + + + + diff --git a/packages/builder/src/components/start/BuilderSettingsModal.svelte b/packages/builder/src/components/start/BuilderSettingsModal.svelte new file mode 100644 index 0000000000..3d58d65e9b --- /dev/null +++ b/packages/builder/src/components/start/BuilderSettingsModal.svelte @@ -0,0 +1,51 @@ + + + +
Hosting
+

This section contains settings that relate to the deployment and hosting of apps made in this builder.

+ + {#if selfhosted} + + + + {/if} +
+ + diff --git a/packages/builder/src/pages/_layout.svelte b/packages/builder/src/pages/_layout.svelte index 3f8b36d8f4..13c6bed988 100644 --- a/packages/builder/src/pages/_layout.svelte +++ b/packages/builder/src/pages/_layout.svelte @@ -7,6 +7,9 @@ CommunityIcon, BugIcon, } from "components/common/Icons" + import BuilderSettingsButton from "components/start/BuilderSettingsButton.svelte" + + let modal
@@ -16,27 +19,30 @@
-
@@ -76,8 +82,10 @@ } .nav-section { - margin: 20px 0px; + margin: 20px 0 0 0; display: flex; flex-direction: column; + justify-content: space-between; + height: 100%; } diff --git a/packages/server/src/api/controllers/hosting.js b/packages/server/src/api/controllers/hosting.js index 4e95405738..f2a2ac61cb 100644 --- a/packages/server/src/api/controllers/hosting.js +++ b/packages/server/src/api/controllers/hosting.js @@ -18,14 +18,14 @@ exports.save = async ctx => { if (type === HostingTypes.CLOUD) { ctx.throw(400, "Cannot update Cloud hosting information") } - await db.put({ + const response = await db.put({ _id: HOSTING_DOC, type, appServerUrl, objectStoreUrl, useHttps, }) - ctx.body = "Hosting information saved successfully" + ctx.body = response } exports.fetch = async ctx => { diff --git a/packages/server/src/api/routes/hosting.js b/packages/server/src/api/routes/hosting.js index b8d4256bcd..783259a514 100644 --- a/packages/server/src/api/routes/hosting.js +++ b/packages/server/src/api/routes/hosting.js @@ -6,8 +6,8 @@ const { BUILDER } = require("../../utilities/security/permissions") const router = Router() router - .fetch("/api/hosting/info", authorized(BUILDER), controller.fetchInfo) - .fetch("/api/hosting", authorized(BUILDER), controller.fetch) + .get("/api/hosting/info", authorized(BUILDER), controller.fetchInfo) + .get("/api/hosting", authorized(BUILDER), controller.fetch) .post("/api/hosting", authorized(BUILDER), controller.save) module.exports = router diff --git a/packages/server/src/api/routes/index.js b/packages/server/src/api/routes/index.js index 2a8669b042..455f0fe989 100644 --- a/packages/server/src/api/routes/index.js +++ b/packages/server/src/api/routes/index.js @@ -17,6 +17,7 @@ const templatesRoutes = require("./templates") const analyticsRoutes = require("./analytics") const routingRoutes = require("./routing") const permissionRoutes = require("./permission") +const hostingRoutes = require("./hosting") exports.mainRoutes = [ deployRoutes, @@ -34,6 +35,7 @@ exports.mainRoutes = [ webhookRoutes, routingRoutes, permissionRoutes, + hostingRoutes, // these need to be handled last as they still use /api/:tableId // this could be breaking as koa may recognise other routes as this tableRoutes,