since goang 122 released you can now start building your API services with just the net HTTP package without the need for any external packages like realx fiber she un name it so in this video I'm going to be showing you how I'm going to be building my apis from now on since go 122 we're going to go over some of the advanced features that might be used in some of the other packages such as getting path parameters from your handers building and using middle Wares the cative methods without a need for those n the IFL statements that you need to do in the net HTP package and how to do sub rting as well now I'm not advocating that you shouldn't use any other external routers I'm suggesting is the strategy to go without dependencies until I have a clear reason to add one now this has worked for me before and this is a bit off topic but how do you know when should you add the package in a project and for me it boils down to two questions which is do I or my team have the resources or time to implement that by ourselves and do I want to maintain this feature implementation in the future so with that in mind let's test if we can really replace the other routers with the net HTTP package so I'm going to try to build this in a way that if in the future I want to replace my routing I can do it easily so the first thing I'm going to do is going to create a file called api. go and here is going to leave the implementation for the API server so I'm going to create a type called API server and this is is going to be a stct which can hold the address for example if you have any other dependencies for example database you could add it here I'm going to also create a new Constructor for this which is going to return the API server pointers and let's just return it like so and here we're going to take the address as a parameter and we're going to set it like so so here on the main we can actually go ahead and create our server and pass in the address and now we need to run our server I'm going to call this run let's go ahead and create our run method and it's going to return an error so here let's use the hnet HTTP router so it's going to be HTTP do new serve Max let me just create a new server here HTTP do server and I'm going to pass in here the address and the Handler is going to be the router actually here needs to be add the end we can just do server do listen and serve then finally I'm going to add here a log so we know when the server has started all right so let's create for example a Handler it's going to be slash users slash user ID so this is the path parameter I'm going to show you how we can now access these parameters in the endpoints so what we we can do now to get the user ID is that you can go here to the request and get the path do value you can get the name of the Val that you wrote so user ID and then let's quickly write this to the outputs response and if you check the documentation there are here some special cases for example if you spe if you use this special wild card it only matches the end of the URL for example this pattern and there is a lot more here but the basic idea really is that you can select the path value like so so so let's try this out so I'm going to run this server and in another tab I'm going to curl this so if I hit enter we should get the user ID one 2 three and if we try with the different user ID you should get that user ID as well so this is working now we just used a get request and another thing that is new is the declarative methods that you can add here on the URL so before you would need to do something like this so let's imagine this was your Handler so what you need to do before with the net HTTP is that you need to manually check the method and then use the appropriate Handler requests but now what we can do is if you check the documentation is that the pattern is something like this so it's going to be the method a white space and then the host and the path we already have this part so what we can do here is add for example the get a post you could also do delete and all of those methods so this is going to be a get actually let's make this a post just to show show you that this is going to be a get so if you do the example before this is going to fail because this method does not exist but if we change this two gets and as you can see we get the user ID so this is in my opinion a very very good addition to the library and I should also mention if you didn't read here the documentation that if you don't specify the method is going to act as a catch all so for example in this case here I have decided to make this a delete and then here below I have made this cetch all so it doesn't have a method here and if we run the server again you can see that we are landing at the catsh because we're doing a get request if we did a post it would be the same thing also note that any additional spaces after the method is going to make this unusable so if we try again and make get requests we can see that we get the 404 because the package probably doesn't understand so we need to make sure that we have just one space here on the the Commendation to even add this white space so is to be clear and hopefully that saves you some hours from the Bing now what about middlewares and to be honest I'm not the biggest fan of middlewares I like to keep things more declarative and maybe because of my War histories with debugging expressjs applications that made me dislike it even more but they can definitely still be useful when used sparingly for example simple loggers or uh require authentification middleware might be useful so let's try and build them into our API here so let's start by impl in a simple request logger that prints out the request URL and the method when a user makes a request to our server we need to return a Handler fun so let's do that and just before I forget let me call the next function with the Ser HTTP and here let's do the log. printf I'm going to say the method is going to be the path which are coming from the request.
method at the request do URL do path I think so let's try it out so here we wrap our router with the middleware like so and let's try that out and here I'm going to make the request for the users again and if we check our server we should have here a request log so the method get on this path and if you make another request it should have a new log here and we have it so this is how you could just make a simple middleware so very simple but what about combining this middleware with another one so let's say that we want to authenticate this route so only authenticated users that have an authorization token for example JT or something like that can access this inpo so let me first create the require authenication middleware and just like the above it's going to require the HTTP Handler and it's going to return a Handler fank so the first thing we need to do is check if the user is authenticated so let's get the token from the request for example let's get the header dot get it's going to be do theorization by the way I have a complete video course on how to build a restful API I'm going to leave in the description below there we use authentication and Implement our own JT if you're interested and here I'm just going to make a very simple check so if the token doesn't look like this we can just say that the user is not authenticated because it doesn't make sense for us to implement here complete authentication and here I'm just going to say for the user that he's unauthorized and return from the middleware if he's authenticated then we can just call the next. Ser GTP now how can we combine this middleware with this one we can do it like this so we can wrap the other middleware but if you're thinking what I'm thinking this is AGL so let's do something better but let's just try it out and see if it works so here I just started the server and let's do a curl and we get an authorized request because we don't have the header so we're pretty sure that this is working but let me try again and do with the barer token users sl42 so here is the request that I'm passing the barer token like so in the header of the authorization so let's hit enter this quot is exra and if we hit enter we get the user ID 42 so we know that we are deticated now if you were about he mod mid I would probably start to get crazy at looking at this so uh let's Implement something else so what I'm thinking here is that we can make a type called midare and it's going to be the signature so let's just copy we don't need here a name and here we need to put a function and this is how we are defining any middleware then here let's create our function called middleware um chain and it's going to receive all of the middlewares instead it's going to be like so and we return a middleware at the end so this is going to return a function as well which is going to be I'm going to call this next I'm going toop over all of the middle from the ends to the first one because the order matters just like so and then we call the next and at the end all we need to do is call the next.