Skip to content

Instantly share code, notes, and snippets.

@sbp
Created January 19, 2010 23:56
Show Gist options
  • Save sbp/281449 to your computer and use it in GitHub Desktop.
Save sbp/281449 to your computer and use it in GitHub Desktop.
/*
* server.js — Ampify Draft Server
* Author: Sean B. Palmer, inamidst.com
* License: Creative Commons Attribution 3.0
*
* Source: http://gist.github.com/281449
* Helper: http://gist.github.com/281380
* Requires: (a) redis, (b) nodejs (c) redis-node-client
*/
var sys = require('sys');
var http = require('http');
var url = require('url');
var multipart = require('multipart');
var redis = require('./redisclient');
function missing(req, res) {
var message = '404 Not Found';
res.sendHeader(404, {'Content-Type': 'text/html'});
res.sendBody('<title>' + message + '</title><p>' + message);
res.finish();
}
function badmethod(req, res) {
var message = '405 Method Not Allowed';
res.sendHeader(405, {'Content-Type': 'text/html'});
res.sendBody('<title>' + message + '</title><p>' + message);
res.finish();
}
var handleGet = {};
function onGet(path, handler) {
handleGet[path] = handler;
}
var handlePost = {};
function onPost(path, handler) {
handlePost[path] = handler;
}
function listener(req, res) {
if (req.method == 'GET' || req.method == 'POST') {
var path = url.parse(req.url).pathname;
switch (req.method) {
case 'GET': var handler = handleGet[path] || missing; break;
case 'POST': var handler = handlePost[path] || missing; break;
}
handler(req, res);
} else { badmethod(); }
}
onGet('/test/counter', function (req, res) {
function counter() {
client.incr('test:counter').addCallback(function (value) {
res.sendHeader(200, {'Content-Type': 'text/plain'});
res.sendBody(value.toString());
res.finish();
client.close();
});
};
var client = new redis.Client();
client.connect(counter);
});
function doc(f) {
var code = String(f);
var i = code.indexOf('\n'), j = code.lastIndexOf('\n');
return code.substring(i + 1, j);
}
var home = function() {/*
<!DOCTYPE html>
<title>Ampify Draft Server</title>
<link rel="stylesheet" href="/site/style">
<h1>Ampify Draft Server</h1>
<p><a href="/site/signup">Signup</a>,
<a href="/test/counter">Counter</a>
<hr>
<p><a href="http://gist.github.com/281449">Source</a>,
<a href="http://inamidst.com/stuff/esp/ampify">About</a>,
<a href="http://github.com/tav/ampify">More</a>
*/}
onGet('/', function (req, res) {
res.sendHeader(200, {'Content-Type': 'text/html'});
res.sendBody(doc(home));
res.finish();
});
var style = function() {/*
html { font: 1em/1.4em sans-serif }
body { margin: 3em }
h1, h2 { font-weight: normal }
*/}
onGet('/site/style', function (req, res) {
res.sendHeader(200, {'Content-Type': 'text/css'});
res.sendBody(doc(style));
res.finish();
});
var signup = function() {/*
<!DOCTYPE html>
<title>Signup for an Account</title>
<link rel="stylesheet" href="/site/style">
<h1>Signup for an Account</h1>
<form method="post" action="/site/signup" enctype="multipart/form-data">
<p>Username: <input name="username" type="text"><br>
Password: <input name="password" type="password"><br>
<input type="submit" value="Create Account">
</form>
<p><a href="/">Home</a>
*/}
onGet('/site/signup', function (req, res) {
res.sendHeader(200, {'Content-Type': 'text/html'});
res.sendBody(doc(signup));
res.finish();
});
onPost('/site/signup', function (req, res) {
multipart.parse(req).addCallback(function (parts) {
var username = parts['username'];
var password = parts['password'];
function createaccount() {
client.incr('global:nextUserId').addCallback(function (uid) {
client.set('uid:' + uid.toString() + ':username', username);
client.set('uid:' + uid.toString() + ':password', password);
client.set('uid:' + username + ':uid', uid);
res.sendHeader(200, {'Content-Type': 'text/plain'});
res.sendBody('Created UID ' + uid.toString());
res.finish();
});
};
var client = new redis.Client();
client.connect(createaccount);
});
});
function serve(port, host) {
var server = http.createServer(listener);
server.listen(port || 8000, host);
var addr = (host || 'localhost') + ':' + port.toString();
sys.puts('Serving at ' + addr);
}
function main(argv) {
var argv = argv || process.ARGV;
var port = argv[2] * 1;
serve(port);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment