265 lines
11 KiB
Markdown
265 lines
11 KiB
Markdown
# 13.2 カスタム定義のルータの設計
|
||
|
||
## HTTPルーティング
|
||
HTTPルーティングのセットアップはHTTPリクエストが対応する関数の処理(またはstructの方法)に送信されることを担当します。例えば前の節においてご紹介した構造図では、ルーティングはフレームワークにおいてイベントプロセッサに相当します。このイベントは以下を含みます:
|
||
|
||
- ユーザリクエストのパス(path)(例えば:/user/123,/article/123)、当然文字列情報も検索します(例えば?id=11)
|
||
- HTTPのリクエストメソッド(method)(GET、POST、PUT、DELETE、PATCH等)
|
||
|
||
ルータはユーザがリクエストしたイベント情報に基づいて対応する処理関数(コントロールレイヤ)にリダイレクトします。
|
||
## デフォルトルーティングの実装
|
||
3.4節でGoのhttpパッケージの詳細の中でGoのhttpパッケージがどのように設計されルーティングを実装しているかをご紹介しました。ここではもうひとつ例を挙げてご説明しましょう:
|
||
|
||
func fooHandler(w http.ResponseWriter, r *http.Request) {
|
||
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
|
||
}
|
||
|
||
http.Handle("/foo", fooHandler)
|
||
|
||
http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
|
||
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
|
||
})
|
||
|
||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||
|
||
上の例ではhttpのデフォルトであるDefaultServeMuxをコールしてルーティングを追加しています。2つの引数を渡すことでユーザがアクセスするリソースを提供します。1つ目の引数はユーザがこのリソースにアクセスするであろうURLパス(r.URL.Pathに保存されます)、2つ目の引数は次に実行される関数です。ルーティングの考え方は次の二点に集約されます:
|
||
|
||
- ルーティング情報の追加
|
||
- ユーザのリクエストを実行される関数にリダイレクトする
|
||
|
||
Goのデフォルトのルーティングは関数`http.Handle`とhttp.HandleFunc`によって追加され、どちらも深いレイヤで`DefaultServeMux.Handle(pattern string, handler Handler)`をコールしています。この関数はルーティング情報をmap情報`map[string]muxEntity`に保存することで上の1つ目を解決します。
|
||
|
||
Goはポートを監視し、tcp接続を受け付けるとHandlerに処理を投げます。上の例ではデフォルトのnilは`http.DefaultServeMux`です。`DefaultServeMux.ServeHTTP`関数によってディスパッチを行います。事前に保存しておいたmapルーティング情報を、ユーザがアクセスするURLにマッチングすることで、対応する登録された処理関数を探し出します。このように上の二点目を実装します。
|
||
|
||
for k, v := range mux.m {
|
||
if !pathMatch(k, path) {
|
||
continue
|
||
}
|
||
if h == nil || len(k) > n {
|
||
n = len(k)
|
||
h = v.h
|
||
}
|
||
}
|
||
|
||
|
||
## beegoフレームワークのルーティングの実装
|
||
現在ほとんどすべてのWebアプリケーションのルーティングはすべてhttpのデフォルトのルータに基いて実装されています。しかし、Goにはじめから備わっているルータにはいくつかの制限があります:
|
||
|
||
- パラメータ設定をサポートしない。例えば/user/:uid といったマッチング等です。
|
||
- RESTモードをあまりよくサポートしていません。アクセスを制限する方法がありません。例えば上の例で言えば、ユーザによる/fooへのアクセスに、GET、POST、DELETE、HEADといったメソッドでアクセスすることです。
|
||
- 一般的にウェブサイトのルーティングルールは多すぎて、書くのが大変です。以前私はあるAPIアプリケーションを開発したことがあるのですが、ルーティングルールは30数個ありました。このようなルーティングの多さは実は簡素化することができます。structの方法を通して簡素化することが可能です。
|
||
|
||
beegoフレームワークのルータは上のいくつかの制限を考慮して設計されたRESTメソッドのルーティングを実装しています。ルーティングの設計も上のGoデフォルトの設計の二点を考慮しています:すなわち、ルーティングの保存とルーティングのリダイレクトです。
|
||
|
||
### ルーティングの保存
|
||
ここでお話した制限に対して、我々はまずパラメータのサポートに正規表現を使えるよう解決する必要があります。2点目と3点目については柔軟な方法によって解決します。RESTの方法をstructの方法に組み込んでしまうのです。その後関数ではなくstructにルーティングすることで、ルーティングをリダイレクトする際methodに従って異なるメソッドを実行することができるようになります。
|
||
|
||
上の考え方で、我々は2つのデータ型controllerInfo(パスと対応するstructを保存する。ここではreflect.Type型)とControllerRegistor(routersはsliceを使ってユーザが追加したルーティング情報を保存する)を設計しました。
|
||
|
||
type controllerInfo struct {
|
||
regex *regexp.Regexp
|
||
params map[int]string
|
||
controllerType reflect.Type
|
||
}
|
||
|
||
type ControllerRegistor struct {
|
||
routers []*controllerInfo
|
||
Application *App
|
||
}
|
||
|
||
|
||
ControllerRegistorの外側のインターフェース関数には以下があります。
|
||
|
||
func (p *ControllerRegistor) Add(pattern string, c ControllerInterface)
|
||
|
||
細かい実装は以下に示します:
|
||
|
||
func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) {
|
||
parts := strings.Split(pattern, "/")
|
||
|
||
j := 0
|
||
params := make(map[int]string)
|
||
for i, part := range parts {
|
||
if strings.HasPrefix(part, ":") {
|
||
expr := "([^/]+)"
|
||
|
||
//a user may choose to override the defult expression
|
||
// similar to expressjs: ‘/user/:id([0-9]+)’
|
||
|
||
if index := strings.Index(part, "("); index != -1 {
|
||
expr = part[index:]
|
||
part = part[:index]
|
||
}
|
||
params[j] = part
|
||
parts[i] = expr
|
||
j++
|
||
}
|
||
}
|
||
|
||
//recreate the url pattern, with parameters replaced
|
||
//by regular expressions. then compile the regex
|
||
|
||
pattern = strings.Join(parts, "/")
|
||
regex, regexErr := regexp.Compile(pattern)
|
||
if regexErr != nil {
|
||
|
||
//TODO add error handling here to avoid panic
|
||
panic(regexErr)
|
||
return
|
||
}
|
||
|
||
//now create the Route
|
||
t := reflect.Indirect(reflect.ValueOf(c)).Type()
|
||
route := &controllerInfo{}
|
||
route.regex = regex
|
||
route.params = params
|
||
route.controllerType = t
|
||
|
||
p.routers = append(p.routers, route)
|
||
|
||
}
|
||
|
||
### スタティックルーティングの実装
|
||
上では動的なルーティングの実装を行いました。Goのhttpパッケージはデフォルトで静的なファイルを処理するFileServerをサポートしています。自分で定義したルータを実装したわけですから、静的なファイルも自分たちで設定しなければなりません。beegoの静的ディレクトリパスはグローバル変数StaticDirに保存されています。StaticDirはmap型で、以下のように実装されています:
|
||
|
||
func (app *App) SetStaticPath(url string, path string) *App {
|
||
StaticDir[url] = path
|
||
return app
|
||
}
|
||
|
||
アプリケーションにおいて静的なルーティングを設定するには以下の方法で行います:
|
||
|
||
beego.SetStaticPath("/img","/static/img")
|
||
|
||
|
||
### リダイレクトルーティング
|
||
リダイレクトルーティングはControllerRegistorの中のルーティング情報に基づいてリダイレクトが行われます。細かい実装は以下のコードに示します:
|
||
|
||
// AutoRoute
|
||
func (p *ControllerRegistor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
defer func() {
|
||
if err := recover(); err != nil {
|
||
if !RecoverPanic {
|
||
// go back to panic
|
||
panic(err)
|
||
} else {
|
||
Critical("Handler crashed with error", err)
|
||
for i := 1; ; i += 1 {
|
||
_, file, line, ok := runtime.Caller(i)
|
||
if !ok {
|
||
break
|
||
}
|
||
Critical(file, line)
|
||
}
|
||
}
|
||
}
|
||
}()
|
||
var started bool
|
||
for prefix, staticDir := range StaticDir {
|
||
if strings.HasPrefix(r.URL.Path, prefix) {
|
||
file := staticDir + r.URL.Path[len(prefix):]
|
||
http.ServeFile(w, r, file)
|
||
started = true
|
||
return
|
||
}
|
||
}
|
||
requestPath := r.URL.Path
|
||
|
||
//find a matching Route
|
||
for _, route := range p.routers {
|
||
|
||
//check if Route pattern matches url
|
||
if !route.regex.MatchString(requestPath) {
|
||
continue
|
||
}
|
||
|
||
//get submatches (params)
|
||
matches := route.regex.FindStringSubmatch(requestPath)
|
||
|
||
//double check that the Route matches the URL pattern.
|
||
if len(matches[0]) != len(requestPath) {
|
||
continue
|
||
}
|
||
|
||
params := make(map[string]string)
|
||
if len(route.params) > 0 {
|
||
//add url parameters to the query param map
|
||
values := r.URL.Query()
|
||
for i, match := range matches[1:] {
|
||
values.Add(route.params[i], match)
|
||
params[route.params[i]] = match
|
||
}
|
||
|
||
//reassemble query params and add to RawQuery
|
||
r.URL.RawQuery = url.Values(values).Encode() + "&" + r.URL.RawQuery
|
||
//r.URL.RawQuery = url.Values(values).Encode()
|
||
}
|
||
//Invoke the request handler
|
||
vc := reflect.New(route.controllerType)
|
||
init := vc.MethodByName("Init")
|
||
in := make([]reflect.Value, 2)
|
||
ct := &Context{ResponseWriter: w, Request: r, Params: params}
|
||
in[0] = reflect.ValueOf(ct)
|
||
in[1] = reflect.ValueOf(route.controllerType.Name())
|
||
init.Call(in)
|
||
in = make([]reflect.Value, 0)
|
||
method := vc.MethodByName("Prepare")
|
||
method.Call(in)
|
||
if r.Method == "GET" {
|
||
method = vc.MethodByName("Get")
|
||
method.Call(in)
|
||
} else if r.Method == "POST" {
|
||
method = vc.MethodByName("Post")
|
||
method.Call(in)
|
||
} else if r.Method == "HEAD" {
|
||
method = vc.MethodByName("Head")
|
||
method.Call(in)
|
||
} else if r.Method == "DELETE" {
|
||
method = vc.MethodByName("Delete")
|
||
method.Call(in)
|
||
} else if r.Method == "PUT" {
|
||
method = vc.MethodByName("Put")
|
||
method.Call(in)
|
||
} else if r.Method == "PATCH" {
|
||
method = vc.MethodByName("Patch")
|
||
method.Call(in)
|
||
} else if r.Method == "OPTIONS" {
|
||
method = vc.MethodByName("Options")
|
||
method.Call(in)
|
||
}
|
||
if AutoRender {
|
||
method = vc.MethodByName("Render")
|
||
method.Call(in)
|
||
}
|
||
method = vc.MethodByName("Finish")
|
||
method.Call(in)
|
||
started = true
|
||
break
|
||
}
|
||
|
||
//if no matches to url, throw a not found exception
|
||
if started == false {
|
||
http.NotFound(w, r)
|
||
}
|
||
}
|
||
|
||
### 事始め
|
||
このようなルーティング設計に基いていると、前に説明した3つの制限をクリアできます。使い方は以下に示します:
|
||
|
||
基本的なルーティング登録の使用:
|
||
|
||
beego.BeeApp.RegisterController("/", &controllers.MainController{})
|
||
|
||
オプションの登録:
|
||
|
||
beego.BeeApp.RegisterController("/:param", &controllers.UserController{})
|
||
|
||
正規表現マッチング:
|
||
|
||
beego.BeeApp.RegisterController("/users/:uid([0-9]+)", &controllers.UserController{})
|
||
|
||
## links
|
||
* [目次](<preface.md>)
|
||
* 前へ: [プロジェクトのプラン](<13.1.md>)
|
||
* 次へ: [controller設計](<13.3.md>)
|