2020-02-03 10:24:25 +01:00
|
|
|
const Koa = require("koa")
|
2020-05-07 11:53:34 +02:00
|
|
|
const logger = require("koa-logger")
|
2020-05-07 15:04:32 +02:00
|
|
|
const api = require("./api")
|
2020-02-03 10:24:25 +01:00
|
|
|
const koaBody = require("koa-body")
|
2020-05-14 16:12:30 +02:00
|
|
|
const env = require("./environment")
|
|
|
|
const http = require("http")
|
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
|
|
|
|
|
|
|
if (env.LOGGER !== "off") app.use(logger())
|
2020-05-04 18:13:57 +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 => {
|
|
|
|
const server = http.createServer(app.callback())
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
server.on("error", e => {
|
|
|
|
if (e.code === "EADDRINUSE") {
|
|
|
|
reject(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
server.listen({ port }, () => {
|
|
|
|
resolve(server)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|