Merge branch 'master' of https://github.com/seejohnrun/haste-server
This commit is contained in:
commit
3603469130
|
@ -38,7 +38,7 @@ STDOUT. Check the README there for more details and usages.
|
||||||
* `host` - the host the server runs on (default localhost)
|
* `host` - the host the server runs on (default localhost)
|
||||||
* `port` - the port the server runs on (default 7777)
|
* `port` - the port the server runs on (default 7777)
|
||||||
* `keyLength` - the length of the keys to user (default 10)
|
* `keyLength` - the length of the keys to user (default 10)
|
||||||
* `maxLength` - maximum length of a paste (default none)
|
* `maxLength` - maximum length of a paste (default 400000)
|
||||||
* `staticMaxAge` - max age for static assets (86400)
|
* `staticMaxAge` - max age for static assets (86400)
|
||||||
* `recompressStaticAssets` - whether or not to compile static js assets (true)
|
* `recompressStaticAssets` - whether or not to compile static js assets (true)
|
||||||
* `documents` - static documents to serve (ex: http://hastebin.com/about.com)
|
* `documents` - static documents to serve (ex: http://hastebin.com/about.com)
|
||||||
|
@ -156,9 +156,9 @@ All of which are optional except `type` with very logical default values.
|
||||||
|
|
||||||
### Memcached
|
### Memcached
|
||||||
|
|
||||||
To use memcached storage you must install the `memcache` package via npm
|
To use memcache storage you must install the `memcached` package via npm
|
||||||
|
|
||||||
`npm install memcache`
|
`npm install memcached`
|
||||||
|
|
||||||
Once you've done that, your config section should look like:
|
Once you've done that, your config section should look like:
|
||||||
|
|
||||||
|
|
|
@ -33,10 +33,9 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
"storage": {
|
"storage": {
|
||||||
"type": "redis",
|
"type": "memcached",
|
||||||
"host": "0.0.0.0",
|
"host": "127.0.0.1",
|
||||||
"port": 6379,
|
"port": 11211,
|
||||||
"db": 2,
|
|
||||||
"expire": 2592000
|
"expire": 2592000
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ DocumentHandler.prototype.handleRawGet = function(key, response, skipExpire) {
|
||||||
this.store.get(key, function(ret) {
|
this.store.get(key, function(ret) {
|
||||||
if (ret) {
|
if (ret) {
|
||||||
winston.verbose('retrieved raw document', { key: key });
|
winston.verbose('retrieved raw document', { key: key });
|
||||||
response.writeHead(200, { 'content-type': 'text/plain' });
|
response.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
|
||||||
response.end(ret);
|
response.end(ret);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -123,7 +123,7 @@ DocumentHandler.prototype.chooseKey = function(callback) {
|
||||||
} else {
|
} else {
|
||||||
callback(key);
|
callback(key);
|
||||||
}
|
}
|
||||||
});
|
}, true); // Don't bump expirations when key searching
|
||||||
};
|
};
|
||||||
|
|
||||||
DocumentHandler.prototype.acceptableKey = function() {
|
DocumentHandler.prototype.acceptableKey = function() {
|
||||||
|
|
|
@ -1,45 +1,52 @@
|
||||||
var memcached = require('memcache');
|
const memcached = require('memcached');
|
||||||
var winston = require('winston');
|
const winston = require('winston');
|
||||||
|
|
||||||
|
class MemcachedDocumentStore {
|
||||||
|
|
||||||
// Create a new store with options
|
// Create a new store with options
|
||||||
var MemcachedDocumentStore = function(options) {
|
constructor(options) {
|
||||||
this.expire = options.expire;
|
this.expire = options.expire;
|
||||||
if (!MemcachedDocumentStore.client) {
|
|
||||||
MemcachedDocumentStore.connect(options);
|
const host = options.host || '127.0.0.1';
|
||||||
|
const port = options.port || 11211;
|
||||||
|
const url = `${host}:${port}`;
|
||||||
|
this.connect(url);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// Create a connection
|
// Create a connection
|
||||||
MemcachedDocumentStore.connect = function(options) {
|
connect(url) {
|
||||||
var host = options.host || '127.0.0.1';
|
this.client = new memcached(url);
|
||||||
var port = options.port || 11211;
|
|
||||||
this.client = new memcached.Client(port, host);
|
winston.info(`connecting to memcached on ${url}`);
|
||||||
this.client.connect();
|
|
||||||
this.client.on('connect', function() {
|
this.client.on('failure', function(error) {
|
||||||
winston.info('connected to memcached on ' + host + ':' + port);
|
winston.info('error connecting to memcached', {error});
|
||||||
});
|
});
|
||||||
this.client.on('error', function(e) {
|
}
|
||||||
winston.info('error connecting to memcached', { error: e });
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// Save file in a key
|
// Save file in a key
|
||||||
MemcachedDocumentStore.prototype.set =
|
set(key, data, callback, skipExpire) {
|
||||||
function(key, data, callback, skipExpire) {
|
this.client.set(key, data, skipExpire ? 0 : this.expire, (error) => {
|
||||||
MemcachedDocumentStore.client.set(key, data, function(err) {
|
callback(!error);
|
||||||
err ? callback(false) : callback(true);
|
});
|
||||||
}, skipExpire ? 0 : this.expire);
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// Get a file from a key
|
// Get a file from a key
|
||||||
MemcachedDocumentStore.prototype.get = function(key, callback, skipExpire) {
|
get(key, callback, skipExpire) {
|
||||||
var _this = this;
|
this.client.get(key, (error, data) => {
|
||||||
MemcachedDocumentStore.client.get(key, function(err, reply) {
|
callback(error ? false : data);
|
||||||
callback(err ? false : reply);
|
|
||||||
if (_this.expire && !skipExpire) {
|
// Update the key so that the expiration is pushed forward
|
||||||
winston.warn('store does not currently push forward expirations on GET');
|
if (!skipExpire) {
|
||||||
|
this.set(key, data, (updateSucceeded) => {
|
||||||
|
if (!updateSucceeded) {
|
||||||
|
winston.error('failed to update expiration on GET', {key});
|
||||||
|
}
|
||||||
|
}, skipExpire);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = MemcachedDocumentStore;
|
module.exports = MemcachedDocumentStore;
|
||||||
|
|
|
@ -29,6 +29,11 @@ RedisDocumentStore.connect = function(options) {
|
||||||
if (options.password) {
|
if (options.password) {
|
||||||
RedisDocumentStore.client.auth(options.password);
|
RedisDocumentStore.client.auth(options.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RedisDocumentStore.client.on('error', function(err) {
|
||||||
|
winston.error('redis disconnected', err);
|
||||||
|
});
|
||||||
|
|
||||||
RedisDocumentStore.client.select(index, function(err) {
|
RedisDocumentStore.client.select(index, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
winston.error(
|
winston.error(
|
||||||
|
|
|
@ -1,7 +1,15 @@
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const rethink = require('rethinkdbdash');
|
const rethink = require('rethinkdbdash');
|
||||||
|
const winston = require('winston');
|
||||||
|
|
||||||
var RethinkDBStore = (options) => {
|
const md5 = (str) => {
|
||||||
|
const md5sum = crypto.createHash('md5');
|
||||||
|
md5sum.update(str);
|
||||||
|
return md5sum.digest('hex');
|
||||||
|
};
|
||||||
|
|
||||||
|
class RethinkDBStore {
|
||||||
|
constructor(options) {
|
||||||
this.client = rethink({
|
this.client = rethink({
|
||||||
silent: true,
|
silent: true,
|
||||||
host: options.host || '127.0.0.1',
|
host: options.host || '127.0.0.1',
|
||||||
|
@ -10,30 +18,29 @@ var RethinkDBStore = (options) => {
|
||||||
user: options.user || 'admin',
|
user: options.user || 'admin',
|
||||||
password: options.password || ''
|
password: options.password || ''
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
RethinkDBStore.md5 = (str) => {
|
set(key, data, callback) {
|
||||||
const md5sum = crypto.createHash('md5');
|
this.client.table('uploads').insert({ id: md5(key), data: data }).run((error) => {
|
||||||
md5sum.update(str);
|
if (error) {
|
||||||
return md5sum.digest('hex');
|
callback(false);
|
||||||
};
|
winston.error('failed to insert to table', error);
|
||||||
|
return;
|
||||||
RethinkDBStore.prototype.set = (key, data, callback) => {
|
}
|
||||||
try {
|
|
||||||
this.client.table('uploads').insert({ id: RethinkDBStore.md5(key), data: data }).run((error) => {
|
|
||||||
if (error) return callback(false);
|
|
||||||
callback(true);
|
callback(true);
|
||||||
});
|
});
|
||||||
} catch (err) {
|
|
||||||
callback(false);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
RethinkDBStore.prototype.get = (key, callback) => {
|
get(key, callback) {
|
||||||
this.client.table('uploads').get(RethinkDBStore.md5(key)).run((error, result) => {
|
this.client.table('uploads').get(md5(key)).run((error, result) => {
|
||||||
if (error || !result) return callback(false);
|
if (error || !result) {
|
||||||
|
callback(false);
|
||||||
|
if (error) winston.error('failed to insert to table', error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
callback(result.data);
|
callback(result.data);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = RethinkDBStore;
|
module.exports = RethinkDBStore;
|
||||||
|
|
Loading…
Reference in New Issue