1
0
Fork 0
mirror of synced 2024-10-06 04:54:52 +13:00
budibase/packages/worker/src/api/controllers/app.js

35 lines
953 B
JavaScript

const fetch = require("node-fetch")
const CouchDB = require("../../db")
const env = require("../../environment")
const APP_PREFIX = "app_"
const URL_REGEX_SLASH = /\/|\\/g
exports.getApps = async ctx => {
let allDbs
// allDbs call of CouchDB is very inaccurate in production
if (env.COUCH_DB_URL) {
allDbs = await (await fetch(`${env.COUCH_DB_URL}/_all_dbs`)).json()
} else {
allDbs = await CouchDB.allDbs()
}
const appDbNames = allDbs.filter(dbName => dbName.startsWith(APP_PREFIX))
const appPromises = appDbNames.map(db => new CouchDB(db).get(db))
const apps = await Promise.allSettled(appPromises)
const body = {}
for (let app of apps) {
if (app.status !== "fulfilled") {
continue
}
app = app.value
let url = app.url || encodeURI(`${app.name}`)
url = `/${url.replace(URL_REGEX_SLASH, "")}`
body[url] = {
appId: app._id,
name: app.name,
url,
}
}
ctx.body = body
}