package routerimport ( "../ctrl" "../funcs" "github.com/julIEnschmIDt/httprouter" "net/http")type mIDdleWareHandler struct { r *httprouter.Router}func NewMIDdleWareHandler(r *httprouter.Router) http.Handler { m := mIDdleWareHandler{} m.r = r return m}func (m mIDdleWareHandler) Servehttp(w http.ResponseWriter,r *http.Request) { //检验合法性(session) ok := funcs.ValIDateUser(r) if ok == false { funcs.Resp(w,-1,"请先登录",nil) return } m.r.Servehttp(w,r)}func RegisterHandlers() *httprouter.Router { router := httprouter.New() router.Servefiles("/asset/*filepath",http.Dir("asset")) //启动静态资源服务 router.POST("/user/login",ctrl.Login) //登陆 router.POST("/user/register",ctrl.Register) //注册 router.POST("/contact/addfrIEnd",ctrl.AddFrIEnd) //添加好友 router.POST("/contact/loadfrIEnd",ctrl.LoadFrIEnd) //加载好友 return router}
main.go
package mainimport ( "./funcs" "./router" "HTML/template" "log" "net/http" "runtime")var ( AllConf funcs.Confs err error tpl *template.Template //SessionManager *funcs.Manager)//func init() {// SessionManager = funcs.Manager{}//}//循环渲染所有模版、路由方法func displayVIEw() { tpl,err = template.ParseGlob("vIEw/**/*") //错误则不必再继续 if err != nil { log.Fatal(err.Error()) } for _,v := range tpl.Templates() { tplname := v.name() http.HandleFunc(tplname,func(writer http.ResponseWriter,request *http.Request) { tpl.ExecuteTemplate(writer,tplname,nil) }) }}func main() { //初始化配置 if AllConf,err = funcs.InitConf("config.Json"); err != nil { log.Fatal(err) } //初始化核心数 runtime.GOMAXPROCS(runtime.Numcpu()) //初始化数据库 //渲染模版 //displayVIEw() //注册路由 r := router.RegisterHandlers() mh := router.NewMIDdleWareHandler(r) err := http.ListenAndServe(":80",mh) if err != nil { log.Fatal(err) }}总结
以上是内存溢出为你收集整理的golang实现路由中间件middleware全部内容,希望文章能够帮你解决golang实现路由中间件middleware所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)