From 4464409743a08903859340053bff58bd94df0ffd Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 5 Jan 2014 23:31:03 +0100 Subject: go: setenv HOSTN to generate absolute uris --- go/README | 10 ++++++---- go/index.js | 13 +++++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) (limited to 'go') diff --git a/go/README b/go/README index 78f6b195..17deb7d6 100644 --- a/go/README +++ b/go/README @@ -7,16 +7,18 @@ go - minimalistic uri shortener npm install hiredis # run service - PORT=1337 node . + HOSTN=go PORT=80 node . + + if you omit `HOSTN`, then relative shortened uris will be generated. # add uri - curl -F uri=https://mywaytoolonguri http://localhost:1337 + curl -F uri=https://mywaytoolonguri http://go - this will give you a relative shortened uri. + this will give you a shortened uri. # resolve uri - curl -L http://localhost:1337/shortened-relative-uri + curl -L http://go/1 # clear database diff --git a/go/index.js b/go/index.js index 470010a9..95ddd2ef 100644 --- a/go/index.js +++ b/go/index.js @@ -1,5 +1,13 @@ +var hostname = process.env.HOSTN; var httpPort = process.env.PORT || 1337; var redisPrefix = 'go:'; +var uriPrefix = ''; +if (hostname) { + uriPrefix += 'http://' + hostname; + if (httpPort != 80) { + uriPrefix += ':' + httpPort; + } +} var http = require('http'); var formidable = require('formidable'); @@ -41,8 +49,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) { -- cgit v1.2.3 From 1fa549d013c6b1a628fdb7fa3aa7620696e71171 Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 5 Jan 2014 23:52:27 +0100 Subject: go: add code section comments --- go/index.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'go') diff --git a/go/index.js b/go/index.js index 95ddd2ef..16b6df98 100644 --- a/go/index.js +++ b/go/index.js @@ -1,6 +1,10 @@ +// 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; @@ -8,14 +12,20 @@ if (hostname) { 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); @@ -23,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); @@ -33,7 +45,7 @@ function listener (req, res) { return methodNotAllowed(req, res); } } - + function create (req, res) { redisClient.incr(redisPrefix + 'index', function (err, reply) { if (err) { @@ -64,7 +76,7 @@ function create (req, res) { }); }); } - + function retrieve (req, res) { var key = redisPrefix + req.url; redisClient.get(key, function (error, reply) { @@ -84,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'); -- cgit v1.2.3 From 0afc64d121af21e8fa147acfc8419297a0a81d22 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 6 Jan 2014 00:02:01 +0100 Subject: go: README looks like markdown --- go/README | 27 --------------------------- go/README.markdown | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 27 deletions(-) delete mode 100644 go/README create mode 100644 go/README.markdown (limited to 'go') diff --git a/go/README b/go/README deleted file mode 100644 index 17deb7d6..00000000 --- a/go/README +++ /dev/null @@ -1,27 +0,0 @@ -go - minimalistic uri shortener - -# installation - npm install - - optionally - npm install hiredis - -# run service - HOSTN=go PORT=80 node . - - if you omit `HOSTN`, then relative shortened uris will be generated. - -# 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/README.markdown b/go/README.markdown new file mode 100644 index 00000000..17deb7d6 --- /dev/null +++ b/go/README.markdown @@ -0,0 +1,27 @@ +go - minimalistic uri shortener + +# installation + npm install + + optionally + npm install hiredis + +# run service + HOSTN=go PORT=80 node . + + if you omit `HOSTN`, then relative shortened uris will be generated. + +# 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:`. -- cgit v1.2.3 From c6386b16b5fe69abe284bd4c658fe286394e631a Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 6 Jan 2014 00:04:42 +0100 Subject: go: make README look like markdown harder --- go/README.markdown | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'go') diff --git a/go/README.markdown b/go/README.markdown index 17deb7d6..d3088685 100644 --- a/go/README.markdown +++ b/go/README.markdown @@ -1,26 +1,32 @@ -go - minimalistic uri shortener +# go - minimalistic uri shortener + +## install dependencies -# installation npm install - optionally + apparently you can also + npm install hiredis -# run service + for more awesome. + +## run service + HOSTN=go PORT=80 node . if you omit `HOSTN`, then relative shortened uris will be generated. -# add uri +## add uri + curl -F uri=https://mywaytoolonguri http://go this will give you a shortened uri. -# resolve uri +## resolve uri - curl -L http://go/1 + curl -L http://go/1 -# clear database +## clear database redis-cli keys 'go:*' | xargs redis-cli del -- cgit v1.2.3 From eec4484676565be3384fde82f8fcbc71f1fe0ffe Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 6 Jan 2014 00:06:20 +0100 Subject: go: talk about default port --- go/README.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'go') diff --git a/go/README.markdown b/go/README.markdown index d3088685..11a31975 100644 --- a/go/README.markdown +++ b/go/README.markdown @@ -15,6 +15,7 @@ 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 -- cgit v1.2.3