#Http
10 articles
08 Create Middleware Router
Understanding Middleware
HttpRouter is a library for creating an http router, it doesn’t have any other features apart from a router and this router is an implementation of the default http.Handler
from Golang itself so that to create middleware we can create it ourselves as we have created in the previous Golang Web post, namely
HTTP Middleware.
07 Handle Not Allowed Method
Understanding Method Not Allowed
When we use ServerMux, we cannot determine what HTTP Method we will use in the Handler. However, on the router that we are using, we can determine what HTTP method we want to use so that the client will send the appropriate method to the router that we have specified. If it does not comply with the provisions on the Router, a Method Not Allowed error will occur.
06 Handle Not Found Page
Understanding Not Found Handler
Apart from the Router being able to control panic
, it can also have a handler for pages not found or what we often call pages that cannot be accessed. The Not Found handler is a handler that is executed when a client tries to make a request for a page or URL of our website that is not in our Router service. By default, if there is no route it will not be found, but the Router will continue the request to http.NotFound
, but we can also change it to a specific router page by changing
05 Learn About Panic Handler
Understanding Panic Handlers
When we create logic in the handler, we must think of a case where panic occurs, then what should we do? In this Panic handler an error will automatically occur and the website will stop returning responses. In the previous material, we discussed how the Web handles panic by creating special Middleware manually. However, the Router has been provided to handle panic
by using the PanicHandler
attribute.
04 Learn About Serve File
Understanding File Server
In the material
Creating Golang Web. So the Router also supports serving static files using the ServeFiles(Path, FileSystem)
function where in Path
we have to use Catch All Parameters. Meanwhile, in FileSystem
you can manually load it from a folder or use Golang Embed as we discussed in the previous material.
03 Learn About Route Pattern
Use of Named Parameter
The Pattern Router have a have a Pattern to handle http or web application for every endpoint has a parameter pattern in a URL and is often called a Named Parameter. Named Parameter is a pattern for creating parameters using names. Each parameter name begins with a colon :
followed by the parameter name. For example, an example like the one below.
02 Learning About HTTP Router Params
Use of HTTP Router Params
The httprouter.Handle
has an additional parameter, namely Params
, which is used to store parameters sent from the client, but this Params
is not a query for parameters
but is a parameter from the URL. Sometimes we need to create URLs that are not fixed or can change, for example /product/1
, /product/2
and so on.
01 Introduction HTTP Router
Introduction
HttpRouter
is a popular open source library for HTTP Handler in Golang. This HttpRouter
is famous for its speed and is very minimalist or simple because it only has features for routing, it doesn’t have any features other than that. If you want to see more details, you can visit the github here https://github.com/julienschmidt/httprouter
20 How to Understanding Routing Library in Golang
Routing Library
Golang actually provides ServeMux
as a handler that can handle several endpoints or the term is routing
. But most Golang programmers will usually use libraries to do this routing because ServeMux
does not have advanced features such as path variables, auto binding parameters and middleware. So there are many other alternatives that we can use for routing libraries besides ServeMux
.
19 How to Understanding HTTP Middleware in Golang
Middleware
In web creation, we often hear the concept of middleware or filter or interceptor which is a feature that we can add code to before and after a handler is executed.