2021-04-09 04:39:46 +12:00
|
|
|
// need to load environment first
|
2022-01-24 23:48:59 +13:00
|
|
|
import * as env from "./environment"
|
2022-08-30 21:59:27 +12:00
|
|
|
|
|
|
|
// enable APM if configured
|
|
|
|
if (process.env.ELASTIC_APM_ENABLED) {
|
|
|
|
const apm = require("elastic-apm-node").start({
|
|
|
|
serviceName: process.env.SERVICE,
|
|
|
|
environment: process.env.BUDIBASE_ENVIRONMENT,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
import { ExtendableContext } from "koa"
|
2022-03-30 04:03:44 +13:00
|
|
|
import db from "./db"
|
|
|
|
db.init()
|
2020-02-03 22:24:25 +13:00
|
|
|
const Koa = require("koa")
|
2020-12-05 01:09:02 +13:00
|
|
|
const destroyable = require("server-destroy")
|
2020-02-03 22:24:25 +13:00
|
|
|
const koaBody = require("koa-body")
|
2021-05-28 21:09:32 +12:00
|
|
|
const pino = require("koa-pino-logger")
|
2020-05-15 02:12:30 +12:00
|
|
|
const http = require("http")
|
2020-05-19 02:33:29 +12:00
|
|
|
const api = require("./api")
|
2020-06-01 04:12:52 +12:00
|
|
|
const eventEmitter = require("./events")
|
2020-09-22 00:49:34 +12:00
|
|
|
const automations = require("./automations/index")
|
2020-07-15 03:00:58 +12:00
|
|
|
const Sentry = require("@sentry/node")
|
2021-03-24 07:04:53 +13:00
|
|
|
const fileSystem = require("./utilities/fileSystem")
|
2021-05-07 23:24:51 +12:00
|
|
|
const bullboard = require("./automations/bullboard")
|
2022-05-31 21:16:22 +12:00
|
|
|
const { logAlert } = require("@budibase/backend-core/logging")
|
2022-08-05 07:23:45 +12:00
|
|
|
const { pinoSettings } = require("@budibase/backend-core")
|
2022-05-31 08:22:06 +12:00
|
|
|
const { Thread } = require("./threads")
|
2022-08-16 09:23:45 +12:00
|
|
|
const fs = require("fs")
|
2022-03-04 02:37:04 +13:00
|
|
|
import redis from "./utilities/redis"
|
2022-01-24 23:48:59 +13:00
|
|
|
import * as migrations from "./migrations"
|
2022-06-30 22:28:52 +12:00
|
|
|
import { events, installation, tenancy } from "@budibase/backend-core"
|
2022-09-06 05:28:53 +12:00
|
|
|
import {
|
|
|
|
createAdminUser,
|
|
|
|
generateApiKey,
|
|
|
|
getChecklist,
|
|
|
|
} from "./utilities/workerRequests"
|
2022-08-19 00:29:49 +12:00
|
|
|
import { watch } from "./watch"
|
2022-09-13 20:54:25 +12:00
|
|
|
import { initialise as initialiseWebsockets } from "./websocket"
|
2020-05-08 01:04:32 +12:00
|
|
|
|
2020-02-03 22:24:25 +13:00
|
|
|
const app = new Koa()
|
2019-06-14 21:05:46 +12:00
|
|
|
|
2020-05-08 01:04:32 +12:00
|
|
|
// set up top level koa middleware
|
2021-02-10 07:49:12 +13:00
|
|
|
app.use(
|
|
|
|
koaBody({
|
|
|
|
multipart: true,
|
|
|
|
formLimit: "10mb",
|
|
|
|
jsonLimit: "10mb",
|
|
|
|
textLimit: "10mb",
|
|
|
|
enableTypes: ["json", "form", "text"],
|
2021-06-12 05:56:30 +12:00
|
|
|
parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
|
2021-02-10 07:49:12 +13:00
|
|
|
})
|
|
|
|
)
|
2020-05-15 02:12:30 +12:00
|
|
|
|
2022-08-05 07:23:45 +12:00
|
|
|
app.use(pino(pinoSettings()))
|
2020-05-05 04:13:57 +12:00
|
|
|
|
2021-05-07 23:24:51 +12:00
|
|
|
if (!env.isTest()) {
|
2021-11-18 05:30:06 +13:00
|
|
|
const plugin = bullboard.init()
|
|
|
|
app.use(plugin)
|
2021-05-07 23:24:51 +12:00
|
|
|
}
|
|
|
|
|
2020-06-01 04:12:52 +12:00
|
|
|
app.context.eventEmitter = eventEmitter
|
2020-10-13 01:32:52 +13:00
|
|
|
app.context.auth = {}
|
2020-05-25 09:54:08 +12:00
|
|
|
|
2020-05-08 01:04:32 +12:00
|
|
|
// api routes
|
2022-05-31 08:22:06 +12:00
|
|
|
app.use(api.router.routes())
|
2020-05-05 04:13:57 +12:00
|
|
|
|
2021-05-07 06:27:24 +12:00
|
|
|
if (env.isProd()) {
|
2020-10-29 09:35:06 +13:00
|
|
|
env._set("NODE_ENV", "production")
|
2020-07-15 03:00:58 +12:00
|
|
|
Sentry.init()
|
|
|
|
|
2021-06-25 05:16:48 +12:00
|
|
|
app.on("error", (err: any, ctx: ExtendableContext) => {
|
|
|
|
Sentry.withScope(function (scope: any) {
|
|
|
|
scope.addEventProcessor(function (event: any) {
|
2020-07-15 03:00:58 +12:00
|
|
|
return Sentry.Handlers.parseRequest(event, ctx.request)
|
|
|
|
})
|
|
|
|
Sentry.captureException(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-17 01:27:27 +12:00
|
|
|
const server = http.createServer(app.callback())
|
2020-12-05 01:09:02 +13:00
|
|
|
destroyable(server)
|
2022-09-13 20:54:25 +12:00
|
|
|
initialiseWebsockets(server)
|
2020-07-17 01:27:27 +12:00
|
|
|
|
2022-05-31 21:16:22 +12:00
|
|
|
let shuttingDown = false,
|
|
|
|
errCode = 0
|
2021-05-25 04:05:46 +12:00
|
|
|
server.on("close", async () => {
|
2022-05-31 08:22:06 +12:00
|
|
|
// already in process
|
|
|
|
if (shuttingDown) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shuttingDown = true
|
2022-08-31 21:47:41 +12:00
|
|
|
console.log("Server Closed")
|
2022-05-31 08:22:06 +12:00
|
|
|
await automations.shutdown()
|
2021-05-25 04:05:46 +12:00
|
|
|
await redis.shutdown()
|
2022-05-10 21:33:59 +12:00
|
|
|
await events.shutdown()
|
2022-05-31 08:22:06 +12:00
|
|
|
await Thread.shutdown()
|
|
|
|
api.shutdown()
|
2022-05-31 21:16:22 +12:00
|
|
|
if (!env.isTest()) {
|
|
|
|
process.exit(errCode)
|
|
|
|
}
|
2021-03-11 06:55:42 +13:00
|
|
|
})
|
2020-07-16 04:25:08 +12:00
|
|
|
|
2021-02-04 05:09:48 +13:00
|
|
|
module.exports = server.listen(env.PORT || 0, async () => {
|
2020-07-17 01:27:27 +12:00
|
|
|
console.log(`Budibase running on ${JSON.stringify(server.address())}`)
|
2021-02-04 05:09:48 +13:00
|
|
|
env._set("PORT", server.address().port)
|
|
|
|
eventEmitter.emitPort(env.PORT)
|
2021-03-24 07:04:53 +13:00
|
|
|
fileSystem.init()
|
2021-05-13 04:37:09 +12:00
|
|
|
await redis.init()
|
2022-05-31 08:46:08 +12:00
|
|
|
|
|
|
|
// run migrations on startup if not done via http
|
|
|
|
// not recommended in a clustered environment
|
|
|
|
if (!env.HTTP_MIGRATIONS && !env.isTest()) {
|
|
|
|
try {
|
|
|
|
await migrations.migrate()
|
|
|
|
} catch (e) {
|
2022-06-06 20:50:06 +12:00
|
|
|
logAlert("Error performing migrations. Exiting.", e)
|
2022-05-31 08:46:08 +12:00
|
|
|
shutdown()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 22:28:52 +12:00
|
|
|
// check and create admin user if required
|
|
|
|
if (
|
|
|
|
env.SELF_HOSTED &&
|
|
|
|
!env.MULTI_TENANCY &&
|
|
|
|
env.BB_ADMIN_USER_EMAIL &&
|
|
|
|
env.BB_ADMIN_USER_PASSWORD
|
|
|
|
) {
|
|
|
|
const checklist = await getChecklist()
|
|
|
|
if (!checklist?.adminUser?.checked) {
|
2022-06-30 22:40:52 +12:00
|
|
|
try {
|
2022-07-05 05:11:40 +12:00
|
|
|
const tenantId = tenancy.getTenantId()
|
2022-09-06 05:28:53 +12:00
|
|
|
const user = await createAdminUser(
|
2022-06-30 22:40:52 +12:00
|
|
|
env.BB_ADMIN_USER_EMAIL,
|
|
|
|
env.BB_ADMIN_USER_PASSWORD,
|
2022-07-05 05:11:40 +12:00
|
|
|
tenantId
|
2022-06-30 22:40:52 +12:00
|
|
|
)
|
2022-09-06 05:28:53 +12:00
|
|
|
// Need to set up an API key for automated integration tests
|
|
|
|
if (env.isTest()) {
|
|
|
|
await generateApiKey(user._id)
|
|
|
|
}
|
|
|
|
|
2022-06-30 22:40:52 +12:00
|
|
|
console.log(
|
|
|
|
"Admin account automatically created for",
|
|
|
|
env.BB_ADMIN_USER_EMAIL
|
|
|
|
)
|
|
|
|
} catch (e) {
|
|
|
|
logAlert("Error creating initial admin user. Exiting.", e)
|
|
|
|
shutdown()
|
|
|
|
}
|
2022-06-30 22:28:52 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 09:23:45 +12:00
|
|
|
// monitor plugin directory if required
|
2022-08-17 03:27:03 +12:00
|
|
|
if (
|
|
|
|
env.SELF_HOSTED &&
|
|
|
|
!env.MULTI_TENANCY &&
|
|
|
|
env.PLUGINS_DIR &&
|
|
|
|
fs.existsSync(env.PLUGINS_DIR)
|
|
|
|
) {
|
2022-08-19 00:29:49 +12:00
|
|
|
watch()
|
2022-08-16 09:23:45 +12:00
|
|
|
}
|
|
|
|
|
2022-05-31 08:46:08 +12:00
|
|
|
// check for version updates
|
|
|
|
await installation.checkInstallVersion()
|
|
|
|
|
|
|
|
// done last - this will never complete
|
2021-09-08 06:06:20 +12:00
|
|
|
await automations.init()
|
2020-07-17 02:19:46 +12:00
|
|
|
})
|
2020-12-05 01:09:02 +13:00
|
|
|
|
2022-01-24 23:48:59 +13:00
|
|
|
const shutdown = () => {
|
2020-12-05 01:09:02 +13:00
|
|
|
server.close()
|
|
|
|
server.destroy()
|
2022-01-24 23:48:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
process.on("uncaughtException", err => {
|
2022-05-31 08:54:24 +12:00
|
|
|
// @ts-ignore
|
|
|
|
// don't worry about this error, comes from zlib isn't important
|
|
|
|
if (err && err["code"] === "ERR_INVALID_CHAR") {
|
|
|
|
return
|
|
|
|
}
|
2022-05-31 21:16:22 +12:00
|
|
|
errCode = -1
|
|
|
|
logAlert("Uncaught exception.", err)
|
2022-01-24 23:48:59 +13:00
|
|
|
shutdown()
|
2020-12-05 01:09:02 +13:00
|
|
|
})
|
|
|
|
|
|
|
|
process.on("SIGTERM", () => {
|
2022-01-24 23:48:59 +13:00
|
|
|
shutdown()
|
2020-12-05 01:09:02 +13:00
|
|
|
})
|
2022-08-31 21:47:41 +12:00
|
|
|
|
|
|
|
process.on("SIGINT", () => {
|
|
|
|
shutdown()
|
|
|
|
})
|