golang不到20行代碼實現路由調度

項目地址

githubjava

本項目依賴

使用標準庫實現,無額外依賴mysql

爲何須要路由調度層

golang http標準庫只能精確匹配請求的URI,而後執行handler。如今通常web項目都至少有個Controller層,以struct實現,根據不一樣的請求路徑派發到不一樣的方法中去。

路由調度器定義

因爲golang暫時還不能夠動態建立對象(好比java的Class.forName("xxx").newInstance(),xxx是任意存在的class名稱)。因此須要手動註冊一下controller關係。git

  1. 定義routes保存controller指針
  2. 解析請求過來的URL查詢參數,暫定aaction名稱,ccontroller名稱,本文偷了下懶,沒對PATH_INFO作處理,也沒有對actionName的首字母自動大寫,這個不影響本文要傳達的核心內容,有興趣的讀者能夠自行實現。
  3. 根據URL中的controllerName找到對應的controller
  4. 使用反射將當前請求對象的*http.Requesthttp.ResponseWriter設置到該Controller
  5. 使用反射以及actionName對應該controller的方法
因爲golang的繼承不是通常的OOP,因此也沒有父子類這種說法,路由註冊那裏只能使用interface{}

代碼實現

app/app.go

該文件爲核心調度文件github

package app

import (
    "net/http"
    "reflect"
    "fmt"
)

type application struct {
    routes map[string]interface{}
}

func New() *application {
    return &application{
        routes: make(map[string]interface{}),
    }
}

func (p *application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    controllerName := r.URL.Query().Get("c")
    actionName := r.URL.Query().Get("a")
    if controllerName == "" || actionName == "" {
        http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
        return
    }
    route, ok := p.routes[controllerName]
    if !ok {
        http.Error(w, "Controller Not Found", http.StatusNotFound)
        return
    }
    ele := reflect.ValueOf(route).Elem()
    ele.FieldByName("Request").Set(reflect.ValueOf(r))
    ele.FieldByName("Response").Set(reflect.ValueOf(w))
    ele.MethodByName(actionName).Call([]reflect.Value{})
}

func (p *application) printRoutes() {
    for route, controller := range p.routes {
        ele := reflect.ValueOf(controller).Type().String()
        fmt.Printf("%s %s\n", route, ele)
    }
}

func (p *application) Get(route string, controller interface{}) {
    p.routes[route] = controller
}

func (p *application) Run(addr string) error {
    p.printRoutes()
    fmt.Printf("listen on %s\n", addr)
    return http.ListenAndServe(addr, p)
}

app/controller.go

控制器"基類"golang

package app

import "net/http"

type Controller struct {
    Response http.ResponseWriter
    Request  *http.Request
}

controller/site.go

具體業務邏輯類web

package controllers

import (
    "fmt"
    "app"
)

type SiteController struct {
    app.Controller
}

func (p SiteController) Index() {
    fmt.Fprint(p.Response, p.Request.RequestURI)
}

main.go

入口文件sql

package main

import (
    _ "github.com/go-sql-driver/mysql"
    "app"
    "controllers"
)

func main() {
    application := app.New()
    application.Get("site", &controllers.SiteController{})
    application.Run(":8080")
}

運行項目

  1. 啓動進程
  2. 訪問http://localhost:8080?c=site&a=Index會輸出/?c=site&a=Index

寫在最後

但願這個小小的項目能啓發到各位讀者,早日開發出適合本身的Web框架!app

相關文章
相關標籤/搜索