2018-03-19 06:48:05 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const config = require('./config');
|
2018-03-20 16:22:56 +00:00
|
|
|
const Repository = require('./lib/repository');
|
2018-03-19 06:48:05 +00:00
|
|
|
|
|
|
|
const _ = require('lodash');
|
2018-03-21 15:51:46 +00:00
|
|
|
const fs = require('fs');
|
2018-03-19 06:48:05 +00:00
|
|
|
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');
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
(async () =>
|
2018-03-19 06:48:05 +00:00
|
|
|
{
|
2018-03-22 15:45:11 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const isDevelopment = process.env.NODE_ENV !== 'production';
|
2018-03-19 06:48:05 +00:00
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
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
|
2018-03-21 15:51:46 +00:00
|
|
|
});
|
2018-03-20 16:22:56 +00:00
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
const app = express();
|
2018-03-19 06:48:05 +00:00
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
app.disable('x-powered-by');
|
2018-03-19 06:48:05 +00:00
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
|
|
|
app.use(bodyParser.json());
|
2018-03-19 06:48:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
const loadAPI = (route, name) => { app.use(route, require('./lib/api/' + name)(repository)) }
|
2018-03-19 06:48:05 +00:00
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
loadAPI('/', 'upload');
|
|
|
|
loadAPI('/token', 'token');
|
|
|
|
loadAPI('/admin', 'admin');
|
2018-03-19 06:48:05 +00:00
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
// Frontend
|
|
|
|
if (isDevelopment)
|
2018-03-19 06:48:05 +00:00
|
|
|
{
|
2018-03-22 15:45:11 +00:00
|
|
|
const compiler = webpack(webpackConfig);
|
2018-03-19 06:48:05 +00:00
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
app.use(webpackDevMiddleware(compiler, {
|
|
|
|
publicPath: webpackConfig.output.publicPath
|
|
|
|
}));
|
2018-03-19 06:48:05 +00:00
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
app.use(webpackHotMiddleware(compiler));
|
|
|
|
}
|
2018-03-19 06:48:05 +00:00
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
app.use(express.static(path.join(__dirname, 'public', 'dist')));
|
2018-03-19 06:48:05 +00:00
|
|
|
|
2018-03-21 15:51:46 +00:00
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
// 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/') });
|
2018-03-21 15:51:46 +00:00
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
var server = app.listen(config.port, () => console.log('Recv running on port ' + server.address().port));
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
console.log(e);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-03-19 06:48:05 +00:00
|
|
|
})();
|