1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-23 09:50:17 +01:00

Config rewrite

This commit is contained in:
zneix 2020-08-28 04:46:48 +02:00
parent e4eeec3d27
commit daabcf7dcd
6 changed files with 141 additions and 123 deletions

View file

@ -1,2 +1,3 @@
**/*.min.js
config.js
example.config.js

1
.gitignore vendored
View file

@ -8,3 +8,4 @@ data
npm-debug.log
static/*.min.js
config.js

View file

@ -40,7 +40,7 @@ STDOUT. Check the README there for more details and usages.
* `keyLength` - the length of the keys to user (default 10)
* `maxLength` - maximum length of a paste (default 400000)
* `staticMaxAge` - max age for static assets (86400)
* `recompressStaticAssets` - whether or not to compile static js assets (true)
* `compressStaticAssets` - whether or not to compile static js assets (true)
* `documents` - static documents to serve (ex: http://hastebin.com/about.md)
in addition to static assets. These will never expire.
* `storage` - storage options (see below)

View file

@ -1,31 +0,0 @@
module.exports = {
"host": "127.0.0.1",
"port": 4200,
"keyLength": 10,
"maxLength": 400000,
"staticMaxAge": 60 * 60 * 24,
"recompressStaticAssets": true,
"logging": [], //re-add this feature later
"keyGenerator": {
"type": "phonetic"
},
"rateLimits": {
"windowMs": 60 * 60 * 1000,
"max": 500
},
"storage": {
"type": "file",
"path": "./data"
},
"documents": {
"about": "./about.md"
}
}

48
example.config.js Normal file
View file

@ -0,0 +1,48 @@
module.exports = {
//address and port to which server will bind, host can also be a hostname
"host": "127.0.0.1",
"port": 7777,
//length of random characters in link that's generated on document save
"keyLength": 10,
//max allowed paste length - 0 for unlimited
"maxLength": 400000,
//algorithm used to generate random characters
//see docs/generators.md for more information
"keyGenerator": {
"type": "phonetic"
},
//max age for static website assets
"staticMaxAge": 60 * 60 * 24,
//whether or not to minify static js scripts which load faster for end-users
"compressStaticAssets": true,
//TODO: re-add more options to logging
//logging preferences
"logging": {
//can be one of: error, warn, info, http, verbose, debug, silly
"level": "info"
},
//rate limits for requests, handled by express-rate-limit
//options can be found here: https://github.com/nfriedly/express-rate-limit/blob/master/lib/express-rate-limit.js#L7-L14
"rateLimits": {
"windowMs": 30 * 60 * 1000,
"max": 250
},
//storage system used for storing saved haste documents
//see docs/storage.md for more information
"storage": {
"type": "file",
"path": "./data"
},
//static documents that will never expire ("name": "path")
"documents": {
"about": "./about.md"
}
};

View file

@ -1,5 +1,5 @@
const fs = require('fs');
const winston = require('winston');
const fs = require('fs');
const minify = require('babel-minify');
const st = require('st');
const app = require('express')();
@ -11,9 +11,8 @@ 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',
level: config.logging.level,
format: winston.format.combine(
winston.format.colorize(),
winston.format.printf(info => `${info.level}: ${info.message} ${utils.stringifyJSONMessagetoLogs(info)}`)
@ -46,7 +45,7 @@ else {
}
//compress static javascript assets
if (config.recompressStaticAssets){
if (config.compressStaticAssets){
let files = fs.readdirSync('./static');
//https://regex101.com/r/5cJagJ/2
for (const file of files){
@ -62,8 +61,8 @@ if (config.recompressStaticAssets){
(async function(){
//send the static documents into the preferred store, skipping expirations
for (const name in config.documents){
//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');
@ -73,67 +72,67 @@ for (const name in config.documents){
else {
winston.warn('failed to load static document', { name: name, path: path });
}
}
}
//pick up a key generator
let pwOptions = config.keyGenerator || new Object;
pwOptions.type = pwOptions.type || 'random';
let Gen = require(`./lib/key_generators/${pwOptions.type}`);
let keyGenerator = new Gen(pwOptions);
//pick up a key generator
let pwOptions = config.keyGenerator || new Object;
pwOptions.type = pwOptions.type || 'random';
let Gen = require(`./lib/key_generators/${pwOptions.type}`);
let keyGenerator = new Gen(pwOptions);
//configure the document handler
let documentHandler = new DocumentHandler({
//configure the document handler
let documentHandler = new DocumentHandler({
store: preferredStore,
maxLength: config.maxLength,
keyLength: config.keyLength,
keyGenerator: keyGenerator
});
});
//rate limit all requests
if (config.rateLimits) app.use(expressRateLimit(config.rateLimits));
//rate limit all requests
if (config.rateLimits) app.use(expressRateLimit(config.rateLimits));
//try API first
//try API first
//get raw documents
app.get('/raw/:id', async (req, res) => {
//get raw documents
app.get('/raw/:id', async (req, res) => {
const key = req.params.id.split('.')[0];
const skipExpire = Boolean(config.documents[key]);
return await documentHandler.handleGetRaw(key, res, skipExpire);
});
});
//add documents
app.post('/documents', async (req, res) => {
//add documents
app.post('/documents', async (req, res) => {
return await documentHandler.handlePost(req, res);
});
});
//get documents
app.get('/documents/:id', async (req, res) => {
//get documents
app.get('/documents/:id', async (req, res) => {
const key = req.params.id.split('.')[0];
const skipExpire = Boolean(config.documents[key]);
return await documentHandler.handleGet(key, res, skipExpire);
});
});
//try static next
app.use(st({
//try static next
app.use(st({
path: './static',
passthrough: true,
index: false
}));
}));
//then we can loop back - and everything else should be a token,
//so route it back to /
app.get('/:id', (req, res, next) => {
//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 = '/';
next();
});
});
//and match index
app.use(st({
//and match index
app.use(st({
content: { maxAge: config.staticMaxAge },
path: './static',
index: 'index.html'
}));
}));
app.listen(config.port, config.host, () => winston.info(`listening on ${config.host}:${config.port}`));
app.listen(config.port, config.host, () => winston.info(`listening on ${config.host}:${config.port}`));
})();