2019-08-09 01:58:11 +02:00
|
|
|
const express = require('express')
|
2024-06-26 15:34:33 +02:00
|
|
|
require('express-async-errors')
|
2022-07-27 11:23:57 +02:00
|
|
|
const app = express()
|
2022-05-20 13:03:34 +02:00
|
|
|
const initialize = require('./initialize.server')
|
2022-05-20 14:01:47 +02:00
|
|
|
|
2022-08-02 15:14:34 +02:00
|
|
|
const config = require('./config')
|
|
|
|
const helpers = require('./helpers')
|
2022-12-23 01:08:14 +01:00
|
|
|
const api = require('./api')
|
2022-08-02 15:14:34 +02:00
|
|
|
|
2022-07-27 11:23:57 +02:00
|
|
|
async function main () {
|
2023-03-19 23:26:57 +01:00
|
|
|
const log = require('./log')
|
2022-12-23 01:08:14 +01:00
|
|
|
|
2023-03-19 23:26:57 +01:00
|
|
|
try {
|
|
|
|
await initialize.start()
|
|
|
|
} catch (e) {
|
|
|
|
log.error('[ERROR]' + e)
|
|
|
|
}
|
2022-12-23 01:08:14 +01:00
|
|
|
|
|
|
|
app.use([
|
|
|
|
helpers.initSettings,
|
|
|
|
helpers.logRequest,
|
|
|
|
helpers.serveStatic()
|
|
|
|
])
|
2022-07-27 11:23:57 +02:00
|
|
|
// const metricsController = require('./metrics')
|
|
|
|
// const promBundle = require('express-prom-bundle')
|
|
|
|
// const metricsMiddleware = promBundle({ includeMethod: true })
|
2022-08-02 15:14:54 +02:00
|
|
|
|
2022-07-27 11:23:57 +02:00
|
|
|
app.enable('trust proxy')
|
|
|
|
|
|
|
|
// do not handle all routes on setup
|
|
|
|
if (config.status === 'READY') {
|
|
|
|
const cors = require('cors')
|
|
|
|
const { spamFilter } = require('./federation/helpers')
|
|
|
|
const federation = require('./federation')
|
|
|
|
const webfinger = require('./federation/webfinger')
|
|
|
|
const exportController = require('./api/controller/export')
|
|
|
|
const tagController = require('./api/controller/tag')
|
|
|
|
const placeController = require('./api/controller/place')
|
|
|
|
const collectionController = require('./api/controller/collection')
|
2022-11-04 12:22:21 +01:00
|
|
|
const authController = require('./api/controller/oauth')
|
2022-07-27 11:23:57 +02:00
|
|
|
|
|
|
|
// rss / ics feed
|
|
|
|
app.use(helpers.feedRedirect)
|
|
|
|
app.get('/feed/:format/tag/:tag', cors(), tagController.getEvents)
|
|
|
|
app.get('/feed/:format/place/:placeName', cors(), placeController.getEvents)
|
|
|
|
app.get('/feed/:format/collection/:name', cors(), collectionController.getEvents)
|
|
|
|
app.get('/feed/:format', cors(), exportController.export)
|
|
|
|
|
|
|
|
app.use('/event/:slug', helpers.APRedirect)
|
|
|
|
|
|
|
|
// federation api / activitypub / webfinger / nodeinfo
|
|
|
|
app.use('/federation', federation)
|
|
|
|
app.use('/.well-known', webfinger)
|
|
|
|
|
|
|
|
// ignore unimplemented ping url from fediverse
|
|
|
|
app.use(spamFilter)
|
|
|
|
|
2022-11-04 12:22:21 +01:00
|
|
|
app.use(authController.authenticate)
|
|
|
|
app.post('/oauth/token', authController.token)
|
|
|
|
app.post('/oauth/login', authController.login)
|
|
|
|
app.get('/oauth/authorize', authController.authorization)
|
|
|
|
app.post('/oauth/authorize', authController.decision)
|
2021-09-30 11:06:59 +02:00
|
|
|
}
|
2022-07-27 11:23:57 +02:00
|
|
|
|
|
|
|
// api!
|
2022-12-23 01:08:14 +01:00
|
|
|
app.use('/api', api())
|
2022-07-27 11:23:57 +02:00
|
|
|
|
|
|
|
// // Handle 500
|
|
|
|
app.use((error, _req, res, _next) => {
|
2024-06-26 15:34:33 +02:00
|
|
|
log.error('[ERROR] %s', error)
|
|
|
|
return res.sendStatus(500) //.send('500: Internal Server Error')
|
2022-07-27 11:23:57 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// remaining request goes to nuxt
|
|
|
|
// first nuxt component is ./pages/index.vue (with ./layouts/default.vue)
|
|
|
|
app.use(async (_req, res, next) => {
|
2024-05-16 17:59:30 +02:00
|
|
|
await helpers.initLocals(res)
|
2022-07-27 11:23:57 +02:00
|
|
|
res.locals.status = config.status
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
|
|
main()
|
|
|
|
}
|
2019-09-17 18:16:59 +02:00
|
|
|
|
2022-05-23 14:42:15 +02:00
|
|
|
module.exports = {
|
2022-07-27 11:23:57 +02:00
|
|
|
main,
|
2022-05-23 14:42:15 +02:00
|
|
|
handler: app,
|
2022-05-25 10:53:39 +02:00
|
|
|
unload: () => initialize.shutdown(false)
|
2022-05-23 14:42:15 +02:00
|
|
|
}
|