2021-04-08 18:39:46 +02:00
|
|
|
// need to load environment first
|
2022-01-24 11:48:59 +01:00
|
|
|
import * as 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"
|
2022-03-29 17:03:44 +02:00
|
|
|
db.init()
|
2020-02-03 10:24:25 +01:00
|
|
|
const Koa = require("koa")
|
2020-12-04 13:09:02 +01:00
|
|
|
const destroyable = require("server-destroy")
|
2020-02-03 10:24:25 +01:00
|
|
|
const koaBody = require("koa-body")
|
2020-05-14 16:12:30 +02:00
|
|
|
const http = require("http")
|
2020-05-18 16:33:29 +02:00
|
|
|
const api = require("./api")
|
2020-09-21 14:49:34 +02:00
|
|
|
const automations = require("./automations/index")
|
2020-07-14 17:00:58 +02:00
|
|
|
const Sentry = require("@sentry/node")
|
2022-05-30 22:22:06 +02:00
|
|
|
const { Thread } = require("./threads")
|
2022-11-22 20:49:59 +01:00
|
|
|
import * as redis from "./utilities/redis"
|
|
|
|
import { events, logging } from "@budibase/backend-core"
|
2022-09-13 10:54:25 +02:00
|
|
|
import { initialise as initialiseWebsockets } from "./websocket"
|
2022-10-27 16:15:08 +02:00
|
|
|
import { startup } from "./startup"
|
2020-05-07 15:04:32 +02:00
|
|
|
|
2020-02-03 10:24:25 +01: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",
|
|
|
|
enableTypes: ["json", "form", "text"],
|
2021-06-11 19:56:30 +02:00
|
|
|
parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
|
2021-02-09 19:49:12 +01:00
|
|
|
})
|
|
|
|
)
|
2020-05-14 16:12:30 +02:00
|
|
|
|
2021-05-06 20:27:24 +02:00
|
|
|
if (env.isProd()) {
|
2020-10-28 21:35:06 +01:00
|
|
|
env._set("NODE_ENV", "production")
|
2020-07-14 17:00:58 +02:00
|
|
|
Sentry.init()
|
|
|
|
|
2021-06-24 19:16:48 +02:00
|
|
|
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())
|
2020-12-04 13:09:02 +01:00
|
|
|
destroyable(server)
|
2022-09-13 10:54:25 +02:00
|
|
|
initialiseWebsockets(server)
|
2020-07-16 15:27:27 +02:00
|
|
|
|
2022-05-31 11:16:22 +02:00
|
|
|
let shuttingDown = false,
|
|
|
|
errCode = 0
|
2021-05-24 18:05:46 +02:00
|
|
|
server.on("close", async () => {
|
2022-05-30 22:22:06 +02:00
|
|
|
// already in process
|
|
|
|
if (shuttingDown) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shuttingDown = true
|
2022-08-31 11:47:41 +02:00
|
|
|
console.log("Server Closed")
|
2022-05-30 22:22:06 +02:00
|
|
|
await automations.shutdown()
|
2021-05-24 18:05:46 +02:00
|
|
|
await redis.shutdown()
|
2022-05-10 11:33:59 +02:00
|
|
|
await events.shutdown()
|
2022-05-30 22:22:06 +02:00
|
|
|
await Thread.shutdown()
|
|
|
|
api.shutdown()
|
2022-05-31 11:16:22 +02:00
|
|
|
if (!env.isTest()) {
|
|
|
|
process.exit(errCode)
|
|
|
|
}
|
2021-03-10 18:55:42 +01:00
|
|
|
})
|
2020-07-15 18:25:08 +02:00
|
|
|
|
2021-02-03 17:09:48 +01:00
|
|
|
module.exports = server.listen(env.PORT || 0, async () => {
|
2022-10-27 16:15:08 +02:00
|
|
|
await startup(app, server)
|
2020-07-16 16:19:46 +02:00
|
|
|
})
|
2020-12-04 13:09:02 +01:00
|
|
|
|
2022-01-24 11:48:59 +01:00
|
|
|
const shutdown = () => {
|
2020-12-04 13:09:02 +01:00
|
|
|
server.close()
|
|
|
|
server.destroy()
|
2022-01-24 11:48:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
process.on("uncaughtException", err => {
|
2022-05-30 22:54:24 +02: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 11:16:22 +02:00
|
|
|
errCode = -1
|
2022-11-22 20:49:59 +01:00
|
|
|
logging.logAlert("Uncaught exception.", err)
|
2022-01-24 11:48:59 +01:00
|
|
|
shutdown()
|
2020-12-04 13:09:02 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
process.on("SIGTERM", () => {
|
2022-01-24 11:48:59 +01:00
|
|
|
shutdown()
|
2020-12-04 13:09:02 +01:00
|
|
|
})
|
2022-08-31 11:47:41 +02:00
|
|
|
|
|
|
|
process.on("SIGINT", () => {
|
|
|
|
shutdown()
|
|
|
|
})
|