您好我正在使用justinas/alice,我想根据路径创建不同的中间件。即如果我有路径1和路径2,我想为路径1应用m1,m2,m3,为路径2应用m1,m2;
我试过了:
router := mux.NewRouter()
router2 := mux.NewRouter()
router.HandleFunc(path1,Func1)
router2.HandleFunc(path2,Func2)
middlewares:=alice.New(m1,m2).Then(router2)
middlewaress:=middlewares.Append(middlewares)
- 然后:
if err := http.ListenAndServe(fmt.Sprintf(":%d", sconf.Server.Port), middlewaress); err != nil {
}
我该怎么办?
答案 0 :(得分:1)
您需要将router
和router
的处理程序设置为alice
返回的链。
// define routers
router := mux.NewRouter() // assuming this is gorilla mux
router2 := mux.NewRouter()
// create alice chains
chain1 := alice.New(m1, m2, m3).Then(func1)
chain2 := alice.New(m1, m2).Then(func2)
// set chains as path handlers
router.HandleFunc(path1, chain1)
router2.HandleFunc(path2, chain2)