2021-04-08 18:39:46 +02:00
|
|
|
// need to load environment first
|
2022-11-23 19:25:20 +01:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-20 09:15:46 +01:00
|
|
|
import { Scope } from "@sentry/node"
|
|
|
|
import { Event } from "@sentry/types/dist/event"
|
|
|
|
import Application from "koa"
|
2022-04-20 12:30:03 +02:00
|
|
|
import { bootstrap } from "global-agent"
|
2022-11-14 23:55:47 +01:00
|
|
|
import * as db from "./db"
|
2022-11-23 19:25:20 +01:00
|
|
|
import { auth, logging, events, pinoSettings } from "@budibase/backend-core"
|
2022-03-29 17:03:44 +02:00
|
|
|
db.init()
|
2022-11-23 19:25:20 +01:00
|
|
|
import Koa from "koa"
|
|
|
|
import koaBody from "koa-body"
|
|
|
|
import http from "http"
|
2022-11-28 18:54:04 +01:00
|
|
|
import api from "./api"
|
2022-11-23 19:25:20 +01:00
|
|
|
import * as redis from "./utilities/redis"
|
2022-11-29 16:38:25 +01:00
|
|
|
const Sentry = require("@sentry/node")
|
2021-06-27 16:46:04 +02:00
|
|
|
const koaSession = require("koa-session")
|
2020-12-16 20:50:02 +01:00
|
|
|
const logger = require("koa-pino-logger")
|
2022-12-15 12:35:22 +01:00
|
|
|
import destroyable from "server-destroy"
|
2020-12-16 20:50:02 +01:00
|
|
|
|
2022-04-20 12:30:03 +02:00
|
|
|
// this will setup http and https proxies form env variables
|
|
|
|
bootstrap()
|
|
|
|
|
2022-01-20 09:15:46 +01:00
|
|
|
const app: Application = new Koa()
|
2020-12-16 20:50:02 +01:00
|
|
|
|
2021-07-05 18:16:45 +02:00
|
|
|
app.keys = ["secret", "key"]
|
2021-06-27 16:46:04 +02:00
|
|
|
|
2020-12-16 20:50:02 +01:00
|
|
|
// set up top level koa middleware
|
|
|
|
app.use(koaBody({ multipart: true }))
|
2021-06-27 16:46:04 +02:00
|
|
|
app.use(koaSession(app))
|
2022-08-04 21:23:45 +02:00
|
|
|
app.use(logger(pinoSettings()))
|
2020-12-16 20:50:02 +01:00
|
|
|
|
2021-04-01 21:34:43 +02:00
|
|
|
// authentication
|
2022-11-23 19:25:20 +01:00
|
|
|
app.use(auth.passport.initialize())
|
|
|
|
app.use(auth.passport.session())
|
2021-04-01 21:34:43 +02:00
|
|
|
|
2020-12-16 20:50:02 +01:00
|
|
|
// api routes
|
|
|
|
app.use(api.routes())
|
|
|
|
|
2021-10-18 15:57:30 +02:00
|
|
|
// sentry
|
2022-11-29 16:43:44 +01:00
|
|
|
if (env.isProd()) {
|
|
|
|
Sentry.init()
|
|
|
|
|
|
|
|
app.on("error", (err, ctx) => {
|
|
|
|
Sentry.withScope(function (scope: Scope) {
|
|
|
|
scope.addEventProcessor(function (event: Event) {
|
|
|
|
return Sentry.Handlers.parseRequest(event, ctx.request)
|
|
|
|
})
|
|
|
|
Sentry.captureException(err)
|
2021-10-18 15:57:30 +02:00
|
|
|
})
|
|
|
|
})
|
2022-11-29 16:43:44 +01:00
|
|
|
}
|
2021-10-18 15:57:30 +02:00
|
|
|
|
2020-12-16 20:50:02 +01:00
|
|
|
const server = http.createServer(app.callback())
|
|
|
|
destroyable(server)
|
|
|
|
|
2022-05-31 11:16:22 +02:00
|
|
|
let shuttingDown = false,
|
|
|
|
errCode = 0
|
2021-05-24 19:45:43 +02:00
|
|
|
server.on("close", async () => {
|
2022-05-30 22:22:06 +02:00
|
|
|
if (shuttingDown) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shuttingDown = true
|
2022-08-31 11:47:41 +02:00
|
|
|
console.log("Server Closed")
|
2021-05-24 19:45:43 +02:00
|
|
|
await redis.shutdown()
|
2022-05-10 11:33:59 +02:00
|
|
|
await events.shutdown()
|
2022-05-31 11:16:22 +02:00
|
|
|
if (!env.isTest()) {
|
|
|
|
process.exit(errCode)
|
|
|
|
}
|
2021-05-24 19:45:43 +02:00
|
|
|
})
|
2020-12-16 20:50:02 +01:00
|
|
|
|
2022-05-30 22:22:06 +02:00
|
|
|
const shutdown = () => {
|
|
|
|
server.close()
|
|
|
|
server.destroy()
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:25:20 +01:00
|
|
|
export = server.listen(parseInt(env.PORT || "4002"), async () => {
|
2020-12-17 13:39:55 +01:00
|
|
|
console.log(`Worker running on ${JSON.stringify(server.address())}`)
|
2021-05-24 19:45:43 +02:00
|
|
|
await redis.init()
|
2020-12-16 20:50:02 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
process.on("uncaughtException", err => {
|
2022-05-31 11:16:22 +02:00
|
|
|
errCode = -1
|
2022-11-23 19:25:20 +01:00
|
|
|
logging.logAlert("Uncaught exception.", err)
|
2022-05-30 22:22:06 +02:00
|
|
|
shutdown()
|
2020-12-16 20:50:02 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
process.on("SIGTERM", () => {
|
2022-05-30 22:22:06 +02:00
|
|
|
shutdown()
|
2020-12-16 20:50:02 +01:00
|
|
|
})
|
2022-08-25 20:41:47 +02:00
|
|
|
|
|
|
|
process.on("SIGINT", () => {
|
|
|
|
shutdown()
|
|
|
|
})
|