1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-01 01:30:21 +01:00
haste-server/server.js

134 lines
4 KiB
JavaScript
Raw Normal View History

2020-08-25 22:57:13 +02:00
const fs = require('fs');
const winston = require('winston');
2020-08-26 04:54:58 +02:00
const uglify = require('uglify-js');
const st = require('st');
const app = require('express')();
const expressRateLimit = require('express-rate-limit');
2011-11-18 21:44:28 +01:00
2020-08-25 22:57:13 +02:00
const DocumentHandler = require('./lib/document_handler');
2020-08-26 04:54:58 +02:00
const HasteUtils = require('./lib/util');
const utils = new HasteUtils();
//set up logger
//only Console for now, gotta utilize config.json in the future
winston.add(new winston.transports.Console({
level: 'silly',
format: winston.format.combine(
winston.format.colorize(),
winston.format.printf(info => `${info.level}: ${info.message} ${utils.stringifyJSONMessagetoLogs(info)}`)
),
}));
2011-11-18 21:44:28 +01:00
2020-08-26 04:54:58 +02:00
//load config and set some defaults
const config = require('./config');
config.port = config.port || 7777;
config.host = config.host || '127.0.0.1';
//defaulting storage type to file
if (!config.storage){
config.storage = {
type: 'file',
path: './data'
};
2011-11-18 23:26:25 +01:00
}
2020-08-26 04:54:58 +02:00
if (!config.storage.type) config.storage.type = 'file';
2011-11-18 16:17:41 +01:00
2020-08-26 04:54:58 +02:00
let preferredStore;
if (process.env.REDISTOGO_URL && config.storage.type == 'redis'){
let redisClient = require('redis-url').connect(process.env.REDISTOGO_URL);
let Store = require('./lib/document_stores/redis');
preferredStore = new Store(config.storage, redisClient);
2012-09-27 17:46:53 +02:00
}
else {
2020-08-26 04:54:58 +02:00
let Store = require(`./lib/document_stores/${config.storage.type}`);
preferredStore = new Store(config.storage);
2012-09-27 17:46:53 +02:00
}
2011-11-18 16:17:41 +01:00
2020-08-26 04:54:58 +02:00
//compress static javascript assets
if (config.recompressStaticAssets){
let files = fs.readdirSync('./static');
for (const file of files){
if ((file.indexOf('.js') == file.length - 3) && (file.indexOf('.min.js') == -1)){
let dest = `${file.substring(0, file.length - 3)}.min${file.substring(file.length - 3)}`;
let origCode = fs.readFileSync(`./static/${file}`, 'utf8');
fs.writeFileSync(`./static/${dest}`, uglify.minify(origCode).code, 'utf8');
winston.info(`compressed ${file} into ${dest}`);
}
2011-11-27 21:49:17 +01:00
}
}
2020-08-26 04:54:58 +02:00
//send the static documents into the preferred store, skipping expirations
for (const name in config.documents){
let path = config.documents[name];
winston.info('loading static document', { name: name, path: path });
let data = fs.readFileSync(path, 'utf8');
if (data){
preferredStore.set(name, data, doc => winston.debug('loaded static document', { success: doc }), true);
}
else {
winston.warn('failed to load static document', { name: name, path: path });
}
2011-11-22 15:22:37 +01:00
}
2020-08-26 04:54:58 +02:00
//pick up a key generator
let pwOptions = config.keyGenerator || new Object;
pwOptions.type = pwOptions.type || 'random';
2020-08-26 04:54:58 +02:00
let Gen = require(`./lib/key_generators/${pwOptions.type}`);
let keyGenerator = new Gen(pwOptions);
2020-08-26 04:54:58 +02:00
//configure the document handler
2020-08-25 22:57:13 +02:00
let documentHandler = new DocumentHandler({
2020-08-26 04:54:58 +02:00
store: preferredStore,
maxLength: config.maxLength,
keyLength: config.keyLength,
keyGenerator: keyGenerator
2011-11-22 15:22:37 +01:00
});
2020-08-26 04:54:58 +02:00
//rate limit all requests
if (config.rateLimits) app.use(expressRateLimit(config.rateLimits));
2016-03-06 22:20:40 +01:00
2020-08-26 04:54:58 +02:00
//try API first
2016-03-06 22:20:40 +01:00
2020-08-26 04:54:58 +02:00
//get raw documents
app.get('/raw/:id', (req, res) => {
const key = req.params.id.split('.')[0];
const skipExpire = Boolean(config.documents[key]);
return documentHandler.handleGetRaw(key, res, skipExpire);
});
2016-03-06 22:20:40 +01:00
2020-08-26 04:54:58 +02:00
//add documents
app.post('/documents', (req, res) => {
return documentHandler.handlePost(req, res);
});
//get documents
app.get('/documents/:id', (req, res) => {
const key = req.params.id.split('.')[0];
const skipExpire = Boolean(config.documents[key]);
return documentHandler.handleGet(key, res, skipExpire);
});
//try static next
app.use(st({
path: './static',
passthrough: true,
index: false
2016-03-06 22:20:40 +01:00
}));
2020-08-26 04:54:58 +02:00
//then we can loop back - and everything else should be a token,
//so route it back to /
app.get('/:id', (req, res, next) => {
req.sturl = '/';
2016-03-06 22:20:40 +01:00
next();
2020-08-26 04:54:58 +02:00
});
2016-03-06 22:20:40 +01:00
2020-08-26 04:54:58 +02:00
//and match index
app.use(st({
2016-03-06 22:20:40 +01:00
content: { maxAge: config.staticMaxAge },
2020-08-26 04:54:58 +02:00
path: './static',
index: 'index.html'
2016-03-06 22:20:40 +01:00
}));
2020-08-26 04:54:58 +02:00
app.listen(config.port, config.host, () => winston.info(`listening on ${config.host}:${config.port}`));