summaryrefslogtreecommitdiffstats
path: root/go
diff options
context:
space:
mode:
Diffstat (limited to 'go')
-rw-r--r--go/README15
-rw-r--r--go/index.js83
-rw-r--r--go/package.json9
3 files changed, 107 insertions, 0 deletions
diff --git a/go/README b/go/README
index 1bc2e7b9..0f9de490 100644
--- a/go/README
+++ b/go/README
@@ -1 +1,16 @@
go - minimalistic uri shortener
+
+# installation
+ npm install
+
+ optionally
+ npm install hiredis
+
+# run service
+ node .
+
+# 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
new file mode 100644
index 00000000..723fb78a
--- /dev/null
+++ b/go/index.js
@@ -0,0 +1,83 @@
+var httpPort = process.env.PORT || 1337;
+var redisPrefix = 'go:';
+
+var http = require('http');
+var formidable = require('formidable');
+var redis = require('redis');
+
+var redisClient = redis.createClient();
+var httpServer = http.createServer(listener);
+
+redisClient.on('error', function (err) {
+ console.log('redis made a bubu:', err.message);
+ process.exit(23);
+});
+httpServer.listen(httpPort, function () {
+ console.log('http server listening on port', httpPort);
+});
+
+function listener (req, res) {
+ if (req.method === 'POST' && req.url === '/') {
+ return create(req, res);
+ } else if (req.method === 'GET') {
+ return retrieve(req, res);
+ } else {
+ return methodNotAllowed(req, res);
+ }
+}
+
+function create (req, res) {
+ redisClient.incr(redisPrefix + 'index', function (err, reply) {
+ if (err) {
+ return internalError(err, req, res);
+ }
+
+ var form = new formidable.IncomingForm();
+
+ form.parse(req, function(err, fields, files) {
+ if (err) {
+ return internalError(err, req, res);
+ }
+
+ var uri = fields.uri;
+ // TODO check uri(?)
+ var shortUri = '/' + reply;
+ var key = redisPrefix + shortUri;
+
+ redisClient.set(key, uri, function (error) {
+ if (error) {
+ return internalError(err, req, res);
+ }
+
+ res.writeHead(200, { 'content-type': 'text/plain' });
+ return res.end(shortUri + '\r\n');
+ });
+ });
+ });
+}
+
+function retrieve (req, res) {
+ var key = redisPrefix + req.url;
+ redisClient.get(key, function (error, reply) {
+ if (error) {
+ return internalError(err, req, res);
+ }
+
+ if (typeof reply !== 'string') {
+ res.writeHead(404, { 'content-type': 'text/plain' });
+ return res.end('not found\r\n');
+ }
+
+ res.writeHead(200, { 'content-type': 'text/plain' });
+ return res.end(reply + '\r\n');
+ });
+}
+
+function methodNotAllowed (req, res) {
+ res.writeHead(405, { 'content-type': 'text/plain' });
+ return res.end('method not allowed\r\n');
+}
+function internalError (error, req, res) {
+ res.writeHead(500, { 'content-type': 'text/plain' });
+ return res.end(error.message + '\r\n');
+}
diff --git a/go/package.json b/go/package.json
new file mode 100644
index 00000000..4656b992
--- /dev/null
+++ b/go/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "go",
+ "version": "0.0.0",
+ "description": "minimalistic uri shortener",
+ "dependencies": {
+ "formidable": "*",
+ "redis": "*"
+ }
+}