summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlassulus <lassulus@googlemail.com>2014-01-06 00:09:21 +0100
committerlassulus <lassulus@googlemail.com>2014-01-06 00:09:21 +0100
commitb62891213655ecae9f8abf859d571b8730460d62 (patch)
treef6fa7273ce66b43486d6dbaae09a9e56b89bb700
parentf98847bb46197e2872705abf9278fda26895d75d (diff)
parenteec4484676565be3384fde82f8fcbc71f1fe0ffe (diff)
Merge branch 'master' of github.com:krebscode/painload
-rw-r--r--go/README25
-rw-r--r--go/README.markdown34
-rw-r--r--go/index.js39
3 files changed, 64 insertions, 34 deletions
diff --git a/go/README b/go/README
deleted file mode 100644
index 78f6b195..00000000
--- a/go/README
+++ /dev/null
@@ -1,25 +0,0 @@
-go - minimalistic uri shortener
-
-# installation
- npm install
-
- optionally
- npm install hiredis
-
-# run service
- PORT=1337 node .
-
-# add uri
- curl -F uri=https://mywaytoolonguri http://localhost:1337
-
- this will give you a relative shortened uri.
-
-# resolve uri
-
- curl -L http://localhost:1337/shortened-relative-uri
-
-# clear database
-
- redis-cli keys 'go:*' | xargs redis-cli del
-
- if you have changed `redisPrefix`, then use that instead of `go:`.
diff --git a/go/README.markdown b/go/README.markdown
new file mode 100644
index 00000000..11a31975
--- /dev/null
+++ b/go/README.markdown
@@ -0,0 +1,34 @@
+# go - minimalistic uri shortener
+
+## install dependencies
+
+ npm install
+
+ apparently you can also
+
+ npm install hiredis
+
+ for more awesome.
+
+## run service
+
+ HOSTN=go PORT=80 node .
+
+ if you omit `HOSTN`, then relative shortened uris will be generated.
+ if you omit `PORT`, then it's `1337`.
+
+## add uri
+
+ curl -F uri=https://mywaytoolonguri http://go
+
+ this will give you a shortened uri.
+
+## resolve uri
+
+ curl -L http://go/1
+
+## clear database
+
+ redis-cli keys 'go:*' | xargs redis-cli del
+
+ if you have changed `redisPrefix`, then use that instead of `go:`.
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');