Build a URL shortener with Go and Chi like TINYURL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to build a URL shortener like a tiny URL with go and Chi as a sttp router so this is going to be a very basic a URL shortener service create a short alas URL of the long URL so whenever you are hitting a short URL it redirects to the long URL so in simple words you can consider like a short URL is like a pointer which points to a long URL so we are going to create a very basic URL short note so what it will do is like it will going to have two apis one for post request which will generate a short URL and another is for get request which will redirect the whatever tiny URL we have created the functionality is very basic so we'll have a generate API when it will receive a request to convert a long URL to a short URL it will create a key first it will generate a key and then it will map that key with this long URL and it will save it in the database and it will return and it will return a short URL with with key in it when someone hits this short URL it will first extract the key from the URL then it will fetch the long URL from the database based on the key and then it will redirect with 302 status code at the end of the video we'll go through this system architecture which will explain like if you are going for a production use case what are the few things you have to understand and you have to cover before going to the production with that note let's start so first we are going to initialize the module use go mod in it and the module name so we are going to use URL shortener and this will initialize the module and in this particular module we are going to save all the dependency which we are going to use so we are going to use two dependencies the first one is the chi and another one is this short uu ID which we are going to use for generating the key let's first add the chi so we have to use goget and then github.com GOI V5 and this will install the chi V V5 the next dependency is short uu ID so it is also based on the Google uu ID after generating A New U ID from Google U ID it just trim it to make it a shorter so now our dependencies are installed so let's start with the mean. go so create a file called mean. go we are going to use a map to hold all our key and uh URL mapping let's create a type mapper and this going to have two Fields one is mapping and it going to be have a map of a string Comm string it will store the key and the original URL key value pair second field going to be a lock of sync. mutex type this lock will prevent from deadlock kind of situation if there are two requests which is trying to access the same map then there is a a possibility like it can lead to a deadlock situation to prevent it whenever one request is try to access the mapping we will log log the mapping and once it will serve it then we will unlock it so that the another request can access it in the global variable we will keep all this mapping URL mapper of type mapper and to initiate this mapping we'll use the init function now let's first create a basic server using Chi so for that funk main create a new router using chi. new router let's add a simple router to test it so for that we are going to use R do get uh get request and it going to be of uh simple slash and it going to have one function in the function the first field is called response writer so the response we are going to write using the response writer and the second field is the request which is of pointer type in this request we will receive if there is any field in the request either it can be in the query or it is in the body we are going to respond back with a server up or like uh it is working kind of of a message w. write going to be a bite type server is running and let's start the server on 3,000 Port HTTP do listen and serve and it's going to listen on the 3,000 port number using colon 3000 and it going to serve this router okay so let's just check okay cor. and let's just see like calll Local Host colon 3000 and yeah server is running so our server is working now let's just add the post request for creating the short URL and a get request to redirect it so let's first create the create short URL Handler so we need a create short URL Handler and it going to receive the response writer and request to extract the data from form use r. par form and to get the URL field use r. form doget and the key now we'll just check if uh if request is not empty a kind of thing so for that we'll just simply add a validation like if U or URL is equals to equals to empty then simply return it with a with a status code B request so for that we are going to use HTTP do status bet request and we are going to send one more message with it so that going to be like a URL field is empty and let's just create a post request now so r. post and it going to be like a short it it going to handle the create short URL Handler now the next part is like we need a key so that we can map it to the original URL so for that we will just simply use generate key okay and and for that we are going to use the short u ID which we have installed first so it going to be like key equals to short u id. new okay now we have to create a mapping function okay so for that we are going to use like insert mapping okay and it going to receive the key and the URL so before writing it we have to log the state so that we'll not end up in the deadlock kind of situation so to lock it simply use URL mapper do loock okay do lock it will lock that particular variable and then a differ statement so that once we'll complete this function or when this function will complete it will unlock the state URL mapper dolog do unlock and we just have to Simply add the mapping in it so for that URL mapper do mapping the key and the URL simple okay let's just call the insert insert mapping and the key and the URL on successful insertion let's just log it and then we'll just simply return the new URL so to log we simply use the logger package uh print Ln log URL mapped successfully okay and we have to send the status c 2 200 so for that simply write header HTTP do status okay okay and we have to return a new URL or short URL which which the user can use to format it we are going to use Sprint T okay so now when anyone will go to this particular URL it will redirect it now create short URL Handler is done so let's just add one more route which can redirect it so we need one R.G and the route name going to be short and we need a redirect Handler so let's just Define the redirect Handler to redirect it properly we first have to extract the key from the URL so for that as we know like we are adding the key after this sh okay so what we are going to do we will have a key field here in this way and to access this particular key we are going to use the ch. URL param g. URL param and the key so here key is the keyword which we have added here so if you'll add it here as ID then we have to use the ID then here also we'll just use the basic check uh the same thing okay so simply key should not be empty okay and key field is empty and we'll simply return the error then we have to fetch the mapping from the URL mapper so for that we'll simply use a function called Fetch mapping first we are going to log the state we'll read it and once our read is complete then we'll unlock it so that the other requests can again pick it up so URL mapper do locklock and the differ statement to unlock it and we are going to Simply return the key URL mapper do mapping so whatever the value of the key we'll just return it okay so here also we are going to have a simple basic check if if key does exist or not okay so first we'll just fetch it fetch mapping for the key and a simple check if our original URL is empty or not so there is possibility like if the if this particular thing doesn't exist or deleted so it is just for the validation purpose and once we have all the thing everything is working then we just have to redirect it so for the redirection we have to use the HTTP do redirect and we have to pass the writer response writer the request and the URL on to which we want to redirect so it's going to be you and the status code with which we want to redirect so generally whenever you are using the redirect so always use the 301 or 302 so for 302 we have HTTP do status found our URL shortener is kind of completed let's just add one more thing called middleware comes with a lot of middle wees with it and we are going to use a middleware called logger whenever we are making any request like a post or get it will log that particular request and it will also tell how much time that particular request has taken so to add it r. use and then the middleware package so you have to select the gv5 middleware okay and then just use the logger so Chi comes with lot of middle Bears so we'll talk about those thing in future so before we start we have have a bug in our code there is a validation we have to return it from that point only else it will continue and it will be like a false positive in all the checks one here okay and then one in the redirect Handler return and one for the URL so now let's just test our apis I'm going to use the postman extension under the extension you can search for Postman and you can install it so this is the extension which I'm going to use for the API testing let's just start our [Music] Server create a new HTTP request so we are going to use the post and we are going to Simply use the shorted so we are going to check first the validation things so if we just send it so we can see like it is a HTTP 400 bad request status code and our error is like URL field is empty so inside body we have to select the W form URL encoded and under the keys write the key as URL and value we are going to give simply https google.com okay and if we now run it we can see we got a short URL so let's just uh test it so just copy it and open it in the browser paste the URL and just hit enter so we can see like it now redirected to google.com and if we see the inspect also also under Network tab we'll see it again so let's just try again okay and if you can see here see it is a status C 302 found and in our logs we can see all those things are coming this get request was hit couple of times okay and how much time it taken and everything addition to the short url url shorten service provides features like tracking number of clicks custom URL and user interaction with the link so as we can see we clicked this particular link uh Local Host this particular get request multiple times so what are URL shortener will do is like it will keep incrementing the account so that it will understand okay this particular link is clicked how many times so this is for the analytics purpose so what we built is a very basic in nature so if you want to expand it so these are the this is a very basic uh high level system design you can do and there are a few articles you can read about it to expand it much more so what we built is very basic in to go to the next level or to make it Advanced so what are the things you can do is like to make your application resilient or robust in nature so what you have to do is like you have to create multiple instances of your server put a load balance in front of it so that all the load which you which is coming to your application it will be distributed in then you have to work on your the URL generator algorithm like the key which you are going to generate is it going to be unique and what is going to be the length and everything because if you're using the six character length seven character length to eight character length accordingly your number of combination will increase or decrease and you have to understand like how many keys you're going to save the uni key generator will come at a some price like what algorithm you using is it going to be like a uu ID which we have used so it will use lot of memory and it will create a very big key having a Long Key means like more storage in the database which results in like more and more GBS of data you will keep storing the next thing is cache so you have to Cache the most used URLs it will make your get request very fast and reduce the response time you also have to think about like what policy you are going to use for the cash like once the cash is full what keys it should keep and what it should delete least recently used policy is best for this particular use Keys then you also have to consider the DB cluster so the DB must be in a cluster mode so that it will always be available and with respect to analytics what you can do is like uh whenever a URL is getting hit okay and when you are redirecting you to 302 so during that time you can push this particular data in the queue and then this particular queue can be consumed by a consumer and that particular consumer will then pull the data and put it in the database so in that way uh it might not be the real time but it will be more accurate so this is the basic URL shortened architecture uh and if you want to see the complete video on it and the and with all this load balancer cach and everything in it so let me know I'll try to create the complete video on it I hope you like this lecture and I will see you in the next one
Info
Channel: practicego
Views: 1,807
Rating: undefined out of 5
Keywords: golang, golang tutorial, learn golang, golang for beginners, golang api, gorilla mux, mongodb, web development, backend development, rest api, api development, golang web app, todo list api, golang crud operations, mongodb golang, gorilla mux tutorial, deploy golang app, heroku deployment, coding, programming, software development, tech, tutorial, beginner, fullstack, new, freecodecamp, hactoberfest, golangbridge, testing, golang-testing, goroutine, interview, interviewquestion, 2024
Id: d3TcCeFNz48
Channel Id: undefined
Length: 17min 33sec (1053 seconds)
Published: Mon Feb 19 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.