Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
janfejtek committed Oct 28, 2017
1 parent 710d4c1 commit 7da78b1
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 18 deletions.
100 changes: 83 additions & 17 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,111 @@ var app = express();
var mongoClient = mongodb.MongoClient;
var mongodburl = "mongodb://" + process.env.MONGO_NAME + ":" + process.env.MONGO_PASSWORD + "@ds229415.mlab.com:29415/" + process.env.MONGO_DBNAME;

const SHORT_LENGTH = 3;


function randomBase64() {
var random = Math.floor(Math.random() * 64);
if (random <= 25)
return String.fromCharCode(97 + random);
if (random <= 51)
return String.fromCharCode(65 - 26 + random);
if (random <= 61)
return ""+(random- 52);
switch(random) {
case 62:
return "-";
case 63:
return "_";
default:
return "=";
}
}

function generateRandomId(length) {
var generated = "";
for (var i = 0; i< length; i++) {
generated += randomBase64();
}
return generated;
}

app.get("/", function (request, response) {
response.sendFile(__dirname + '/views/index.html');
});

app.get("/shortener/*", function (request, response) {

var url = request.params[0];
if (!url.startsWith("http://www.") && !url.startsWith("https://www.")) {
response.send({error: "Wrong format"})
}
else {
var id = null;

mongoClient.connect(mongodburl, function(err, db) {
if (err)
throw err;
id = "X05"
mongodb.urls.insert({ "id": id, "url" : "bar" })
db.close();
})

if (id != null) {
response.send({short_url: "https://fertile-tub.glitch.me/"+id});

}
else {
response.send({error: "Internal error"})
}
var urlsCollection = db.collection('urls');


urlsCollection.find({"url": url}).toArray(function(err, documents) {
if (err)
throw err;
var id = null;
if (documents.length == 0) {
do {
id = generateRandomId(SHORT_LENGTH);
}
while( urlsCollection.find({"id": id}).toArray(function(err, documents) {
if (documents.length > 0)
return false;
else
return true;
}));



db.collection('urls').insert({ "id": id, "url" : url }, function(err, data) {
if (err)
throw err;
response.send({short_url: "https://fertile-tub.glitch.me/"+id});
})

}
else {
response.send({short_url: "https://fertile-tub.glitch.me/"+documents[0].id})
}
})



})
}

});

app.get("/:value", function(request, response) {
var id = request.params.value;
mongoClient.connect(mongodburl, function(err, db) {
if (err)
throw err;
var urlsCollection = db.collection('urls');

urlsCollection.find({"id": id}).toArray(function(err, documents) {
if (err)
throw err;
if (documents.length === 0) {
response.send({error: "Invalid id"})

}
else {
response.redirect(documents[0].url)
}
})

})

// get from mongo server


// if not found
response.send({error: "Invalid id"})
})

// listen for requests :)
Expand Down
19 changes: 18 additions & 1 deletion views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h2>
If I pass an invalid URL that doesn't follow the valid http://www.example.com format, the JSON response will contain an error instead.
</li>
<li>
User Story: When I visit that shortened URL, it will redirect me to my original link.
When I visit that shortened URL, it will redirect me to my original link.
</li>
</ul>
<h2>
Expand All @@ -38,7 +38,24 @@ <h2>
<ul>
<li>
<a href="https://fertile-tub.glitch.me/shortener/https://www.google.com">https://fertile-tub.glitch.me/shortener/https://www.google.com</a>
<p>
Example response: {"short_url":"https://fertile-tub.glitch.me/djy"}
</p>
<p>
Error response: {error: <i>Error message</i>}
</p>
</li>
</ul>
<h2>
Notes:
</h2>
<ul>
<li>
Each url is shortened only once - multiple submits of the same site result in same short URL.
</li>
<li>
Id of the url has length of 3 characters, IDs are base64 encoded.
</li>
</ul>

<!-- Your web-app is https, so your scripts need to be too -->
Expand Down

0 comments on commit 7da78b1

Please sign in to comment.