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-04-28 13:00:30 +00:00
|
|
|
const NotificationWorker = require('./lib/workers/notification');
|
2018-03-19 06:48:05 +00:00
|
|
|
|
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');
|
2018-04-26 12:08:08 +00:00
|
|
|
const resolvePath = require('resolve-path');
|
2018-04-26 14:34:07 +00:00
|
|
|
const cookieParser = require('cookie-parser');
|
2018-03-19 06:48:05 +00:00
|
|
|
|
|
|
|
const webpack = require('webpack');
|
|
|
|
const webpackDevMiddleware = require('webpack-dev-middleware');
|
|
|
|
const webpackHotMiddleware = require('webpack-hot-middleware');
|
2018-04-29 20:33:27 +00:00
|
|
|
const webpackConfigFactory = require('./webpack.config.js');
|
2018-03-19 06:48:05 +00:00
|
|
|
|
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-05-01 18:53:29 +00:00
|
|
|
console.log('Running in ' + (isDevelopment ? 'development' : 'production') + ' mode');
|
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-04-26 14:34:07 +00:00
|
|
|
app.use(cookieParser());
|
2018-03-19 06:48:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-26 12:08:08 +00:00
|
|
|
app.use('/', require('./lib/api/upload')(repository, tusServer));
|
|
|
|
app.use('/token', require('./lib/api/token')(repository));
|
|
|
|
app.use('/admin', require('./lib/api/admin')(repository));
|
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-04-29 20:33:27 +00:00
|
|
|
const webpackConfig = webpackConfigFactory(process.env, { mode: 'development' });
|
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-04-26 12:08:08 +00:00
|
|
|
|
|
|
|
// Automatic fallback support for file icons
|
|
|
|
app.get('/images/fileicons/:format/:filename', (req, res) =>
|
|
|
|
{
|
|
|
|
var basePath;
|
|
|
|
var filePath;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
basePath = resolvePath('./public/dist/images/fileicons/', req.params.format);
|
|
|
|
filePath = resolvePath(basePath, req.params.filename);
|
|
|
|
}
|
|
|
|
catch (err)
|
|
|
|
{
|
|
|
|
res.sendStatus(404);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.stat(filePath, (err, stat) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
res.sendFile(resolvePath(basePath, '_blank.png'));
|
|
|
|
else
|
|
|
|
res.sendFile(filePath);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-04-03 21:28:43 +00:00
|
|
|
app.use(express.static(path.join(__dirname, 'custom')));
|
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-04-28 13:00:30 +00:00
|
|
|
// Background workers
|
|
|
|
var notificationWorker = new NotificationWorker(repository);
|
|
|
|
notificationWorker.start(config.notifications.interval * 1000);
|
|
|
|
|
2018-04-26 12:08:08 +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
|
|
|
})();
|