Files
talk/routes/index.js
T

181 lines
5.1 KiB
JavaScript

const express = require('express');
const path = require('path');
const plugins = require('../services/plugins');
const debug = require('debug')('talk:routes');
const authentication = require('../middleware/authentication');
const {passport} = require('../services/passport');
const pubsub = require('../middleware/pubsub');
const i18n = require('../services/i18n');
const enabled = require('debug').enabled;
const errors = require('../errors');
const {createGraphOptions} = require('../graph');
const apollo = require('apollo-server-express');
const {DISABLE_STATIC_SERVER} = require('../config');
const SetupService = require('../services/setup');
const static = require('express-static-gzip');
const router = express.Router();
//==============================================================================
// STATIC FILES
//==============================================================================
if (!DISABLE_STATIC_SERVER) {
/**
* Serve the directories under public/dist from this router.
*/
router.use('/public', express.static(path.join(__dirname, '../public')));
router.use('/static', static(path.resolve(path.join(__dirname, '../dist')), {
indexFromEmptyFile: false,
enableBrotli: true,
customCompressions: [
{
encodingName: 'deflate',
fileExtension: 'zz',
},
],
}));
}
//==============================================================================
// PASSPORT MIDDLEWARE
//==============================================================================
const passportDebug = require('debug')('talk:passport');
// Install the passport plugins.
plugins.get('server', 'passport').forEach((plugin) => {
passportDebug(`added plugin '${plugin.plugin.name}'`);
// Pass the passport.js instance to the plugin to allow it to inject it's
// functionality.
plugin.passport(passport);
});
// Setup the PassportJS Middleware.
router.use(passport.initialize());
// Attach the authentication middleware, this will be responsible for decoding
// (if present) the JWT on the request.
router.use('/api', authentication, pubsub);
//==============================================================================
// GraphQL Router
//==============================================================================
// GraphQL endpoint.
router.use('/api/v1/graph/ql', apollo.graphqlExpress(createGraphOptions));
// Only include the graphiql tool if we aren't in production mode.
if (process.env.NODE_ENV !== 'production') {
// Interactive graphiql interface.
router.use('/api/v1/graph/iql', (req, res) => {
res.render('graphiql', {
endpointURL: `${req.app.locals.BASE_URL}api/v1/graph/ql`
});
});
// GraphQL documentation.
router.get('/admin/docs', (req, res) => {
res.render('admin/docs');
});
}
//==============================================================================
// ROUTES
//==============================================================================
router.use('/api/v1', require('./api'));
router.use('/admin', require('./admin'));
router.use('/embed', require('./embed'));
if (process.env.NODE_ENV !== 'production') {
router.use('/assets', require('./assets'));
router.get('/', async (req, res) => {
try {
await SetupService.isAvailable();
return res.redirect('/admin/install');
} catch (e) {
return res.render('article', {
title: 'Coral Talk',
asset_url: '',
asset_id: '',
body: '',
basePath: '/static/embed/stream'
});
}
});
} else {
router.get('/', async (req, res, next) => {
try {
await SetupService.isAvailable();
return res.redirect('/admin/install');
} catch (e) {
return res.redirect('/admin');
}
});
}
// Inject server route plugins.
plugins.get('server', 'router').forEach((plugin) => {
debug(`added plugin '${plugin.plugin.name}'`);
// Pass the root router to the plugin to mount it's routes.
plugin.router(router);
});
//==============================================================================
// ERROR HANDLING
//==============================================================================
// Catch 404 and forward to error handler.
router.use((req, res, next) => {
next(errors.ErrNotFound);
});
// General api error handler. Respond with the message and error if we have it
// while returning a status code that makes sense.
router.use('/api', (err, req, res, next) => {
if (err !== errors.ErrNotFound) {
if (process.env.NODE_ENV !== 'test' || enabled('talk:errors')) {
console.error(err);
}
}
if (err instanceof errors.APIError) {
res.status(err.status).json({
message: err.message,
error: err
});
} else {
res.status(500).json({});
}
});
router.use('/', (err, req, res, next) => {
if (err !== errors.ErrNotFound) {
console.error(err);
}
i18n.init(req);
if (err instanceof errors.APIError) {
res.status(err.status);
res.render('error', {
message: err.message,
error: process.env.NODE_ENV === 'development' ? err : {}
});
} else {
res.render('error', {
message: err.message,
error: process.env.NODE_ENV === 'development' ? err : {}
});
}
});
module.exports = router;