forked from zneix/haste-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
122 lines (101 loc) · 3.43 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const winston = require('winston');
const fs = require('fs');
const st = require('st');
const app = require('express')();
const expressRateLimit = require('express-rate-limit');
const DocumentHandler = require('./lib/document_handler');
const HasteUtils = require('./lib/util');
const utils = new HasteUtils();
(async function(){
//"out-of-box" support - copy example config if it doesn't exist
if (!fs.existsSync('./config.js')){
await fs.promises.copyFile('./example.config.js', './config.js').catch(err => {
winston.error('failed to copy example config', {error: err});
process.exit(1);
});
}
//load config and set some defaults
const config = require('./config');
config.host = process.env.HOST || config.host || '127.0.0.1';
config.port = process.env.PORT || config.port || 7777;
//set up logger
winston.add(new winston.transports.Console({
level: config.logging.level,
format: winston.format.combine(
winston.format.colorize(),
winston.format.printf(info => `${info.level}: ${info.message} ${utils.stringifyJSONMessagetoLogs(info)}`)
),
}));
//defaulting storage type to file
if (!config.storage){
config.storage = {
type: 'file',
path: './data'
};
}
if (!config.storage.type) config.storage.type = 'file';
let Store = require(`./lib/document_stores/${config.storage.type}`);
let preferredStore = new Store(config.storage);
//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){
await 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 });
}
}
//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({
store: preferredStore,
maxLength: config.maxLength,
keyLength: config.keyLength,
keyGenerator: keyGenerator
});
//rate limit all requests
if (config.rateLimits) app.use(expressRateLimit(config.rateLimits));
//try API first
//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) => {
return await documentHandler.handlePost(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({
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) => {
req.sturl = '/';
next();
});
//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}`));
})();