budibase/packages/server/src/app.js

55 lines
1.3 KiB
JavaScript
Raw Normal View History

const Koa = require("koa")
2020-07-14 17:00:58 +02:00
const electron = require("electron")
const koaBody = require("koa-body")
const logger = require("koa-pino-logger")
2020-05-14 16:12:30 +02:00
const http = require("http")
const api = require("./api")
const env = require("./environment")
2020-05-31 18:12:52 +02:00
const eventEmitter = require("./events")
const workflows = require("./workflows/index")
2020-07-14 17:00:58 +02:00
const Sentry = require("@sentry/node")
2020-05-07 15:04:32 +02: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
app.use(koaBody({ multipart: true }))
2020-05-14 16:12:30 +02:00
2020-05-18 16:46:38 +02:00
app.use(
logger({
prettyPrint: {
levelFirst: true,
},
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-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
if (electron.app && electron.app.isPackaged) {
process.env.NODE_ENV = "production"
2020-07-14 17:00:58 +02:00
Sentry.init()
app.on("error", (err, ctx) => {
Sentry.withScope(function(scope) {
scope.addEventProcessor(function(event) {
return Sentry.Handlers.parseRequest(event, ctx.request)
})
Sentry.captureException(err)
})
})
}
2020-07-16 15:27:27 +02:00
const server = http.createServer(app.callback())
server.on("close", () => console.log("Server Closed"))
2020-08-18 18:14:26 +02:00
process.on("SIGINT", () => process.exit(1))
2020-07-16 15:27:27 +02:00
module.exports = server.listen(env.PORT || 4001, () => {
console.log(`Budibase running on ${JSON.stringify(server.address())}`)
workflows.init()
2020-07-16 16:19:46 +02:00
})