diff options
Diffstat (limited to 'go/index.js')
-rw-r--r-- | go/index.js | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/go/index.js b/go/index.js index 470010a9..16b6df98 100644 --- a/go/index.js +++ b/go/index.js @@ -1,13 +1,31 @@ +// configuration (and defaults) +var hostname = process.env.HOSTN; var httpPort = process.env.PORT || 1337; var redisPrefix = 'go:'; - + + +// automatic configuration +var uriPrefix = ''; +if (hostname) { + uriPrefix += 'http://' + hostname; + if (httpPort != 80) { + uriPrefix += ':' + httpPort; + } +} + + +// load libraries var http = require('http'); var formidable = require('formidable'); var redis = require('redis'); - + + +// instantiate components var redisClient = redis.createClient(); var httpServer = http.createServer(listener); - + + +// setup compoments redisClient.on('error', function (err) { console.log('redis made a bubu:', err.message); process.exit(23); @@ -15,7 +33,9 @@ redisClient.on('error', function (err) { httpServer.listen(httpPort, function () { console.log('http server listening on port', httpPort); }); - + + +// http handler function listener (req, res) { if (req.method === 'POST' && req.url === '/') { return create(req, res); @@ -25,7 +45,7 @@ function listener (req, res) { return methodNotAllowed(req, res); } } - + function create (req, res) { redisClient.incr(redisPrefix + 'index', function (err, reply) { if (err) { @@ -41,8 +61,9 @@ function create (req, res) { var uri = fields.uri; // TODO check uri(?) - var shortUri = '/' + reply; - var key = redisPrefix + shortUri; + var shortPath = '/' + reply; + var shortUri = uriPrefix + shortPath; + var key = redisPrefix + shortPath; redisClient.set(key, uri, function (error) { if (error) { @@ -55,7 +76,7 @@ function create (req, res) { }); }); } - + function retrieve (req, res) { var key = redisPrefix + req.url; redisClient.get(key, function (error, reply) { @@ -75,7 +96,7 @@ function retrieve (req, res) { return res.end(); }); } - + function methodNotAllowed (req, res) { res.writeHead(405, { 'content-type': 'text/plain' }); return res.end('method not allowed\r\n'); |