budibase/packages/server/src/app.ts

110 lines
2.5 KiB
TypeScript
Raw Normal View History

// need to load environment first
import env from "./environment"
2022-08-30 11:59:27 +02: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-11-26 16:10:41 +01:00
import * as db from "./db"
db.init()
import Koa from "koa"
import koaBody from "koa-body"
import http from "http"
import * as api from "./api"
import * as automations from "./automations"
import { Thread } from "./threads"
import * as redis from "./utilities/redis"
import { events, logging, middleware } from "@budibase/backend-core"
import { initialise as initialiseWebsockets } from "./websocket"
import { startup } from "./startup"
const Sentry = require("@sentry/node")
const destroyable = require("server-destroy")
2020-05-07 15:04:32 +02:00
const app = new Koa()
2019-06-14 11:05:46 +02:00
2020-05-07 15:04:32 +02:00
// set up top level koa middleware
2021-02-09 19:49:12 +01:00
app.use(
koaBody({
multipart: true,
formLimit: "10mb",
jsonLimit: "10mb",
textLimit: "10mb",
// @ts-ignore
2021-02-09 19:49:12 +01:00
enableTypes: ["json", "form", "text"],
parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
2021-02-09 19:49:12 +01:00
})
)
2020-05-14 16:12:30 +02:00
app.use(middleware.logging)
if (env.isProd()) {
env._set("NODE_ENV", "production")
2020-07-14 17:00:58 +02:00
Sentry.init()
app.on("error", (err: any, ctx: ExtendableContext) => {
Sentry.withScope(function (scope: any) {
scope.addEventProcessor(function (event: any) {
2020-07-14 17:00:58 +02:00
return Sentry.Handlers.parseRequest(event, ctx.request)
})
Sentry.captureException(err)
})
})
}
2020-07-16 15:27:27 +02:00
const server = http.createServer(app.callback())
destroyable(server)
initialiseWebsockets(server)
2020-07-16 15:27:27 +02:00
2022-05-31 11:16:22 +02:00
let shuttingDown = false,
errCode = 0
server.on("close", async () => {
// already in process
if (shuttingDown) {
return
}
shuttingDown = true
2022-08-31 11:47:41 +02:00
console.log("Server Closed")
await automations.shutdown()
await redis.shutdown()
await events.shutdown()
await Thread.shutdown()
api.shutdown()
2022-05-31 11:16:22 +02:00
if (!env.isTest()) {
process.exit(errCode)
}
})
export default server.listen(env.PORT || 0, async () => {
await startup(app, server)
2020-07-16 16:19:46 +02:00
})
2022-01-24 11:48:59 +01:00
const shutdown = () => {
server.close()
// @ts-ignore
server.destroy()
2022-01-24 11:48:59 +01: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 11:16:22 +02:00
errCode = -1
logging.logAlert("Uncaught exception.", err)
2022-01-24 11:48:59 +01:00
shutdown()
})
process.on("SIGTERM", () => {
2022-01-24 11:48:59 +01:00
shutdown()
})
2022-08-31 11:47:41 +02:00
process.on("SIGINT", () => {
shutdown()
})