本文整理汇总了Golang中github.com/go-zoo/bone.Mux.PostFunc方法的典型用法代码示例。如果您正苦于以下问题:Golang Mux.PostFunc方法的具体用法?Golang Mux.PostFunc怎么用?Golang Mux.PostFunc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/go-zoo/bone.Mux
的用法示例。
在下文中一共展示了Mux.PostFunc方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: RegisterRoutes
func (a *GraphicalAuthDecorator) RegisterRoutes(mux *bone.Mux) error {
tmpl, err := template.ParseFiles(
a.uiDir+"/templates/login.html",
a.uiDir+"/templates/layout.html",
)
if err != nil {
return err
}
fileserver := http.FileServer(http.Dir(a.uiDir + "/static/"))
mux.Get("/_static/", http.StripPrefix("/_static/", fileserver))
mux.GetFunc(a.config.GraphicalConfig.LoginRoute, func(res http.ResponseWriter, req *http.Request) {
result := LoginResult{}
if len(req.URL.Query()["redirect"]) > 0 {
result.Redirect = req.URL.Query()["redirect"][0]
}
err := tmpl.ExecuteTemplate(res, "layout", &result)
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(err.Error()))
}
})
mux.PostFunc("/_gateway/authenticate", func(res http.ResponseWriter, req *http.Request) {
username := req.PostFormValue("username")
password := req.PostFormValue("password")
redirect := req.PostFormValue("redirect")
result := LoginResult{Redirect: redirect}
if username == "" {
result.Errors.UserEmpty = true
}
if password == "" {
result.Errors.PasswordEmpty = true
}
if result.HasErrors() {
tmpl.ExecuteTemplate(res, "layout", &result)
return
}
token, err := a.authHandler.Authenticate(username, password)
if err != nil {
result.Errors.InvalidCredentials = true
res.WriteHeader(http.StatusUnauthorized)
tmpl.ExecuteTemplate(res, "layout", &result)
return
}
a.authHandler.storage.WriteToken(res, token)
if redirect != "" {
res.Header().Set("Location", redirect)
res.WriteHeader(303)
res.Write([]byte("Successfully authenticated. Redirecting to original request."))
} else {
res.Write([]byte("Hello."))
}
})
return nil
}