forked from tsai/budibase
6 changed files with 79 additions and 8 deletions
@ -0,0 +1,7 @@ |
|||
### Self hosting |
|||
This directory contains utilities that are needed for self hosted platforms to operate. |
|||
These will mostly be utilities, necessary to the operation of the server e.g. storing self |
|||
hosting specific options and attributes to CouchDB. |
|||
|
|||
All the internal operations should be exposed through the `index.js` so importing |
|||
the self host directory should give you everything you need. |
|||
@ -0,0 +1,39 @@ |
|||
const CouchDB = require("../db") |
|||
const env = require("../environment") |
|||
const newid = require("../db/newid") |
|||
|
|||
const SELF_HOST_DB = "self-host-db" |
|||
const SELF_HOST_DOC = "self-host-info" |
|||
|
|||
async function createSelfHostDB(db) { |
|||
await db.put({ |
|||
_id: "_design/database", |
|||
views: {}, |
|||
}) |
|||
const selfHostInfo = { |
|||
_id: SELF_HOST_DOC, |
|||
apiKeyId: newid(), |
|||
} |
|||
await db.put(selfHostInfo) |
|||
return selfHostInfo |
|||
} |
|||
|
|||
exports.init = async () => { |
|||
if (!env.SELF_HOSTED) { |
|||
return |
|||
} |
|||
const db = new CouchDB(SELF_HOST_DB) |
|||
try { |
|||
await db.get(SELF_HOST_DOC) |
|||
} catch (err) { |
|||
// failed to retrieve
|
|||
if (err.status === 404) { |
|||
await createSelfHostDB(db) |
|||
} |
|||
} |
|||
} |
|||
|
|||
exports.getSelfHostInfo = async () => { |
|||
const db = new CouchDB(SELF_HOST_DB) |
|||
return db.get(SELF_HOST_DOC) |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
const { apiKeyTable } = require("../../db/dynamoClient") |
|||
const env = require("../../environment") |
|||
const { getSelfHostInfo } = require("../../selfhost") |
|||
|
|||
/** |
|||
* This file purely exists so that we can centralise all logic pertaining to API keys, as their usage differs |
|||
* in our Cloud environment versus self hosted. |
|||
*/ |
|||
|
|||
exports.getAPIKey = async apiKeyId => { |
|||
if (env.CLOUD && !env.SELF_HOSTED) { |
|||
return apiKeyTable.get({ |
|||
primary: apiKeyId, |
|||
}) |
|||
} |
|||
if (env.SELF_HOSTED) { |
|||
const selfHostInfo = await getSelfHostInfo() |
|||
// if the api key supplied is correct then return structure similar
|
|||
return apiKeyId === selfHostInfo.apiKeyId ? { pk: apiKeyId } : null |
|||
} |
|||
return null |
|||
} |
|||
Loading…
Reference in new issue