1
0
Fork 0
mirror of synced 2024-06-20 19:30:28 +12:00
budibase/packages/server/src/app.ts

107 lines
2.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")
2020-05-15 02:12:30 +12:00
const http = require("http")
const api = require("./api")
const automations = require("./automations/index")
2020-07-15 03:00:58 +12:00
const Sentry = require("@sentry/node")
2022-05-31 21:16:22 +12:00
const { logAlert } = require("@budibase/backend-core/logging")
const { Thread } = require("./threads")
2022-03-04 02:37:04 +13:00
import redis from "./utilities/redis"
import { events } from "@budibase/backend-core"
import { initialise as initialiseWebsockets } from "./websocket"
import { startup } from "./startup"
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
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)
initialiseWebsockets(server)
2020-07-17 01:27:27 +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 () => {
await startup(app, server)
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()
})