Browse Source

refactoring routes to serve app UIs

gh-pages
michael shanks 7 years ago
parent
commit
921b31d595
  1. 1
      .gitignore
  2. 35
      et 04eef6ab4d291621114949c14097987ec1804274q
  3. 1
      lerna.json
  4. 26
      packages/server/appPackages/master/appDefinition.json
  5. BIN
      packages/server/appPackages/testApp/dist/package.tar.gz
  6. 52
      packages/server/middleware/routers.js
  7. 6
      packages/server/package.json
  8. 30
      packages/server/utilities/createAppPackage.js
  9. 43
      packages/server/utilities/masterAppInternal.js

1
.gitignore

@ -1,5 +1,6 @@
.data/ .data/
.temp/ .temp/
packages/server/runtime_apps/
# Logs # Logs
logs logs

35
et 04eef6ab4d291621114949c14097987ec1804274q

@ -1,35 +0,0 @@
commit 78a241d11c55ae717ba844c3d853e594d805e0a2 (HEAD -> master)
Author: michael shanks <mike@budibase.com>
Date: Fri Jun 7 14:11:23 2019 +0100
mono-repo
commit 04eef6ab4d291621114949c14097987ec1804274 (bb/master)
Author: michael shanks <mike@budibase.com>
Date: Fri Jun 7 12:22:23 2019 +0100
license AGPL
commit 5bcf5157e0010aea34be4bc338057a75ae0befec
Author: michael shanks <mike@budibase.com>
Date: Fri Jun 7 12:09:06 2019 +0100
making OS friendly :)
commit d5129fbda3c0fbb35e7275420aee0b2e8a4f6c2b
Author: michael shanks <mike@budibase.com>
Date: Fri Jun 7 12:02:48 2019 +0100
initialise master database working
commit 36f9e7c64faf9c1ce27a1586337b4bd168fe88ec (origin/master, origin/HEAD)
Author: Michael Shanks <mjashanks@hotmail.com>
Date: Fri Apr 5 16:30:29 2019 +0100
backup..
commit a8aa18d01d0640db6fb0f7bcb20e713b38b5610b
Author: Michael Shanks <mjashanks@hotmail.com>
Date: Tue Mar 19 21:45:21 2019 +0000
initial commit

1
lerna.json

@ -1,4 +1,5 @@
{ {
"npmClient": "npm",
"packages": [ "packages": [
"packages/*" "packages/*"
], ],

26
packages/server/appPackages/master/appDefinition.json

@ -45,6 +45,20 @@
"label": "Resolve Application By", "label": "Resolve Application By",
"getInitialValue": "default", "getInitialValue": "default",
"getUndefinedValue": "default" "getUndefinedValue": "default"
},
{
"name": "defaultVersion",
"type": "reference",
"typeOptions": {
"indexNodeKey": "/applications/1-{id}/all_versions",
"reverseIndexNodeKeys": [
"/applications/1-{id}/versions/3-{id}/isdefault"
],
"displayValue": "name"
},
"label": "Default Version",
"getInitialValue": "default",
"getUndefinedValue": "default"
} }
], ],
"children": [ "children": [
@ -237,6 +251,18 @@
"aggregateGroups": [], "aggregateGroups": [],
"allowedRecordNodeIds": [], "allowedRecordNodeIds": [],
"nodeId": 10 "nodeId": 10
},
{
"name": "isdefault",
"type": "index",
"map": "return {};",
"filter": "",
"indexType": "reference",
"getShardName": "",
"getSortKey": "record.id",
"aggregateGroups": [],
"allowedRecordNodeIds": [],
"nodeId": 28
} }
], ],
"allidsShardFactor": 1, "allidsShardFactor": 1,

BIN
packages/server/appPackages/testApp/dist/package.tar.gz

Binary file not shown.

52
packages/server/middleware/routers.js

@ -8,6 +8,9 @@ const { getPackageForBuilder,
savePackage, getApps } = require("../utilities/builder"); savePackage, getApps } = require("../utilities/builder");
const builderPath = resolve(__dirname, "../builder"); const builderPath = resolve(__dirname, "../builder");
const appUiPath = appname => {
}
module.exports = (config, app) => { module.exports = (config, app) => {
@ -23,6 +26,22 @@ module.exports = (config, app) => {
.use(async (ctx, next) => { .use(async (ctx, next) => {
ctx.sessionId = ctx.session._sessCtx.externalKey; ctx.sessionId = ctx.session._sessCtx.externalKey;
ctx.session.accessed = true; ctx.session.accessed = true;
const pathParts = ctx.path.split("/");
if(pathParts.length < 2) {
ctx.throw(StatusCodes.NOT_FOUND, "App Name not declared");
}
const instance = await ctx.master.getInstanceApiForSession(
pathParts[1],
ctx.sessionId);
ctx.instance = instance.instance;
ctx.publicPath = instance.publicPath;
ctx.isAuthenticated = !!instance.instance;
await next(); await next();
}) })
.get("/_builder", async (ctx) => { .get("/_builder", async (ctx) => {
@ -46,11 +65,9 @@ module.exports = (config, app) => {
if(path.startsWith("/api/")) { if(path.startsWith("/api/")) {
await next(); await next();
return; } else {
await send(ctx, path, { root: builderPath });
} }
await send(ctx, path, { root: builderPath });
}) })
.get("/:appname", async (ctx) => { .get("/:appname", async (ctx) => {
ctx.response.status = StatusCodes.OK; ctx.response.status = StatusCodes.OK;
@ -139,22 +156,23 @@ module.exports = (config, app) => {
ctx.request.body); ctx.request.body);
ctx.response.status = StatusCodes.OK; ctx.response.status = StatusCodes.OK;
}) })
.use(async (ctx, next) => { .get("/:appname", async (ctx) => {
await send(ctx, "/index.html", { root: ctx.publicPath });
const pathParts = ctx.path.split("/"); })
.get("/:appname/*", async (ctx, next) => {
if(pathParts.length < 2) { const path = ctx.path.replace(`/${ctx.params.appname}`, "");
ctx.throw(StatusCodes.NOT_FOUND, "App Name not declared");
}
ctx.instance = await ctx.master.getInstanceApiForSession(
pathParts[1],
ctx.sessionId);
if(ctx.instance === null) { if(path.startsWith("/api/")) {
ctx.response.status = StatusCodes.UNAUTHORIZED; await next();
} else { } else {
await send(ctx, path, { root: ctx.publicPath });
}
})
.use(async (ctx, next) => {
if(ctx.isAuthenticated) {
await next(); await next();
} else {
ctx.response.status = StatusCodes.UNAUTHORIZED;
} }
}) })
.post("/:appname/api/changeMyPassword", async (ctx) => { .post("/:appname/api/changeMyPassword", async (ctx) => {

6
packages/server/package.json

@ -1,7 +1,7 @@
{ {
"name": "budibase", "name": "budibase",
"version": "0.0.1", "version": "0.0.1",
"description": "budibase wrapper repo for development", "description": "Budibase Web Server",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
@ -18,6 +18,8 @@
"koa": "^2.7.0", "koa": "^2.7.0",
"koa-body": "^4.1.0", "koa-body": "^4.1.0",
"koa-session": "^5.12.0", "koa-session": "^5.12.0",
"koa-send": "^5.0.0",
"koa-static": "^5.0.0",
"ncp": "^2.0.0", "ncp": "^2.0.0",
"rimraf": "^2.6.3", "rimraf": "^2.6.3",
"tar-fs": "^2.0.0", "tar-fs": "^2.0.0",
@ -27,8 +29,6 @@
"devDependencies": { "devDependencies": {
"@jest/test-sequencer": "^24.8.0", "@jest/test-sequencer": "^24.8.0",
"jest": "^24.8.0", "jest": "^24.8.0",
"koa-send": "^5.0.0",
"koa-static": "^5.0.0",
"server-destroy": "^1.0.1", "server-destroy": "^1.0.1",
"supertest": "^4.0.2" "supertest": "^4.0.2"
}, },

30
packages/server/utilities/createAppPackage.js

@ -21,8 +21,9 @@ const createAppPackage = (context, appPath) => {
appDefinition: appDefModule, appDefinition: appDefModule,
behaviourSources: pluginsModule(context), behaviourSources: pluginsModule(context),
appPath, appPath,
accessLevels accessLevels,
}) ...publicPaths(appPath)
});
} }
const appPackageFolder = (config, appname) => const appPackageFolder = (config, appname) =>
@ -53,15 +54,34 @@ module.exports.masterAppPackage = (context) => {
behaviourSources: config && config.extraMasterPlugins behaviourSources: config && config.extraMasterPlugins
? {...plugins, ...config.extraMasterPlugins} ? {...plugins, ...config.extraMasterPlugins}
: plugins, : plugins,
appPath: standardPackage.appPath appPath: standardPackage.appPath,
unauthenticatedUiPath: standardPackage.unauthenticatedUiPath,
mainUiPath: standardPackage.mainUiPath
}); });
} }
const applictionVersionPath = (appname, versionId) =>
join("..", getRuntimePackageDirectory(appname, versionId))
const publicPaths = (appPath) => ({
mainUiPath: join(
appPath, "ui", "main", "public"),
unauthenticatedUiPath: join(
appPath, "ui", "unauthenticated", "public")
});
module.exports.applictionVersionPublicPaths = (appname, versionId) => {
const appPath = applictionVersionPath(appname, versionId);
return publicPaths(appPath);
}
module.exports.applictionVersionPackage = async (context, appname, versionId, instanceKey) => { module.exports.applictionVersionPackage = async (context, appname, versionId, instanceKey) => {
const pkg = createAppPackage( const pkg = createAppPackage(
context, context,
join("..", getRuntimePackageDirectory(appname, versionId)) applictionVersionPath(appname, versionId)
); );
pkg.appDefinition = constructHierarchy(pkg.appDefinition); pkg.appDefinition = constructHierarchy(pkg.appDefinition);
await injectPlugins( await injectPlugins(
pkg, pkg,

43
packages/server/utilities/masterAppInternal.js

@ -7,7 +7,11 @@ const getDatastore = require("./datastore");
const getDatabaseManager = require("./databaseManager"); const getDatabaseManager = require("./databaseManager");
const {$, splitKey} = require("budibase-core").common; const {$, splitKey} = require("budibase-core").common;
const { keyBy, last } = require("lodash/fp"); const { keyBy, last } = require("lodash/fp");
const { masterAppPackage, applictionVersionPackage } = require("../utilities/createAppPackage"); const {
masterAppPackage,
applictionVersionPackage,
applictionVersionPublicPaths
} = require("../utilities/createAppPackage");
const isMaster = appname => appname === "_master"; const isMaster = appname => appname === "_master";
@ -137,15 +141,23 @@ module.exports = async (context) => {
const getInstanceApiForSession = async (appname, sessionId) => { const getInstanceApiForSession = async (appname, sessionId) => {
if(isMaster(appname)) { if(isMaster(appname)) {
const customId = bb.recordApi.customId("mastersession", sessionId); const customId = bb.recordApi.customId("mastersession", sessionId);
const masterPkg = masterAppPackage(context);
try { try {
const session = await bb.recordApi.load(`/sessions/${customId}`); const session = await bb.recordApi.load(`/sessions/${customId}`);
return await getApisForSession( return ({
masterDatastore, instance: await getApisForSession(
masterAppPackage(context), masterDatastore,
session); masterAppPackage(context),
session),
publicPath: masterPkg.mainUiPath
});
} catch(_) { } catch(_) {
return null; return ({
instance: null,
publicPath: masterPkg.unauthenticatedUiPath
});
} }
} }
else { else {
@ -163,12 +175,21 @@ module.exports = async (context) => {
const appPackage = await applictionVersionPackage( const appPackage = await applictionVersionPackage(
context, appname, versionId, session.instanceKey); context, appname, versionId, session.instanceKey);
return await getApisForSession( return ({
instanceDatastore, instance: await getApisForSession(
appPackage, instanceDatastore,
session); appPackage,
session),
publicPath: appPackage.mainUiPath
});
} catch(_) { } catch(_) {
return null; return ({
instance:null,
publicPath: applictionVersionPublicPaths(
app.name,
app.defaultVersion.id).unauthenticatedUiPath
});
} }
} }
}; };

Loading…
Cancel
Save