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