Recv/index.js
Mark van Renswoude 8b9ed0e666 Further admin preparations
Improved exception handling in async functions
2018-03-22 16:45:11 +01:00

77 lines
1.9 KiB
JavaScript

'use strict'
const config = require('./config');
const Repository = require('./lib/repository');
const _ = require('lodash');
const fs = require('fs');
const express = require('express');
const bodyParser = require('body-parser');
const tus = require('tus-node-server');
const jwt = require('jsonwebtoken');
const path = require('path');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpackConfig = require('./webpack.config.js');
(async () =>
{
try
{
const isDevelopment = process.env.NODE_ENV !== 'production';
const repository = new Repository(config.database);
await repository.load();
const tusServer = new tus.Server();
tusServer.datastore = new tus.FileStore({
path: config.fileUpload.url,
directory: config.fileUpload.path
});
const app = express();
app.disable('x-powered-by');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
const loadAPI = (route, name) => { app.use(route, require('./lib/api/' + name)(repository)) }
loadAPI('/', 'upload');
loadAPI('/token', 'token');
loadAPI('/admin', 'admin');
// Frontend
if (isDevelopment)
{
const compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath
}));
app.use(webpackHotMiddleware(compiler));
}
app.use(express.static(path.join(__dirname, 'public', 'dist')));
// Redirects to make Vue-router URLs less quirky
app.get('/c/:code', (req, res) => { res.redirect(301, '/#/c/' + req.params.code) });
app.get('/admin', (req, res) => { res.redirect(301, '/#/admin/') });
var server = app.listen(config.port, () => console.log('Recv running on port ' + server.address().port));
}
catch (e)
{
console.log(e);
process.exit(1);
}
})();