1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-12-25 03:26:59 +00:00

Added static document loading on start

This commit is contained in:
John Crepezzi 2011-11-22 09:22:37 -05:00
parent 32b4f842b7
commit e403549e13
5 changed files with 39 additions and 12 deletions

2
TODO
View file

@ -2,6 +2,8 @@ cache headers for static assets (and on document GET)
tests
add feedback for errors to UI - esp. too long
add about page
fix that chrome bug where it loads the doc twice
add link to about page
# shared version only
some way to do announcements easily (and use for ads)

5
about.md Normal file
View file

@ -0,0 +1,5 @@
# Haste
Sharing code is a good thing, and it should be _really_ easy to do it. A lot of times, I want to show you something I'm seeing - and that's where we use pastebins.
Haste is the prettiest, easist to use pastebin every made.

View file

@ -23,6 +23,10 @@
"port": 6379,
"db": 2,
"expire": 3600
},
"documents": {
"about": "./about.md"
}
}

View file

@ -34,14 +34,16 @@ RedisDocumentStore.connect = function(options) {
};
// Save file in a key
RedisDocumentStore.prototype.set = function(key, data, callback) {
RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
var _this = this;
RedisDocumentStore.client.set(key, data, function(err, reply) {
if (err) {
callback(false);
}
else {
_this.setExpiration(key);
if (!skipExpire) {
_this.setExpiration(key);
}
callback(true);
}
});

View file

@ -37,29 +37,43 @@ if (!config.storage.type) {
var Store = require('./lib/' + config.storage.type + '_document_store');
var preferredStore = new Store(config.storage);
// Send the static documents into the preferred store, skipping expirations
for (var name in config.documents) {
var path = config.documents[name];
fs.readFile(path, 'utf8', function(err, data) {
if (data && !err) {
preferredStore.set(name, data, function(cb) {
winston.info('loaded static document', { name: name, path: path });
}, true);
}
else {
winston.warn('failed to load static document', { name: name, path: path });
}
});
}
// Configure a static handler for the static files
var staticHandler = new StaticHandler('./static', !!config.cacheStaticAssets);
// Configure the document handler
var documentHandler = new DocumentHandler({
store: preferredStore,
maxLength: config.maxLength,
keyLength: config.keyLength
});
// Set the server up and listen forever
http.createServer(function(request, response) {
var incoming = url.parse(request.url, false);
var handler = null;
// Looking to add a new doc
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
handler = new DocumentHandler({
keyLength: config.keyLength,
maxLength: config.maxLength,
store: preferredStore
});
return handler.handlePost(request, response);
return documentHandler.handlePost(request, response);
}
// Looking up a doc
var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/);
if (request.method == 'GET' && match) {
handler = new DocumentHandler({
store: preferredStore
});
return handler.handleGet(match[1], response);
return documentHandler.handleGet(match[1], response);
}
// Otherwise, look for static file
staticHandler.handle(incoming.pathname, response);