Add 13.2.md syntax highlighting
This commit is contained in:
34
zh/13.2.md
34
zh/13.2.md
@@ -9,6 +9,7 @@ HTTP路由组件负责将HTTP请求交到对应的函数处理(或者是一个st
|
||||
路由器就是根据用户请求的事件信息转发到相应的处理函数(控制层)。
|
||||
## 默认的路由实现
|
||||
在3.4小节有过介绍Go的http包的详解,里面介绍了Go的http包如何设计和实现路由,这里继续以一个例子来说明:
|
||||
```Go
|
||||
|
||||
func fooHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
|
||||
@@ -21,7 +22,8 @@ HTTP路由组件负责将HTTP请求交到对应的函数处理(或者是一个st
|
||||
})
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
|
||||
|
||||
```
|
||||
上面的例子调用了http默认的DefaultServeMux来添加路由,需要提供两个参数,第一个参数是希望用户访问此资源的URL路径(保存在r.URL.Path),第二参数是即将要执行的函数,以提供用户访问的资源。路由的思路主要集中在两点:
|
||||
|
||||
- 添加路由信息
|
||||
@@ -30,6 +32,7 @@ HTTP路由组件负责将HTTP请求交到对应的函数处理(或者是一个st
|
||||
Go默认的路由添加是通过函数`http.Handle`和`http.HandleFunc`等来添加,底层都是调用了`DefaultServeMux.Handle(pattern string, handler Handler)`,这个函数会把路由信息存储在一个map信息中`map[string]muxEntry`,这就解决了上面说的第一点。
|
||||
|
||||
Go监听端口,然后接收到tcp连接会扔给Handler来处理,上面的例子默认nil即为`http.DefaultServeMux`,通过`DefaultServeMux.ServeHTTP`函数来进行调度,遍历之前存储的map路由信息,和用户访问的URL进行匹配,以查询对应注册的处理函数,这样就实现了上面所说的第二点。
|
||||
```Go
|
||||
|
||||
for k, v := range mux.m {
|
||||
if !pathMatch(k, path) {
|
||||
@@ -40,7 +43,7 @@ Go监听端口,然后接收到tcp连接会扔给Handler来处理,上面的
|
||||
h = v.h
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## beego框架路由实现
|
||||
目前几乎所有的Web应用路由实现都是基于http默认的路由器,但是Go自带的路由器有几个限制:
|
||||
@@ -55,6 +58,7 @@ beego框架的路由器基于上面的几点限制考虑设计了一种REST方
|
||||
针对前面所说的限制点,我们首先要解决参数支持就需要用到正则,第二和第三点我们通过一种变通的方法来解决,REST的方法对应到struct的方法中去,然后路由到struct而不是函数,这样在转发路由的时候就可以根据method来执行不同的方法。
|
||||
|
||||
根据上面的思路,我们设计了两个数据类型controllerInfo(保存路径和对应的struct,这里是一个reflect.Type类型)和ControllerRegistor(routers是一个slice用来保存用户添加的路由信息,以及beego框架的应用信息)
|
||||
```Go
|
||||
|
||||
type controllerInfo struct {
|
||||
regex *regexp.Regexp
|
||||
@@ -67,12 +71,14 @@ beego框架的路由器基于上面的几点限制考虑设计了一种REST方
|
||||
Application *App
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
ControllerRegistor对外的接口函数有
|
||||
```Go
|
||||
|
||||
func (p *ControllerRegistor) Add(pattern string, c ControllerInterface)
|
||||
|
||||
```
|
||||
详细的实现如下所示:
|
||||
```Go
|
||||
|
||||
func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) {
|
||||
parts := strings.Split(pattern, "/")
|
||||
@@ -118,22 +124,25 @@ ControllerRegistor对外的接口函数有
|
||||
p.routers = append(p.routers, route)
|
||||
|
||||
}
|
||||
|
||||
```
|
||||
### 静态路由实现
|
||||
上面我们实现的动态路由的实现,Go的http包默认支持静态文件处理FileServer,由于我们实现了自定义的路由器,那么静态文件也需要自己设定,beego的静态文件夹路径保存在全局变量StaticDir中,StaticDir是一个map类型,实现如下:
|
||||
```Go
|
||||
|
||||
func (app *App) SetStaticPath(url string, path string) *App {
|
||||
StaticDir[url] = path
|
||||
return app
|
||||
}
|
||||
|
||||
```
|
||||
应用中设置静态路径可以使用如下方式实现:
|
||||
```Go
|
||||
|
||||
beego.SetStaticPath("/img","/static/img")
|
||||
|
||||
|
||||
```
|
||||
### 转发路由
|
||||
转发路由是基于ControllerRegistor里的路由信息来进行转发的,详细的实现如下代码所示:
|
||||
```Go
|
||||
|
||||
// AutoRoute
|
||||
func (p *ControllerRegistor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -242,22 +251,25 @@ ControllerRegistor对外的接口函数有
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
### 使用入门
|
||||
基于这样的路由设计之后就可以解决前面所说的三个限制点,使用的方式如下所示:
|
||||
|
||||
基本的使用注册路由:
|
||||
```Go
|
||||
|
||||
beego.BeeApp.RegisterController("/", &controllers.MainController{})
|
||||
|
||||
```
|
||||
参数注册:
|
||||
```Go
|
||||
|
||||
beego.BeeApp.RegisterController("/:param", &controllers.UserController{})
|
||||
|
||||
```
|
||||
正则匹配:
|
||||
```Go
|
||||
|
||||
beego.BeeApp.RegisterController("/users/:uid([0-9]+)", &controllers.UserController{})
|
||||
|
||||
```
|
||||
## links
|
||||
* [目录](<preface.md>)
|
||||
* 上一章: [项目规划](<13.1.md>)
|
||||
|
||||
Reference in New Issue
Block a user