How to assign Middleware MUX only to one Handler only?
func (s * server) configurerouter () {
S.Router.HandleFunc ("/ Signup", S.HandleSignUp ()). Methods ("Post")
S.Router.Handlefunc ("/ Signin", S.HandleSignin ()). Methods ("Post")
S.Router.HandleFunc ("/ profile / {ID: [0-9] +}", s .hylecreateprofile ()). Methods ("POST")
S.ROUTER.USE (S.AuthenticateUser)
}
There is a picture of how to make authenticateUser called only with "/ profile / {ID: [0-9] +}"
?
Answer 1, Authority 100%
You need to create Subrouted from the main router. Handler and MidDleWareFunc
In your case, it turns out how
func (s * server) configurerouter () {
S.Router.HandleFunc ("/ Signup", S.HandleSignUp ()). Methods ("Post")
S.Router.Handlefunc ("/ Signin", S.HandleSignin ()). Methods ("Post")
Subrouter: = s.router.handlefunc ("/ profile / {ID: [0-9] +}", s.handlecreateprofile ()). subroute ()
Subrouter.use (S.AuthenticateUser)
subrouter.methods ("POST")
}
Test example
FUNC MAIN () {
Router: = Mux.Newrouter ()
Router.HandleFunc ("/ Signup", Func (http.ResponseWriter, * http.request) {
PrintLN ("Signup")
}). Methods ("POST")
Router.HandleFunc ("/ Signin", Func (http.ResponseWriter, * http.request) {
PrintLN ("Signin")
}). Methods ("POST")
Subrouter: = Router.HandleFunc ("/ Profile", Func (W http.ResponseWriter, R * http.request) {
PrintLN ("42")
}). Subrouter ()
subrouter.use (Func (H Http.Handler) http.Handler {
Return http.HandlerFunc (Func (W http.ResponseWriter, R * http.request) {
w.Write ([] BYTE ("Custom Message"))
})
})
subrouter.methods ("POST")
SRV: = & amp; http.server {
Handler: Router,
ADDR: "127.0.0.1:8000",
WritetimeOut: 15 * Time.second,
ReadTimeout: 15 * Time.second,
}
log.fatal (srv.listenandServe ())
}