mirror of
https://github.com/SunRed/haste-server.git
synced 2024-11-23 17:50:19 +01:00
Made server run async
This commit is contained in:
parent
78aabf90bb
commit
f3288c4e39
3 changed files with 28 additions and 23 deletions
|
@ -14,8 +14,8 @@ const DocumentHandler = function(options) {
|
|||
DocumentHandler.defaultKeyLength = 10;
|
||||
|
||||
// Handle retrieving a document
|
||||
DocumentHandler.prototype.handleGet = function(key, res, skipExpire) {
|
||||
this.store.get(key, function(ret) {
|
||||
DocumentHandler.prototype.handleGet = async function(key, res, skipExpire) {
|
||||
await this.store.get(key, function(ret) {
|
||||
if (ret) {
|
||||
winston.verbose('retrieved document', { key: key });
|
||||
res.writeHead(200, { 'content-type': 'application/json' });
|
||||
|
@ -30,8 +30,8 @@ DocumentHandler.prototype.handleGet = function(key, res, skipExpire) {
|
|||
};
|
||||
|
||||
// Handle retrieving the raw version of a document
|
||||
DocumentHandler.prototype.handleGetRaw = function(key, res, skipExpire) {
|
||||
this.store.get(key, function(ret) {
|
||||
DocumentHandler.prototype.handleGetRaw = async function(key, res, skipExpire) {
|
||||
await this.store.get(key, function(ret) {
|
||||
if (ret) {
|
||||
winston.verbose('retrieved raw document', { key: key });
|
||||
res.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
|
||||
|
@ -46,13 +46,13 @@ DocumentHandler.prototype.handleGetRaw = function(key, res, skipExpire) {
|
|||
};
|
||||
|
||||
// Handle adding a new Document
|
||||
DocumentHandler.prototype.handlePost = function (req, res) {
|
||||
DocumentHandler.prototype.handlePost = async function (req, res) {
|
||||
let _this = this;
|
||||
let buffer = '';
|
||||
let cancelled = false;
|
||||
|
||||
// What to do when done
|
||||
let onSuccess = function () {
|
||||
let onSuccess = async function () {
|
||||
// Check length
|
||||
if (_this.maxLength && buffer.length > _this.maxLength) {
|
||||
cancelled = true;
|
||||
|
@ -64,8 +64,8 @@ DocumentHandler.prototype.handlePost = function (req, res) {
|
|||
return;
|
||||
}
|
||||
// And then save if we should
|
||||
_this.chooseKey(function (key) {
|
||||
_this.store.set(key, buffer, function (resp) {
|
||||
await _this.chooseKey(async function (key) {
|
||||
await _this.store.set(key, buffer, function (resp) {
|
||||
if (resp) {
|
||||
winston.verbose('added document', { key: key });
|
||||
res.writeHead(200, { 'content-type': 'application/json' });
|
||||
|
@ -89,8 +89,8 @@ DocumentHandler.prototype.handlePost = function (req, res) {
|
|||
buffer = val;
|
||||
}
|
||||
});
|
||||
busboy.on('finish', function () {
|
||||
onSuccess();
|
||||
busboy.on('finish', async function () {
|
||||
await onSuccess();
|
||||
});
|
||||
req.pipe(busboy);
|
||||
// Otherwise, use our own and just grab flat data from POST body
|
||||
|
@ -98,9 +98,9 @@ DocumentHandler.prototype.handlePost = function (req, res) {
|
|||
req.on('data', function (data) {
|
||||
buffer += data.toString();
|
||||
});
|
||||
req.on('end', function () {
|
||||
req.on('end', async function () {
|
||||
if (cancelled) { return; }
|
||||
onSuccess();
|
||||
await onSuccess();
|
||||
});
|
||||
req.on('error', function (error) {
|
||||
winston.error('connection error: ' + error.message);
|
||||
|
@ -112,10 +112,10 @@ DocumentHandler.prototype.handlePost = function (req, res) {
|
|||
};
|
||||
|
||||
// Keep choosing keys until one isn't taken
|
||||
DocumentHandler.prototype.chooseKey = function(callback) {
|
||||
DocumentHandler.prototype.chooseKey = async function(callback) {
|
||||
let key = this.acceptableKey();
|
||||
let _this = this;
|
||||
this.store.get(key, function(ret) {
|
||||
await this.store.get(key, function(ret) {
|
||||
if (ret) {
|
||||
_this.chooseKey(callback);
|
||||
} else {
|
||||
|
|
|
@ -46,7 +46,7 @@ FileDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
|
|||
// Get data from a file from key
|
||||
FileDocumentStore.prototype.get = function(key, callback, skipExpire) {
|
||||
const _this = this;
|
||||
const fn = this.basePath + '/' + FileDocumentStore.md5(key);
|
||||
const fn = require('path').join(this.basePath, FileDocumentStore.md5(key));
|
||||
fs.readFile(fn, 'utf8', function(err, data) {
|
||||
if (err) {
|
||||
callback(false);
|
||||
|
|
19
server.js
19
server.js
|
@ -59,13 +59,16 @@ if (config.recompressStaticAssets){
|
|||
winston.info(`compressed ${file} into ${dest}`);
|
||||
}
|
||||
}
|
||||
|
||||
(async function(){
|
||||
|
||||
//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);
|
||||
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 });
|
||||
|
@ -92,22 +95,22 @@ if (config.rateLimits) app.use(expressRateLimit(config.rateLimits));
|
|||
//try API first
|
||||
|
||||
//get raw documents
|
||||
app.get('/raw/:id', (req, res) => {
|
||||
app.get('/raw/:id', async (req, res) => {
|
||||
const key = req.params.id.split('.')[0];
|
||||
const skipExpire = Boolean(config.documents[key]);
|
||||
return documentHandler.handleGetRaw(key, res, skipExpire);
|
||||
return await documentHandler.handleGetRaw(key, res, skipExpire);
|
||||
});
|
||||
|
||||
//add documents
|
||||
app.post('/documents', (req, res) => {
|
||||
return documentHandler.handlePost(req, res);
|
||||
app.post('/documents', async (req, res) => {
|
||||
return await documentHandler.handlePost(req, res);
|
||||
});
|
||||
|
||||
//get documents
|
||||
app.get('/documents/:id', (req, res) => {
|
||||
app.get('/documents/:id', async (req, res) => {
|
||||
const key = req.params.id.split('.')[0];
|
||||
const skipExpire = Boolean(config.documents[key]);
|
||||
return documentHandler.handleGet(key, res, skipExpire);
|
||||
return await documentHandler.handleGet(key, res, skipExpire);
|
||||
});
|
||||
|
||||
//try static next
|
||||
|
@ -132,3 +135,5 @@ app.use(st({
|
|||
}));
|
||||
|
||||
app.listen(config.port, config.host, () => winston.info(`listening on ${config.host}:${config.port}`));
|
||||
|
||||
})();
|
Loading…
Reference in a new issue