Golang Web App - Handling Requests

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
by the end of this video you have a full understanding of handling web requests in go you learn the entire life cycle of a request coming into a go web application and eventually getting a response from a goal server some ninjas welcome back to the golang dojo the fastest and growing channel for hitting your programming language make sure to subscribe to the channel and pick up your free yes free go languages at golangdojo.com all right so let's get started in front of us is the most basic hello world web server implemented indigo now if you are not familiar with all of these instructions as shown here make sure to check out my last video where i talked about why i explained the basics of any web applications and more specifically explaining each one of these instructions implemented in a go assuming you have the context of what these instructions are doing and we can go ahead and kick off the golang hello world server by running the command and go run in main.go and we can go ahead and move on to the browser and type in a local host and hit enter we will be able to see the sentence of a hello world as this instruction is doing right here as you can see here we only have a one pattern being handled by the handle function so anything else any other pattern will be falling into the wood's path as well for example if we have localhost slash ninja we'll get the exact same response because anything that is matching the wood path localhost slash ninja being a one of them will gather the response at the same response of the sentence of hello world however what if we want to show something different and we only want the root path to be showing hello world that we want the slash ninja to be showing the ninja maybe it's gonna show my name wallace as the ninja and any other things that it would just show something like a big fat area for example in order to do that we need to inspect the request coming in and more specifically we want to inspect the url that is coming in and even more specific than that we want to inspect the path of the url that is coming in to do that we can go ahead and have a switch case for the request dot url dot pathway here so when it's the case that it is the router path we will go ahead and print out hello world if it's ninja we will go ahead and print out wallace and if we have anything else we will go ahead and print out big that ever so since we have the different handlings now we can also remove this we can go ahead and rerun this main.go program again and i go back into the browser and type in localhost we are seeing hello world as expected however when we type in slash ninja we should be able to see my name of wallace here as implemented right here and if we type in anything else literally anything else right here we should be able to see a big fat error as implemented right here now besides routing it differently depending on what the url path is there are also other information inside of this request object if we go to the implementation we should be able to see that very first one will be different besides the url right here the very first one is actually the methods now if you remember from our last video when we were talking about the basics of any web applications html requests are the most common type of requests and it has the four most common methods being get a post and put as well as a delete so let's go ahead and try to figure out what type of a method this particular request that coming in is using go ahead and try to print out princess handle laying function with the type of the request or dot method go ahead and rerun in the program again and if we go to the browser and type in localhost it says hello world but this is not our focus right now our focus is the print statement that we are getting as you can see this is a get request in the future i will talk more about how you can use go with these http requests and methods when we are trying to implement west apis using the go programming language and the same goes for the other components in the request structs right here as you can see there are many more components in this struct but for now let's move on to the response a wider object right here and talk about the header first the first header that i want to talk about would be the content type and more specifically i want to see the difference between the content type of html versus a plane for that let's go ahead and copy this function signature here and let's go ahead and change this function from handle function to html versus plain now inside of this function we will go ahead and try to print out hello world again to the response writer but we want to add in something slightly different i want to add in a html header like this and then go ahead and replace the handle function as the handler with the html versus plain and also go ahead and try to have a print statement here so that we can make sure at least from our terminal here we will be able to tell it is actually indeed using this html versus a plain handler instead of this handle function right here that we are no longer calling so let's say go ahead and rerun this main.go file and go to the browser and type in localhost again and hit enter this time around we should be able to see the hello world being bolded up so if we go to the inspector we would be able to see that it is returning a html file so if we go to network and refresh this page from the response right here click on raw we are getting the header as well and it is being automatically categorized into a html file if we go back to the terminal we should also be able to see that it is indeed hitting this handler of html versus a plane right here as this is a what is it being printed out now what you don't see here is that the content type header is it being implicitly set to text html you now if we actually try to change it to plain instead and rerun this program and go back to the browser again head refresh we would be able to see that now it is a printing out the entire tags that we typed in here instead of having the browser categorizing that this is a html tag instead and inside of this uh body we will be able to see that h1 is not being categorized as html tag and if we go to the network refresh go to the network even if we are getting the same text in the response body it is being printed out differently because the header is now text slash plain if we go back to the program right here and change it back into html and try to run this program again and go back into the browser and pay attention due to the content type right here now it is a text slash plain if we refresh again we will be able to see that it is now text html and the html tag being h1 right here is it being recognized as a html tag indeed and even if we remove or comment out this line this content type is still being implicitly has said to text html because it is not too hard to figure out for the browser at least that this is intended to be a html tag so if we go ahead and rerun the program and then go to the browser head of refresh and go to network refresh again and see the content type as you can see it is being defaulted or implicitly said to text html now if we uncomment out this line of instruction and go to the implementation of header right here and go to the implementation of header we'll be able to see that the header is of a type map which means the name of the header is going to be of a type string and the value of this header is going to be of a type slice of a string and these are the example headers that you can set to the responses all right next i want to talk about the server configurations if you remember from our last video we have gone to the implementation we have checked out the implementation of a listen and serve so let's go ahead and do that again and as you can see we are creating a new instance of the server right here and a calling listen and search and this is good but the problem is that or one limitation is that when we are calling the listen and serve a function that comes with hdb library is that we cannot do much modification to the server object right here so if we go to the implementation of the server struct we will be able to see that there are a lot more configurations such as with a timeout and a white timeout that we can potentially set ourselves so let's now try to say that these are configurations to our own liking for that let's go ahead and comment this line out we'll create our own instance of the server like this go ahead and use the same address actually empty just like we have it right here at least for now we will have the same handler as new as we have it right here and what's going to be different is the reader timeout we'll set it to one a second we'll do a white timeout as well set it to one second also don't forget to call the listen and serve a function if we go back to the implementation of http listen and serve this is exactly what we just did we are creating a new instance of the server and calling this a listen and surface function except that we have additional configurations right here so count this line back out and create a different handler so we can test out the timeouts right here so it's called the new handler timeout and give it a different path as well for easier testing we will go ahead and implement our new handler function as soon as this function this handler function is called we will go ahead and print out timeout attempt and the actual timeout logic would be trying to sleep for two seconds since we have it implemented here that the timeouts are going to be one a second so this should be it sufficient to cause a timeout we're also going to try to write it to the response writer saying that unfortunately the timeout did not happen so hopefully this line of instruction will never be successfully executed because it will cause a timeout before we can perform this right to the response writer to the browser all right so let's go ahead and run this program again go running main.go and then go to the browser type in localhost time out and go ahead and hit enter we should be able to see that it is going to keep on running for a few more seconds here and then eventually it should say that the connection was a reset and that is because there was a timeout that occurred and as you can see the browser is pretty smart it actually tried to call the handler function the timeout handler function multiple times and that is why we are getting this and printed out right here good to know that the firefox browser knows how to retry after there had been a timeout but unfortunately because of the the configurations that we have right here the white to the response writer never occurred so that is a server configurations okay so one more thing i want to talk about which is multihiplexer by the way in the series of a golang web app i'm going to be calling multiplexer router and handler as if they're more or less the same thing and also i'm going to be creating a standalone video on the topic of a multiplexers because it can be more complicated so make sure to subscribe to the channel for that standalone video in the future on the multiplexers but for now would you stick with the basics of a multiplexer in this video if we go back to the implementation of a listen and serve we should be able to see that when we are passing in a new handler or a neo multiplexer or server mugs it gets passed in all the way and when the handler is set to neo we are actually using the default software mark's right here which is a multiplexer and this is just a instance of the surfmux right here and if we go all the way back into the main function again and i go to where we are associating the path with a handler function go to the implementation of handle func we're actually setting all of the path association to the default software max as well which is the exact same instance of the surface which means we can actually create our own handler our own multiplexer so we can create our own instance of the mugs let's call it ninja mode actually and http dot server mux now we can go ahead and configure our server to update its handler to be the max ninja mode actually it's going to be the uh the pointer to the marks in ninja mode and then have the function listen and serve a call after we've configured the server with this pointer to this mugs in a ninja mode right here one last thing that we need to do before we call the listening search function on the server here is to make sure this multiplexer has some type of a routing in order to do that we need to go ahead and call the handle func function and give it a routing to the boots pad actually the hollow world ninja mode which i have already implemented so if we go to the implementation we will go ahead and print out the name of the handler function and then we will go ahead and write same thing hello world except that now it is in night time where ninjas would go out and do their missions being great also something that we need to do is we need to get rid of the default multiplexer taken into effect right here because if we call the listen and serve the server with the handler or the multiplexer being new will be kickstarted so that's not what we want because we have a new multiplexer and after that is a configured to be the handler to be the multiplexer this is when we want to listen and serve instead of all the way up here let's go ahead and run this program again go to the browser and type in a local host and hit enter and we can see hollow being printed out and the background is nice and dark which is perfect for ninja missions that's it for today's video for handling requests in a go web server if you have any questions make sure to leave them in the comment section down below for the time being make sure to subscribe to the channel and pick up your free girl and cheat sheet at the golangdojo.com and i'll see you ninjas in the next video [Music]
Info
Channel: Golang Dojo
Views: 1,805
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, golang in 2021, go language, golang tutorial, go programming, golang for beginners, Golang Web App Handling Requests, golang web app, golang web application, golang web app tutorial, web app in go, web application in golang tutorial, web application in go, golang request, golang response, go web app, web development in golang, web development in go tutorial, golang http, web development, web app basics in go, handling request in go
Id: DiZuxLbpkGg
Channel Id: undefined
Length: 17min 17sec (1037 seconds)
Published: Fri Oct 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.