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
|
2019-06-10 00:40:37 +02:00
|
|
|
const nuxt_config = require('../nuxt.config.js')
|
2019-06-21 23:52:18 +02:00
|
|
|
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() {
|
2019-06-21 23:52:18 +02:00
|
|
|
nuxt_config.server = config.server
|
2019-04-03 00:25:12 +02:00
|
|
|
// Init Nuxt.js
|
2019-06-10 00:40:37 +02:00
|
|
|
const nuxt = new Nuxt(nuxt_config)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
|
|
|
// Build only in dev mode
|
2019-06-10 00:40:37 +02:00
|
|
|
if (nuxt_config.dev) {
|
2019-04-03 00:25:12 +02:00
|
|
|
const builder = new Builder(nuxt)
|
|
|
|
await builder.build()
|
|
|
|
} else {
|
|
|
|
await nuxt.ready()
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:49:02 +02:00
|
|
|
// 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-07-24 11:49:02 +02:00
|
|
|
|
2019-04-23 15:45:52 +02:00
|
|
|
app.use(morgan('dev'))
|
2019-06-11 17:44:11 +02:00
|
|
|
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)
|
|
|
|
|
2019-07-24 11:49:02 +02:00
|
|
|
// Listen
|
2019-06-21 23:52:18 +02:00
|
|
|
const server = app.listen(nuxt_config.server)
|
2019-06-10 00:40:37 +02:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
})
|
2019-06-10 00:40:37 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2019-06-21 23:52:18 +02:00
|
|
|
start()
|