This is a creation in Article, where the information may have evolved or changed.
Phalgo-echo Routing
Echo Website address: Https://labstack.com/echo
Echo is the most core component of PHALGO, responsible for the overall request routing return and other functions, and Echo supports the HTTP2 protocol as well as the HTTPS protocol
Why Choose Echo
In the early days, the author considered echo,gin and Beego to try to implement their own projects, and ultimately chose to use Echo as the main routing framework for Phalgo.
The factor that makes me decide is that you should use Fasthttp for echo support so there is no comparison between the basic other frameworks on efficiency (the author is a performance dog), we can look at the official Echo performance chart
Machine configuration:
- Go 1.6
- WRK 4.0.0
- 2 GB, 2 Core
Gin relatively more humane some beego relatively easy to use some interested can try
Registering routes
All components in the Phalgo need to be registered at the portal
//初始化ECHO路由phalgo.NewEcho()
Then we can register our route, we recommend that you store the routing go file in the project to create a routes directory and then introduce it in the portal file.
// Routes 载入路由routes.GetRoutes()
ECHO supports restful standards
phalgo.Echo.Get() //接受Get请求phalgo.Echo.Post() //接受Post请求phalgo.Echo.Delete() //接受Delete请求phalgo.Echo.Put() //接受Put请求phalgo.Echo.Any() //接受所有请求方式
All of the above methods need to accept two parameters, the first is the path of the request such as fill in * * * * *, you need to use **localhost/test/5** this way request, the second parameter must be a method **func (Context) error* * Examples are as follows
func hello(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!")}//接收Get请求e.GET("/test/:id",hello)
Open service
There are two ways to open a service. One is the default standard way one is fasthttp, see the above comparison chart also should be the way fasthttp how strong, we can add the following statement in the entry file to open the service
//使用Fasthttp方式phalgo.RunFasthttp(":1333")//使用Standard方式phalgo.RunStandard(":1333")
This allows you to run an HTTP service.
Middleware middleware
Echo has a lot of good middleware, the author here in the package, here simply mention a few and their functions, the use of the way only need to initialize the service before the start of the
1.Recover
Print request exception information is printed using recover
phalgo.Recover()
For example, to access an unregistered route address, the page will print not Found, the terminal will print the following statement:
{"time":"2016-05-24T17:02:12+08:00","level":"ERROR","prefix":"echo","file":"echo.go","line":"226","message":"Not Found"}
2.Logger
Use logger will print out all the request details, request IP request, request time request address, request httpcode and so on, convenient debugging
Successful request:
{"time":"2016-05-24T17:02:12+08:00","remote_ip":"101.81.5.247","method":"GET","uri":"/","status":200, "latency":194,"latency_human":"194.251µs","rx_bytes":0,"tx_bytes":76}
Request failed:
{"time":"2016-05-24T17:02:10+08:00","remote_ip":"101.81.5.247","method":"GET","uri":"/s","status":404, "latency":79,"latency_human":"79.126µs","rx_bytes":0,"tx_bytes":9}
3.Gzip compression
In many cases we need to turn on gzip compression to reduce the size of the returned data to save traffic can be set in the following ways:
phalgo.Gzip()
4. Trailing slash handling
There is a problem in matching the route, such as we define a route is **/test** we pass **localhost/test/** is not the request should be more than a slash at the end, this time can be processed by the end of the middleware to add the trailing slash or the trailing slash
//自动添加末尾斜杠phalgo.AddTrailingSlash()//自动删除末尾斜杠phalgo.RemoveTrailingSlash()