1
0
Fork 0
mirror of synced 2024-06-27 18:40:42 +12:00
budibase/packages/server/src/app.ts

188 lines
4.5 KiB
TypeScript
Raw Normal View History

// 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"
import db from "./db"
db.init()
const Koa = require("koa")
const destroyable = require("server-destroy")
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")
const api = require("./api")
2020-06-01 04:12:52 +12:00
const eventEmitter = require("./events")
const automations = require("./automations/index")
2020-07-15 03:00:58 +12:00
const Sentry = require("@sentry/node")
const fileSystem = require("./utilities/fileSystem")
const bullboard = require("./automations/bullboard")
2022-05-31 21:16:22 +12:00
const { logAlert } = require("@budibase/backend-core/logging")
const { pinoSettings } = require("@budibase/backend-core")
const { Thread } = require("./threads")
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"
import { events, installation, tenancy } from "@budibase/backend-core"
import { createAdminUser, getChecklist } from "./utilities/workerRequests"
import { watch } from "./watch"
2022-08-23 05:24:34 +12:00
import { Websocket } from "./websocket"
2020-05-08 01:04:32 +12: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"],
parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
2021-02-10 07:49:12 +13:00
})
)
2020-05-15 02:12:30 +12:00
app.use(pino(pinoSettings()))
2020-05-05 04:13:57 +12:00
if (!env.isTest()) {
const plugin = bullboard.init()
app.use(plugin)
}
2020-06-01 04:12:52 +12:00
app.context.eventEmitter = eventEmitter
app.context.auth = {}
2020-05-25 09:54:08 +12:00
2020-05-08 01:04:32 +12:00
// api routes
app.use(api.router.routes())
2020-05-05 04:13:57 +12:00
if (env.isProd()) {
env._set("NODE_ENV", "production")
2020-07-15 03:00:58 +12:00
Sentry.init()
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())
destroyable(server)
2020-07-17 01:27:27 +12:00
2022-08-23 05:24:34 +12:00
// initialise websockets
export const ClientAppSocket = new Websocket(server, "/socket/client")
2022-08-19 22:09:20 +12:00
2022-05-31 21:16:22 +12:00
let shuttingDown = false,
errCode = 0
server.on("close", async () => {
// already in process
if (shuttingDown) {
return
}
shuttingDown = true
2022-08-31 21:47:41 +12:00
console.log("Server Closed")
await automations.shutdown()
await redis.shutdown()
await events.shutdown()
await Thread.shutdown()
api.shutdown()
2022-05-31 21:16:22 +12:00
if (!env.isTest()) {
process.exit(errCode)
}
})
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())}`)
env._set("PORT", server.address().port)
eventEmitter.emitPort(env.PORT)
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) {
logAlert("Error performing migrations. Exiting.", e)
2022-05-31 08:46:08 +12:00
shutdown()
}
}
// 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) {
try {
2022-07-05 05:11:40 +12:00
const tenantId = tenancy.getTenantId()
await createAdminUser(
env.BB_ADMIN_USER_EMAIL,
env.BB_ADMIN_USER_PASSWORD,
2022-07-05 05:11:40 +12:00
tenantId
)
console.log(
"Admin account automatically created for",
env.BB_ADMIN_USER_EMAIL
)
} catch (e) {
logAlert("Error creating initial admin user. Exiting.", e)
shutdown()
}
}
}
// 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)
) {
watch()
}
2022-05-31 08:46:08 +12:00
// check for version updates
await installation.checkInstallVersion()
// done last - this will never complete
await automations.init()
2020-07-17 02:19:46 +12:00
})
2022-01-24 23:48:59 +13:00
const shutdown = () => {
server.close()
server.destroy()
2022-01-24 23:48:59 +13:00
}
process.on("uncaughtException", err => {
// @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()
})
process.on("SIGTERM", () => {
2022-01-24 23:48:59 +13:00
shutdown()
})
2022-08-31 21:47:41 +12:00
process.on("SIGINT", () => {
shutdown()
})