2021-04-09 04:39:46 +12:00
|
|
|
// need to load environment first
|
2021-06-25 05:16:48 +12:00
|
|
|
import { ExtendableContext } from "koa"
|
|
|
|
|
2021-04-09 04:39:46 +12:00
|
|
|
const env = require("./environment")
|
2021-04-16 03:57:01 +12:00
|
|
|
const CouchDB = require("./db")
|
2022-01-11 08:33:00 +13:00
|
|
|
require("@budibase/backend-core").init(CouchDB)
|
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")
|
2021-05-28 21:09:32 +12:00
|
|
|
const pino = require("koa-pino-logger")
|
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-06-01 04:12:52 +12:00
|
|
|
const eventEmitter = require("./events")
|
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")
|
2021-03-24 07:04:53 +13:00
|
|
|
const fileSystem = require("./utilities/fileSystem")
|
2021-05-07 23:24:51 +12:00
|
|
|
const bullboard = require("./automations/bullboard")
|
2021-05-13 04:37:09 +12:00
|
|
|
const redis = require("./utilities/redis")
|
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-06-02 07:17:06 +12:00
|
|
|
app.use(
|
|
|
|
pino({
|
|
|
|
prettyPrint: {
|
|
|
|
levelFirst: true,
|
|
|
|
},
|
|
|
|
level: env.LOG_LEVEL || "error",
|
|
|
|
})
|
|
|
|
)
|
2020-05-05 04:13:57 +12:00
|
|
|
|
2021-05-07 23:24:51 +12:00
|
|
|
if (!env.isTest()) {
|
2021-11-18 05:30:06 +13:00
|
|
|
const plugin = bullboard.init()
|
|
|
|
app.use(plugin)
|
2021-05-07 23:24:51 +12:00
|
|
|
}
|
|
|
|
|
2020-06-01 04:12:52 +12:00
|
|
|
app.context.eventEmitter = eventEmitter
|
2020-10-13 01:32:52 +13:00
|
|
|
app.context.auth = {}
|
2020-05-25 09:54:08 +12:00
|
|
|
|
2020-05-08 01:04:32 +12:00
|
|
|
// api routes
|
|
|
|
app.use(api.routes())
|
2020-05-05 04:13:57 +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)
|
2020-07-17 01:27:27 +12:00
|
|
|
|
2021-05-25 04:05:46 +12:00
|
|
|
server.on("close", async () => {
|
2021-03-11 06:55:42 +13:00
|
|
|
if (env.NODE_ENV !== "jest") {
|
|
|
|
console.log("Server Closed")
|
|
|
|
}
|
2021-05-25 04:05:46 +12:00
|
|
|
await redis.shutdown()
|
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 () => {
|
2020-07-17 01:27:27 +12:00
|
|
|
console.log(`Budibase running on ${JSON.stringify(server.address())}`)
|
2021-02-04 05:09:48 +13:00
|
|
|
env._set("PORT", server.address().port)
|
|
|
|
eventEmitter.emitPort(env.PORT)
|
2021-03-24 07:04:53 +13:00
|
|
|
fileSystem.init()
|
2021-05-13 04:37:09 +12:00
|
|
|
await redis.init()
|
2021-09-08 06:06:20 +12:00
|
|
|
await automations.init()
|
2020-07-17 02:19:46 +12:00
|
|
|
})
|
2020-12-05 01:09:02 +13:00
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
process.on("uncaughtException", err => {
|
2020-12-05 01:09:02 +13:00
|
|
|
console.error(err)
|
|
|
|
server.close()
|
|
|
|
server.destroy()
|
|
|
|
})
|
|
|
|
|
|
|
|
process.on("SIGTERM", () => {
|
|
|
|
server.close()
|
|
|
|
server.destroy()
|
|
|
|
})
|