budibase/packages/worker/src/index.js

64 lines
1.4 KiB
JavaScript
Raw Normal View History

// 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)
const Koa = require("koa")
const destroyable = require("server-destroy")
const koaBody = require("koa-body")
2021-06-27 16:46:04 +02:00
const koaSession = require("koa-session")
const { passport } = require("@budibase/auth").auth
const logger = require("koa-pino-logger")
const http = require("http")
const api = require("./api")
const redis = require("./utilities/redis")
const app = new Koa()
2021-06-27 16:46:04 +02:00
app.keys = ['secret', 'key'];
// set up top level koa middleware
app.use(koaBody({ multipart: true }))
2021-06-27 16:46:04 +02:00
app.use(koaSession(app))
app.use(
logger({
prettyPrint: {
levelFirst: true,
},
level: env.LOG_LEVEL || "error",
})
)
// authentication
app.use(passport.initialize())
app.use(passport.session())
// api routes
app.use(api.routes())
const server = http.createServer(app.callback())
destroyable(server)
server.on("close", async () => {
if (env.isProd()) {
console.log("Server Closed")
}
await redis.shutdown()
})
module.exports = server.listen(parseInt(env.PORT || 4002), async () => {
console.log(`Worker running on ${JSON.stringify(server.address())}`)
await redis.init()
})
process.on("uncaughtException", err => {
console.error(err)
server.close()
server.destroy()
})
process.on("SIGTERM", () => {
server.close()
server.destroy()
})