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