Http routing basics in Go language

Recently, I am writing some web applications in Go language, because the web applications in Go language are not the same as those in Python, the specific difference should be related to the dynamic nature of the language, and at the same time, it is also related to the built-in library support of the language. So this leads to a phenomenon, that is, the web framework in the Go language seems to be less popular (of course I know that Beego is being played in China), unlike Python, various popular frameworks have many supporters.

Because there is no framework, I usually use some Mux components when using it. However, I often confuse some concepts. Therefore, in order to better improve the quality of the code in the future, I will first make a basic Go HTTP component. To sum up, by the way, I hope it will be helpful to you who are reading this article now.

simple web server

Before starting anything else, let's take a look at a simple web server provided by others and see how others do it:

This is a very simple example. In fact, there are two key points. The first one is Line18 - Line 22 , which is a structure that provides a ServeHTTPmethod , and then the object of this structure is used directly as a parameter in Line 12 :

http.ListenAndServe(":8111", db)

Looking at the prototype of the ListenAndServefunction , you can find that the type of db should be Handler, let's take a look at the Handlerinterface :

type Handler interface {
    ServeHTTP(ResponseWriter, *Request)
}

The Handler interface is very simple, there is only one method, and this method accepts two parameters, namely ResponseWriteand Request; this means that as long as a structure implements the Handler interface, it can be used as an HTTP processing structure.

more complex web server

According to the previous understanding, then we can implement a more complex application:

Here we make some judgments on the URL path of the request, and then process different code logic according to different paths. It looks okay at present, but once our application is complicated, this function may be very ugly, as a way to improve code quality. As the first step, it seems that we can easily think of the extraction function, but the simple extraction function still cannot reduce switch/caseour ugly situation. Therefore, in order to solve this problem, the Go language provides a built-in URL organization component ServeMux. Through this ServeMux, we can better manage the URL path and code problems. The following is an example of how to use it:

It can be seen from here that ServeMuxafter , the style of the entire code has become much fresher and the entire structure is clearer. However, this is only the most superficial layer of application. In addition, the Go language also provides us with A simpler simplification is made. In Line 4 , we create one ServeMux. If we don't create it ourselves, the Go language built-in helps us create one DefaultServeMux. We can just register it directly:

This is much simpler. But is this method enough? It doesn't seem to be. Think about it if we are an e-commerce website. The first two are just a very, very simple interface for item and price, and there will be a lot of APIs in the future. As for the interface, if it is all registered like this, how long is the list, and how complicated it is for us to modify and find it.

More advanced URL routing

Although the built-in Mux is simple to use, its function is weaker because of its simplicity, such as the URL group routing we want, that is to say, suppose we want to have:

/items/list
/items/add
/items/delete

The routes of these URLs are all implemented in item.go, and then, for example, the order class is implemented in Order. If the built-in Mux is also used, it will be troublesome to register and modify.

In addition, the built-in Mux cannot perform URL regular matching, nor can it simply implement URL Parameter, so many netizens spontaneously wrote some Mux components. I will briefly demonstrate github.com/gorilla/muxthis component:

What is demonstrated here is an example of URL Parameter, which is the tip of the iceberg. As for more functions, if you are interested, you can go to the link above to view it.

summary

This article starts with a simple Web server example, and describes some evolution of HTTP routing in Go language. The content is relatively simple and rough, but my purpose is only to understand these basics, so that I can When you see the advanced implementation later, you know what the principle of the implementation is, so that you can adapt to the changes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325173109&siteId=291194637