githubjava
使用標準庫實現,無額外依賴mysql
golang http標準庫只能精確匹配請求的URI,而後執行handler。如今通常web項目都至少有個Controller層,以struct實現,根據不一樣的請求路徑派發到不一樣的方法中去。
因爲golang暫時還不能夠動態建立對象(好比java的Class.forName("xxx").newInstance()
,xxx是任意存在的class名稱)。因此須要手動註冊一下controller關係。git
routes
保存controller指針a
爲action名稱
,c
爲controller名稱
,本文偷了下懶,沒對PATH_INFO作處理,也沒有對actionName的首字母自動大寫,這個不影響本文要傳達的核心內容,有興趣的讀者能夠自行實現。controllerName
找到對應的controller*http.Request
和http.ResponseWriter
設置到該Controller因爲golang的繼承不是通常的OOP,因此也沒有父子類這種說法,路由註冊那裏只能使用interface{}
該文件爲核心調度文件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) }
控制器"基類"golang
package app import "net/http" type Controller struct { Response http.ResponseWriter Request *http.Request }
具體業務邏輯類web
package controllers import ( "fmt" "app" ) type SiteController struct { app.Controller } func (p SiteController) Index() { fmt.Fprint(p.Response, p.Request.RequestURI) }
入口文件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") }
http://localhost:8080?c=site&a=Index
會輸出/?c=site&a=Index
但願這個小小的項目能啓發到各位讀者,早日開發出適合本身的Web框架!app