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")
|
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-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")
|
2022-05-31 21:16:22 +12:00
|
|
|
const { logAlert } = require("@budibase/backend-core/logging")
|
2022-05-31 08:22:06 +12:00
|
|
|
const { Thread } = require("./threads")
|
2022-03-04 02:37:04 +13:00
|
|
|
import redis from "./utilities/redis"
|
2022-10-28 03:15:08 +13:00
|
|
|
import { events } from "@budibase/backend-core"
|
2022-09-13 20:54:25 +12:00
|
|
|
import { initialise as initialiseWebsockets } from "./websocket"
|
2022-10-28 03:15:08 +13:00
|
|
|
import { startup } from "./startup"
|
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
|
|
|
|
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 () => {
|
2022-10-28 03:15:08 +13:00
|
|
|
await startup(app, server)
|
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()
|
|
|
|
})
|