2019-08-09 01:58:11 +02:00
|
|
|
const config = require('config')
|
|
|
|
const express = require('express')
|
2019-10-10 11:23:44 +02:00
|
|
|
const cors = require('cors')
|
2019-08-09 01:58:11 +02:00
|
|
|
const api = require('./api')
|
2019-12-26 11:46:21 +01:00
|
|
|
const oauth = require('./api/oauth')
|
2020-01-30 23:43:58 +01:00
|
|
|
const auth = require('./api/auth')
|
2019-10-30 14:58:40 +01:00
|
|
|
const cookieParser = require('cookie-parser')
|
2019-08-09 01:58:11 +02:00
|
|
|
const federation = require('./federation')
|
|
|
|
const webfinger = require('./federation/webfinger')
|
2019-09-13 11:08:18 +02:00
|
|
|
const { spamFilter } = require('./federation/helpers')
|
2021-03-05 14:17:10 +01:00
|
|
|
const log = require('./log')
|
2019-09-17 18:16:59 +02:00
|
|
|
const exportController = require('./api/controller/export')
|
2020-01-27 00:47:03 +01:00
|
|
|
const eventController = require('./api/controller/event')
|
2020-02-16 21:03:50 +01:00
|
|
|
const announceController = require('./api/controller/announce')
|
2021-02-09 12:17:39 +01:00
|
|
|
// const metricsController = require('./metrics')
|
|
|
|
const promBundle = require('express-prom-bundle')
|
|
|
|
const metricsMiddleware = promBundle({ includeMethod: true })
|
2020-01-27 00:47:03 +01:00
|
|
|
|
2019-10-11 18:34:14 +02:00
|
|
|
const helpers = require('./helpers')
|
2019-12-26 11:46:21 +01:00
|
|
|
const app = express()
|
2019-10-11 18:34:14 +02:00
|
|
|
|
2020-01-31 14:56:31 +01:00
|
|
|
// ignore unimplemented ping url from fediverse
|
|
|
|
app.use(spamFilter)
|
|
|
|
|
2021-02-09 12:17:39 +01:00
|
|
|
app.use(metricsMiddleware)
|
|
|
|
|
2019-12-26 11:46:21 +01:00
|
|
|
app.use((req, res, next) => {
|
2021-03-05 14:17:10 +01:00
|
|
|
log.debug(`${req.method} ${req.path}`)
|
2019-09-11 19:12:24 +02:00
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
2019-12-26 11:46:21 +01:00
|
|
|
app.use('/media/', express.static(config.upload_path))
|
2020-01-03 22:24:27 +01:00
|
|
|
// initialize instance settings / authentication / locale
|
2020-01-27 00:47:03 +01:00
|
|
|
app.use(helpers.initSettings)
|
2020-07-28 12:24:39 +02:00
|
|
|
|
2020-07-07 21:58:52 +02:00
|
|
|
// serve favicon and static content
|
|
|
|
app.use('/logo.png', (req, res, next) => {
|
2020-07-28 12:24:39 +02:00
|
|
|
const logoPath = req.settings.logo || './static/gancio'
|
|
|
|
return express.static(logoPath + '.png')(req, res, next)
|
2020-07-07 21:58:52 +02:00
|
|
|
})
|
|
|
|
|
2020-01-15 23:51:09 +01:00
|
|
|
app.use('/favicon.ico', (req, res, next) => {
|
2020-07-28 12:24:39 +02:00
|
|
|
const faviconPath = req.settings.logo || './assets/favicon'
|
|
|
|
return express.static(faviconPath + '.ico')(req, res, next)
|
2020-01-15 23:51:09 +01:00
|
|
|
})
|
2019-12-26 11:46:21 +01:00
|
|
|
|
2019-10-28 17:33:20 +01:00
|
|
|
// rss/ics/atom feed
|
2019-12-26 11:46:21 +01:00
|
|
|
app.get('/feed/:type', cors(), exportController.export)
|
2019-10-11 18:34:14 +02:00
|
|
|
|
2019-08-09 01:58:11 +02:00
|
|
|
// federation api / activitypub / webfinger / nodeinfo
|
2019-12-26 11:46:21 +01:00
|
|
|
app.use('/.well-known', webfinger)
|
|
|
|
app.use('/federation', federation)
|
2019-08-09 01:58:11 +02:00
|
|
|
|
2020-01-27 00:47:03 +01:00
|
|
|
// api!
|
|
|
|
app.use(cookieParser())
|
2020-01-30 23:43:58 +01:00
|
|
|
|
|
|
|
// fill req.user if request is authenticated
|
|
|
|
app.use(auth.fillUser)
|
2020-01-27 00:47:03 +01:00
|
|
|
app.use('/api', api)
|
|
|
|
app.use('/oauth', oauth)
|
|
|
|
|
2019-12-26 11:46:21 +01:00
|
|
|
// // Handle 500
|
2020-01-03 22:24:27 +01:00
|
|
|
app.use((error, req, res, next) => {
|
2021-03-05 14:17:10 +01:00
|
|
|
log.error(error)
|
2020-01-03 22:24:27 +01:00
|
|
|
res.status(500).send('500: Internal Server Error')
|
|
|
|
})
|
2019-08-25 14:34:26 +02:00
|
|
|
|
2019-10-11 18:34:14 +02:00
|
|
|
// remaining request goes to nuxt
|
2020-01-03 22:24:27 +01:00
|
|
|
// first nuxt component is ./pages/index.vue (with ./layouts/default.vue)
|
2020-02-16 21:03:50 +01:00
|
|
|
// prefill current events, tags, places and announcements (used in every path)
|
2020-01-27 00:47:03 +01:00
|
|
|
app.use(async (req, res, next) => {
|
2020-10-17 00:41:21 +02:00
|
|
|
// const start_datetime = getUnixTime(startOfWeek(startOfMonth(new Date())))
|
|
|
|
// req.events = await eventController._select(start_datetime, 100)
|
2020-01-27 00:47:03 +01:00
|
|
|
req.meta = await eventController._getMeta()
|
2020-02-16 21:03:50 +01:00
|
|
|
req.announcements = await announceController._getVisible()
|
2020-01-27 00:47:03 +01:00
|
|
|
next()
|
|
|
|
})
|
2019-09-17 18:16:59 +02:00
|
|
|
|
2019-12-26 11:46:21 +01:00
|
|
|
module.exports = app
|