代码拉取完成,页面将自动刷新
package main
import (
"github.com/tfzxyinhao/proj/context"
"log"
"net/http"
)
// http server request handler
type MyHandler struct {
Db *Database
RouterMatch *Router
CrossDomain bool
}
// default http request handler
func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.Path)
handler, param := h.RouterMatch.Match(r.URL.Path)
c := &context.Context{Params: param, Req: r, Res: w}
if handler != nil {
if h.CrossDomain {
w.Header().Add("Access-Control-Allow-Origin", "*")
}
handler(c)
} else {
w.WriteHeader(404)
w.Write([]byte("Not Found"))
}
}
// register handler for path
func (h *MyHandler) AddRouter(path string, cb func(c *context.Context)) {
h.RouterMatch.Register(path, cb)
}
// enable or disable crossdomain
func (h *MyHandler) EnableCrossDomain(enable bool) {
h.CrossDomain = enable
}
// start to handle http request
func (h *MyHandler) Start(addr string) {
http.ListenAndServe(addr, h)
}
// create and init http hander
func NewHanlder() *MyHandler {
handler := &MyHandler{RouterMatch: &Router{Handler: make(map[string]func(c *context.Context)), SubRouter: make(map[string]*Router)}, Db: &Database{}, CrossDomain: false}
handler.Db.Init()
return handler
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。