当前位置: 首页>>代码示例>>Golang>>正文


Golang Mux.GetFunc方法代码示例

本文整理汇总了Golang中github.com/go-zoo/bone.Mux.GetFunc方法的典型用法代码示例。如果您正苦于以下问题:Golang Mux.GetFunc方法的具体用法?Golang Mux.GetFunc怎么用?Golang Mux.GetFunc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/go-zoo/bone.Mux的用法示例。


在下文中一共展示了Mux.GetFunc方法的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
}
开发者ID:martin-helmich,项目名称:servicegateway,代码行数:66,代码来源:graphical.go


注:本文中的github.com/go-zoo/bone.Mux.GetFunc方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。