gancio-upstream/server/index.js

74 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2019-06-07 17:02:33 +02:00
#!/bin/env node
const path = require('path')
2019-04-03 00:25:12 +02:00
const express = require('express')
const consola = require('consola')
2019-04-23 15:45:52 +02:00
const morgan = require('morgan')
2019-06-06 23:54:32 +02:00
const { Nuxt, Builder } = require('nuxt')
2019-07-03 16:58:24 +02:00
2019-08-02 13:43:28 +02:00
const api = require('./api')
const federation = require('./federation')
const webfinger = require('./federation/webfinger')
2019-04-03 00:25:12 +02:00
// Import and Set Nuxt.js options
const nuxt_config = require('../nuxt.config.js')
const config = require('config')
2019-04-03 00:25:12 +02:00
2019-06-07 17:02:33 +02:00
const app = express()
2019-04-03 00:25:12 +02:00
async function start() {
nuxt_config.server = config.server
2019-04-03 00:25:12 +02:00
// Init Nuxt.js
const nuxt = new Nuxt(nuxt_config)
2019-04-03 00:25:12 +02:00
// Build only in dev mode
if (nuxt_config.dev) {
2019-04-03 00:25:12 +02:00
const builder = new Builder(nuxt)
await builder.build()
} else {
await nuxt.ready()
}
// configurable favicon && logo
2019-07-29 01:27:47 +02:00
app.use('/favicon.ico', express.static(path.resolve(config.favicon || 'assets/favicon.ico')))
2019-04-23 15:45:52 +02:00
app.use(morgan('dev'))
app.use('/media/', express.static(config.upload_path))
2019-07-30 00:57:45 +02:00
// gancio standard api
2019-08-02 13:43:28 +02:00
app.use('/api', api)
2019-07-03 17:22:26 +02:00
2019-07-30 00:57:45 +02:00
// federation api / activitypub / webfinger / nodeinfo
2019-08-02 13:43:28 +02:00
app.use('/.well-known', webfinger)
app.use('/federation', federation)
2019-07-29 01:27:47 +02:00
2019-07-03 16:33:03 +02:00
// Give nuxt middleware to express
2019-04-03 00:25:12 +02:00
app.use(nuxt.render)
// Listen
const server = app.listen(nuxt_config.server)
// close connections/port/unix socket
function shutdown() {
consola.info(`Closing connections..`)
2019-07-08 00:49:03 +02:00
server.close(() => {
// TODO, close db connection?
process.exit()
})
}
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)
server.on('error', e => {
consola.error(e)
})
server.on('listening', () => {
const address = server.address()
consola.ready({
message: `Server listening on ${(typeof address) === 'object' ? `${address.address}:${address.port}` : address}`,
badge: true
})
2019-04-03 00:25:12 +02:00
})
}
2019-06-06 23:54:32 +02:00
start()