FCC Speedrun #4 URL-Shortener
End Product
Deployed app — https://elixir-url-shortener.herokuapp.com/
Recap
For those who are new to FCC, this is the project’s objective.
User’s can create short urls like making such a request -
https://elixir-url-shortener.herokuapp.com/new/https://www.google.com
They get this response -
{
“original_url”: ”https://www.google.com",
“short_url”:”https://elixir-url-shortener.herokuapp.com/8170"
}
Now when they make this request -
https://elixir-url-shortener.herokuapp.com/8170
They are redirected to -
https://www.google.com
Coding Experience
This was my first Phoenix app using a Database. The database used was postgresql and the DSL was Ecto.
I was expecting that it would be tough to use a database in this application given that I am still new to the language and the framework, but I was pleasantly surprised to find out it was not that difficult. Of course part of the reason is the Udemy course by Stephen Grider. He explains the concepts pretty effectively and shows you real code examples making it much simpler for you to follow along and write the code.
Subproblems in this project.
Subproblem 1 —
How do I validate a url. Luckily I did not have to look very far. I found the erlang’s http_uri.parse
function and I also found this blog using it in an example.
Subproblem 2 —
How do I send an error response code (400) in my case. It certainly took me longer to figure this one out than it should have. The reason was I got buried in too much documentation. But the answer was surprisingly simple.
Just like for anything else you call a function.
put_status(conn, 400)
There you go, you just send a 400 response to the client.
Subproblem 3 —
Why do I get Internal Server error
when I post a request with too long url id, like so —
https://elixir-url-shortener.herokuapp.com/238749827347273423948723
Turns out postgres has a limit on the max id (“ 2147483647 ”). So we need to reject requests asking for a number higher than that.
All in all, I had a pleasant experience coding this. I hope you had a pleasant experience reading this too.