mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-01 03:21:21 +00:00
Added static document loading on start
This commit is contained in:
parent
32b4f842b7
commit
e403549e13
5 changed files with 39 additions and 12 deletions
2
TODO
2
TODO
|
@ -2,6 +2,8 @@ cache headers for static assets (and on document GET)
|
||||||
tests
|
tests
|
||||||
add feedback for errors to UI - esp. too long
|
add feedback for errors to UI - esp. too long
|
||||||
add about page
|
add about page
|
||||||
|
fix that chrome bug where it loads the doc twice
|
||||||
|
add link to about page
|
||||||
|
|
||||||
# shared version only
|
# shared version only
|
||||||
some way to do announcements easily (and use for ads)
|
some way to do announcements easily (and use for ads)
|
||||||
|
|
5
about.md
Normal file
5
about.md
Normal 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.
|
|
@ -23,6 +23,10 @@
|
||||||
"port": 6379,
|
"port": 6379,
|
||||||
"db": 2,
|
"db": 2,
|
||||||
"expire": 3600
|
"expire": 3600
|
||||||
|
},
|
||||||
|
|
||||||
|
"documents": {
|
||||||
|
"about": "./about.md"
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,14 +34,16 @@ RedisDocumentStore.connect = function(options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Save file in a key
|
// Save file in a key
|
||||||
RedisDocumentStore.prototype.set = function(key, data, callback) {
|
RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
RedisDocumentStore.client.set(key, data, function(err, reply) {
|
RedisDocumentStore.client.set(key, data, function(err, reply) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(false);
|
callback(false);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
_this.setExpiration(key);
|
if (!skipExpire) {
|
||||||
|
_this.setExpiration(key);
|
||||||
|
}
|
||||||
callback(true);
|
callback(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
34
server.js
34
server.js
|
@ -37,29 +37,43 @@ if (!config.storage.type) {
|
||||||
var Store = require('./lib/' + config.storage.type + '_document_store');
|
var Store = require('./lib/' + config.storage.type + '_document_store');
|
||||||
var preferredStore = new Store(config.storage);
|
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
|
// Configure a static handler for the static files
|
||||||
var staticHandler = new StaticHandler('./static', !!config.cacheStaticAssets);
|
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
|
// Set the server up and listen forever
|
||||||
http.createServer(function(request, response) {
|
http.createServer(function(request, response) {
|
||||||
var incoming = url.parse(request.url, false);
|
var incoming = url.parse(request.url, false);
|
||||||
var handler = null;
|
var handler = null;
|
||||||
// Looking to add a new doc
|
// Looking to add a new doc
|
||||||
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
|
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
|
||||||
handler = new DocumentHandler({
|
return documentHandler.handlePost(request, response);
|
||||||
keyLength: config.keyLength,
|
|
||||||
maxLength: config.maxLength,
|
|
||||||
store: preferredStore
|
|
||||||
});
|
|
||||||
return handler.handlePost(request, response);
|
|
||||||
}
|
}
|
||||||
// Looking up a doc
|
// Looking up a doc
|
||||||
var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/);
|
var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/);
|
||||||
if (request.method == 'GET' && match) {
|
if (request.method == 'GET' && match) {
|
||||||
handler = new DocumentHandler({
|
return documentHandler.handleGet(match[1], response);
|
||||||
store: preferredStore
|
|
||||||
});
|
|
||||||
return handler.handleGet(match[1], response);
|
|
||||||
}
|
}
|
||||||
// Otherwise, look for static file
|
// Otherwise, look for static file
|
||||||
staticHandler.handle(incoming.pathname, response);
|
staticHandler.handle(incoming.pathname, response);
|
||||||
|
|
Loading…
Reference in a new issue