I expect there to be one POST endpoint that does the url shortening. This endpoint takes in the actual url to be shortened, and returns the shortened version of the url.
Request
{
url: my-long-url.com/lets-pretend-this-is-super-long
}
Response:
{
short_param: tinyurl.com/short-url
long_url: my-long-url.com/lets-pretend-this-is-super-long
}
We would also have a GET endpoint that uses the path param for returning back the original URL or nothing if none exists.
GET tinyurl.com/short-url
response:
{
long_url: my-long-url.com/lets-pretend-this-is-super-long
}
For simplicity we will assume that all shortened urls will have the same domain name, which is our websites domain.
I imagine the system would work like this. A user visits our URL shortener page with the intent of shortening their URL. They paste their URL into an input field, click a shortening button, and that button makes our first POST request to convert the URL. On success, we get our new shortened URL. When a user goes to this shortened URL, they can visit the original page as if they had used the long URL.
When a user clicks on the shortening button, that button triggers our first POST request to convert the URL. On the BE, we receive the long URL. We can use some kind of hashing function to generate a string that will be saved in our DB alongside the long URL. Each row will have its long URL with its corresponding hash. We only need to store the hash since we are assuming that our domain names will be static, so we don't need to repetively store the domain name. We can have an index on each column since we expect to be reading from both columns frequently, but only on the hash is fine as well, since we'd probably be reading more from it anyway.
On success of storing the long url with its shortened hash, we return a response with the long URL and domain-name + hash.
Then, we would have route for our GET endpoint that would look like our-domain-name.com/:hash. This endpoint will take the hash portionof the URL and use it to look up the long URL. This will be returned to the FE, which will then use it to perform a redirect to the original page.