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-12-16 20:50:02 +01:00
|
|
|
const Koa = require("koa")
|
|
|
|
const destroyable = require("server-destroy")
|
|
|
|
const koaBody = require("koa-body")
|
2021-04-08 12:20:37 +02:00
|
|
|
const { passport } = require("@budibase/auth")
|
2020-12-16 20:50:02 +01:00
|
|
|
const logger = require("koa-pino-logger")
|
|
|
|
const http = require("http")
|
|
|
|
const api = require("./api")
|
|
|
|
|
|
|
|
const app = new Koa()
|
|
|
|
|
|
|
|
if (!env.SELF_HOSTED) {
|
|
|
|
throw "Currently this service only supports use in self hosting"
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up top level koa middleware
|
|
|
|
app.use(koaBody({ multipart: true }))
|
|
|
|
|
|
|
|
app.use(
|
|
|
|
logger({
|
|
|
|
prettyPrint: {
|
|
|
|
levelFirst: true,
|
|
|
|
},
|
|
|
|
level: env.LOG_LEVEL || "error",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2021-04-01 21:34:43 +02:00
|
|
|
// authentication
|
|
|
|
app.use(passport.initialize())
|
|
|
|
app.use(passport.session())
|
|
|
|
|
2020-12-16 20:50:02 +01:00
|
|
|
// api routes
|
|
|
|
app.use(api.routes())
|
|
|
|
|
|
|
|
const server = http.createServer(app.callback())
|
|
|
|
destroyable(server)
|
|
|
|
|
|
|
|
server.on("close", () => console.log("Server Closed"))
|
|
|
|
|
2021-03-29 16:00:40 +02:00
|
|
|
module.exports = server.listen(parseInt(env.PORT || 4002), async () => {
|
2020-12-17 13:39:55 +01:00
|
|
|
console.log(`Worker running on ${JSON.stringify(server.address())}`)
|
2020-12-16 20:50:02 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
process.on("uncaughtException", err => {
|
|
|
|
console.error(err)
|
|
|
|
server.close()
|
|
|
|
server.destroy()
|
|
|
|
})
|
|
|
|
|
|
|
|
process.on("SIGTERM", () => {
|
|
|
|
server.close()
|
|
|
|
server.destroy()
|
|
|
|
})
|