2019-08-09 01:58:11 +02:00
|
|
|
const path = require('path')
|
|
|
|
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')
|
2019-08-25 14:34:26 +02:00
|
|
|
const debug = require('debug')('routes')
|
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')
|
2020-01-27 00:47:03 +01:00
|
|
|
|
2019-10-11 18:34:14 +02:00
|
|
|
const helpers = require('./helpers')
|
2020-01-27 00:47:03 +01:00
|
|
|
const { startOfMonth, startOfWeek, getUnixTime } = require('date-fns')
|
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)
|
|
|
|
|
2019-12-26 11:46:21 +01:00
|
|
|
app.use((req, res, next) => {
|
2020-02-05 00:48:55 +01:00
|
|
|
debug(req.method, req.path)
|
2019-09-11 19:12:24 +02:00
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
2019-10-11 18:34:14 +02:00
|
|
|
// serve favicon and static content
|
2019-12-26 11:46:21 +01:00
|
|
|
app.use('/logo.png', express.static('./static/gancio.png'))
|
|
|
|
app.use('/media/', express.static(config.upload_path))
|
2019-10-11 18:34:14 +02:00
|
|
|
|
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-01-15 23:51:09 +01:00
|
|
|
app.use('/favicon.ico', (req, res, next) => {
|
|
|
|
const favicon_path = req.settings.favicon || config.favicon || './assets/favicon.ico'
|
|
|
|
return express.static(path.resolve(favicon_path))(req, res, next)
|
|
|
|
})
|
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) => {
|
|
|
|
debug('Error 500: %s', error)
|
|
|
|
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) => {
|
|
|
|
const start_datetime = getUnixTime(startOfWeek(startOfMonth(new Date())))
|
2020-02-09 18:18:47 +01:00
|
|
|
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
|