2021-04-08 18:39:46 +02:00
|
|
|
// need to load environment first
|
|
|
|
const env = require("./environment")
|
2021-04-15 17:57:01 +02:00
|
|
|
const CouchDB = require("./db")
|
|
|
|
require("@budibase/auth").init(CouchDB)
|
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-07-14 17:00:58 +02:00
|
|
|
const electron = require("electron")
|
2020-02-03 10:24:25 +01:00
|
|
|
const koaBody = require("koa-body")
|
2020-05-18 16:33:29 +02:00
|
|
|
const logger = require("koa-pino-logger")
|
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-05-31 18:12:52 +02:00
|
|
|
const eventEmitter = require("./events")
|
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")
|
2021-03-23 19:04:53 +01:00
|
|
|
const fileSystem = require("./utilities/fileSystem")
|
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"],
|
|
|
|
})
|
|
|
|
)
|
2020-05-14 16:12:30 +02:00
|
|
|
|
2020-05-18 16:46:38 +02:00
|
|
|
app.use(
|
|
|
|
logger({
|
|
|
|
prettyPrint: {
|
|
|
|
levelFirst: true,
|
|
|
|
},
|
2020-05-29 15:06:25 +02:00
|
|
|
level: env.LOG_LEVEL || "error",
|
2020-05-18 16:46:38 +02:00
|
|
|
})
|
|
|
|
)
|
2020-05-04 18:13:57 +02:00
|
|
|
|
2020-05-31 18:12:52 +02:00
|
|
|
app.context.eventEmitter = eventEmitter
|
2020-10-12 14:32:52 +02:00
|
|
|
app.context.auth = {}
|
2020-05-24 23:54:08 +02:00
|
|
|
|
2020-05-07 15:04:32 +02:00
|
|
|
// api routes
|
|
|
|
app.use(api.routes())
|
2020-05-04 18:13:57 +02:00
|
|
|
|
2020-07-14 22:07:53 +02:00
|
|
|
if (electron.app && electron.app.isPackaged) {
|
2020-10-28 21:35:06 +01:00
|
|
|
env._set("NODE_ENV", "production")
|
2020-07-14 17:00:58 +02:00
|
|
|
Sentry.init()
|
|
|
|
|
|
|
|
app.on("error", (err, ctx) => {
|
2021-05-03 09:31:09 +02:00
|
|
|
Sentry.withScope(function (scope) {
|
|
|
|
scope.addEventProcessor(function (event) {
|
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)
|
2020-07-16 15:27:27 +02:00
|
|
|
|
2021-03-10 18:55:42 +01:00
|
|
|
server.on("close", () => {
|
|
|
|
if (env.NODE_ENV !== "jest") {
|
|
|
|
console.log("Server Closed")
|
|
|
|
}
|
|
|
|
})
|
2020-07-15 18:25:08 +02:00
|
|
|
|
2021-02-03 17:09:48 +01:00
|
|
|
module.exports = server.listen(env.PORT || 0, async () => {
|
2020-07-16 15:27:27 +02:00
|
|
|
console.log(`Budibase running on ${JSON.stringify(server.address())}`)
|
2021-02-03 17:09:48 +01:00
|
|
|
env._set("PORT", server.address().port)
|
|
|
|
eventEmitter.emitPort(env.PORT)
|
2021-03-23 19:04:53 +01:00
|
|
|
fileSystem.init()
|
2021-03-15 15:11:13 +01:00
|
|
|
await automations.init()
|
2020-07-16 16:19:46 +02:00
|
|
|
})
|
2020-12-04 13:09:02 +01:00
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
process.on("uncaughtException", err => {
|
2020-12-04 13:09:02 +01:00
|
|
|
console.error(err)
|
|
|
|
server.close()
|
|
|
|
server.destroy()
|
|
|
|
})
|
|
|
|
|
|
|
|
process.on("SIGTERM", () => {
|
|
|
|
server.close()
|
|
|
|
server.destroy()
|
|
|
|
})
|