真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

golang實現(xiàn)路由中間件middleware

registerHandlers.go

在三元等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計、成都網(wǎng)站制作 網(wǎng)站設(shè)計制作按需求定制網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計,成都全網(wǎng)營銷,成都外貿(mào)網(wǎng)站制作,三元網(wǎng)站建設(shè)費用合理。

package router

import (
    "../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")) //啟動靜態(tài)資源服務(wù)
    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 main

import (
    "./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{}
//}

//循環(huán)渲染所有模版、路由方法
func DisplayView() {
    tpl, err = template.ParseGlob("view/**/*")
    //錯誤則不必再繼續(xù)
    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)
    }

    //初始化核心數(shù)
    runtime.GOMAXPROCS(runtime.NumCPU())

    //初始化數(shù)據(jù)庫

    //渲染模版
    //DisplayView()

    //注冊路由
    r := router.RegisterHandlers()
    mh := router.NewMiddleWareHandler(r)
    err := http.ListenAndServe(":80", mh)
    if err != nil {
        log.Fatal(err)
    }
}

還可以更靈活:
main.go

package main

import (
    "./funcs"
    "html/template"
    "log"
    "net/http"
    "runtime"
    "./ctrl"
)

var (
    AllConf funcs.Confs
    err     error
    tpl     *template.Template
    //SessionManager *funcs.Manager
)

//func init() {
//  SessionManager = funcs.Manager{}
//}

//循環(huán)渲染所有模版、路由方法
func DisplayView() {
    tpl, err = template.ParseGlob("view/**/*")
    //錯誤則不必再繼續(xù)
    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)
    }

    //初始化核心數(shù)
    runtime.GOMAXPROCS(runtime.NumCPU())

    //初始化數(shù)據(jù)庫

    http.Handle("/user/login", http.HandlerFunc(ctrl.Login)) //登陸 post
    http.Handle("/user/register", http.HandlerFunc(ctrl.Register)) //注冊 post
    http.Handle("/contact/addfriend", funcs.AuthLogin(http.HandlerFunc(ctrl.AddFriend))) //添加好友 get
    http.Handle("/contact/loadfriend", funcs.AuthLogin(http.HandlerFunc(ctrl.LoadFriend))) //加載好友 get

    err := http.ListenAndServe(":80", nil)
    if err != nil {
        log.Fatal(err)
    }
}

middleWare.go

package funcs

import (
    "../defs"
    "../model/redis"
    "encoding/json"
    "net/http"
)

//驗證是否登陸
func AuthLogin(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        //驗證...

    })
}

ctrl/addFriend.go

package ctrl

import (
    "../funcs"
    "../model"
    "fmt"
    "net/http"
    "strconv"
)

var contactModel model.Contact

func AddFriend(writer http.ResponseWriter, request *http.Request) {

    fmt.Println("addFriend")
    return

}

新聞標(biāo)題:golang實現(xiàn)路由中間件middleware
本文網(wǎng)址:http://weahome.cn/article/jssdhc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部