2023-02-15 17:28:20 +01:00
|
|
|
if (process.env.DD_APM_ENABLED) {
|
|
|
|
require("./ddApm")
|
|
|
|
}
|
2022-08-30 11:59:27 +02:00
|
|
|
|
2023-02-15 17:28:20 +01:00
|
|
|
// need to load environment first
|
|
|
|
import env from "./environment"
|
2022-01-20 09:15:46 +01:00
|
|
|
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"
|
2023-02-27 15:41:28 +01:00
|
|
|
import { sdk as proSdk } from "@budibase/pro"
|
2023-02-27 14:42:51 +01:00
|
|
|
import {
|
|
|
|
auth,
|
|
|
|
logging,
|
|
|
|
events,
|
|
|
|
middleware,
|
2023-02-27 15:51:33 +01:00
|
|
|
queue,
|
2023-02-27 14:42:51 +01:00
|
|
|
env as coreEnv,
|
2023-03-27 20:38:49 +02:00
|
|
|
timers,
|
2023-11-10 17:17:18 +01:00
|
|
|
redis,
|
2024-03-08 13:20:52 +01:00
|
|
|
cache,
|
2024-08-07 16:26:04 +02:00
|
|
|
features,
|
2023-02-27 14:42:51 +01:00
|
|
|
} from "@budibase/backend-core"
|
2023-11-20 21:52:29 +01:00
|
|
|
|
2023-11-10 17:17:18 +01:00
|
|
|
db.init()
|
2022-11-23 19:25:20 +01:00
|
|
|
import koaBody from "koa-body"
|
|
|
|
import http from "http"
|
2022-11-28 18:54:04 +01:00
|
|
|
import api from "./api"
|
2023-10-17 11:11:08 +02:00
|
|
|
|
2021-06-27 16:46:04 +02:00
|
|
|
const koaSession = require("koa-session")
|
2023-11-20 21:52:29 +01:00
|
|
|
|
2023-10-17 11:11:08 +02:00
|
|
|
import { userAgent } from "koa-useragent"
|
2023-02-21 20:14:57 +01:00
|
|
|
|
2022-12-15 12:35:22 +01:00
|
|
|
import destroyable from "server-destroy"
|
2023-03-13 14:25:19 +01:00
|
|
|
import { initPro } from "./initPro"
|
2023-03-15 12:23:55 +01:00
|
|
|
import { handleScimBody } from "./middleware/handleScimBody"
|
2020-12-16 20:50:02 +01:00
|
|
|
|
2023-02-27 14:42:51 +01:00
|
|
|
if (coreEnv.ENABLE_SSO_MAINTENANCE_MODE) {
|
2023-02-21 09:23:53 +01:00
|
|
|
console.warn(
|
|
|
|
"Warning: ENABLE_SSO_MAINTENANCE_MODE is set. It is recommended this flag is disabled if maintenance is not in progress"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-20 12:30:03 +02:00
|
|
|
// this will setup http and https proxies form env variables
|
|
|
|
bootstrap()
|
|
|
|
|
2023-11-20 16:15:43 +01:00
|
|
|
const app: Application = new Application()
|
2020-12-16 20:50:02 +01:00
|
|
|
|
2021-07-05 18:16:45 +02:00
|
|
|
app.keys = ["secret", "key"]
|
2024-10-09 14:34:43 +02:00
|
|
|
app.proxy = true
|
2021-06-27 16:46:04 +02:00
|
|
|
|
2020-12-16 20:50:02 +01:00
|
|
|
// set up top level koa middleware
|
2023-03-15 12:23:55 +01:00
|
|
|
app.use(handleScimBody)
|
2023-03-15 13:34:17 +01:00
|
|
|
app.use(koaBody({ multipart: true }))
|
2023-03-15 12:23:55 +01:00
|
|
|
|
2021-06-27 16:46:04 +02:00
|
|
|
app.use(koaSession(app))
|
2023-04-04 16:08:46 +02:00
|
|
|
app.use(middleware.correlation)
|
|
|
|
app.use(middleware.pino)
|
2024-10-09 13:57:14 +02:00
|
|
|
app.use(middleware.ip)
|
2024-11-15 17:01:23 +01:00
|
|
|
if (!coreEnv.DISABLE_CONTENT_SECURITY_POLICY) {
|
2024-11-10 14:08:51 +01:00
|
|
|
app.use(middleware.csp)
|
|
|
|
}
|
2023-02-21 20:14:57 +01:00
|
|
|
app.use(userAgent)
|
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())
|
|
|
|
|
|
|
|
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")
|
2023-03-27 20:38:49 +02:00
|
|
|
timers.cleanup()
|
2023-11-17 17:20:10 +01:00
|
|
|
events.shutdown()
|
|
|
|
await redis.clients.shutdown()
|
2023-02-27 15:41:28 +01:00
|
|
|
await queue.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()
|
|
|
|
}
|
|
|
|
|
2023-01-11 10:37:37 +01:00
|
|
|
export default server.listen(parseInt(env.PORT || "4002"), async () => {
|
2024-06-11 13:13:18 +02:00
|
|
|
let startupLog = `Worker running on ${JSON.stringify(server.address())}`
|
|
|
|
if (env.BUDIBASE_ENVIRONMENT) {
|
|
|
|
startupLog = `${startupLog} - environment: "${env.BUDIBASE_ENVIRONMENT}"`
|
|
|
|
}
|
|
|
|
console.log(startupLog)
|
2024-11-19 12:34:15 +01:00
|
|
|
|
2023-03-13 14:25:19 +01:00
|
|
|
await initPro()
|
2023-11-17 17:37:00 +01:00
|
|
|
await redis.clients.init()
|
2024-08-09 15:00:16 +02:00
|
|
|
features.init()
|
2024-03-08 13:20:52 +01:00
|
|
|
cache.docWritethrough.init()
|
2023-11-03 15:55:56 +01:00
|
|
|
// configure events to use the pro audit log write
|
|
|
|
// can't integrate directly into backend-core due to cyclic issues
|
|
|
|
await events.processors.init(proSdk.auditLogs.write)
|
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()
|
|
|
|
})
|