Skip to content

Instantly share code, notes, and snippets.

@tyrasd
Created June 8, 2021 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyrasd/cfa9bcf9cdbfdd27631aa03bb602d0c2 to your computer and use it in GitHub Desktop.
Save tyrasd/cfa9bcf9cdbfdd27631aa03bb602d0c2 to your computer and use it in GitHub Desktop.
overpass turbo URL shortener
var http = require('http');
var url = require('url');
var redis = require('redis');
var base62 = require('base62');
var db = redis.createClient("/var/run/redis/redis-server.sock");
http.createServer(function (req, res) {
if (req.method == "OPTIONS") {
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'x-requested-with',
});
res.end();
return;
}
var req_url = url.parse(req.url);
if (req_url.pathname == "/create") {
// only allow a whitelist of minify-able pages:
if (!req_url.query.match(/^(\?|index.html\?|map.html\?)/)) {
res.writeHead(403, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': '*',
});
res.end();
} else
db.incr("short_urls_count", function(err, short_urls_count) {
var short_id = base62.encode(short_urls_count);
var short_url = "https://overpass-turbo.eu/s/"+short_id;
db.hset("short_urls", short_id, req_url.query);
res.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin': '*',
});
res.end(short_url);
});
} else {
db.hget("short_urls", req_url.path.substr(1), function(err, short_url) {
res.writeHead(301, {
'Location': 'https://overpass-turbo.eu/'+short_url,
'Access-Control-Allow-Origin': '*',
});
res.end();
});
}
}).listen(8191, '127.0.0.1');
{
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"base62": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/base62/-/base62-2.0.1.tgz",
"integrity": "sha512-4t4WQK7mdbcWzqEBiq6tfo2qDCeIZGXvjifJZyxHIVcjQkZJxpFtu/pa2Va69OouCkg6izZ08hKnPxroeDyzew=="
},
"denque": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz",
"integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ=="
},
"redis": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/redis/-/redis-3.0.2.tgz",
"integrity": "sha512-PNhLCrjU6vKVuMOyFu7oSP296mwBkcE6lrAjruBYG5LgdSqtRBoVQIylrMyVZD/lkF24RSNNatzvYag6HRBHjQ==",
"requires": {
"denque": "^1.4.1",
"redis-commands": "^1.5.0",
"redis-errors": "^1.2.0",
"redis-parser": "^3.0.0"
}
},
"redis-commands": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.6.0.tgz",
"integrity": "sha512-2jnZ0IkjZxvguITjFTrGiLyzQZcTvaw8DAaCXxZq/dsHXz7KfMQ3OUJy7Tz9vnRtZRVz6VRCPDvruvU8Ts44wQ=="
},
"redis-errors": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",
"integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60="
},
"redis-parser": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz",
"integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=",
"requires": {
"redis-errors": "^1.0.0"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment