2020-02-03 10:24:25 +01:00
|
|
|
const Koa = require("koa")
|
|
|
|
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")
|
|
|
|
const env = require("./environment")
|
2020-05-31 18:12:52 +02:00
|
|
|
const eventEmitter = require("./events")
|
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
|
|
|
|
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,
|
|
|
|
},
|
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-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-05-14 16:12:30 +02:00
|
|
|
module.exports = async port => {
|
2020-05-18 16:33:29 +02:00
|
|
|
const serverPort = port || env.PORT
|
2020-05-14 16:12:30 +02:00
|
|
|
const server = http.createServer(app.callback())
|
2020-05-18 16:33:29 +02:00
|
|
|
return server.listen(serverPort || 4001)
|
2020-05-14 17:26:14 +02:00
|
|
|
}
|