本文整理汇总了Golang中util.Redirect函数的典型用法代码示例。如果您正苦于以下问题:Golang Redirect函数的具体用法?Golang Redirect怎么用?Golang Redirect使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Redirect函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ArticleDetailHandler
// 文章详细页
// uri: /articles/{id:[0-9]+}
func ArticleDetailHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
article, prevNext, err := service.FindArticlesById(vars["id"])
if err != nil {
util.Redirect(rw, req, "/articles")
return
}
if article == nil || article.Id == 0 || article.Status == model.StatusOffline {
util.Redirect(rw, req, "/articles")
return
}
likeFlag := 0
hadCollect := 0
user, ok := filter.CurrentUser(req)
if ok {
uid := user["uid"].(int)
likeFlag = service.HadLike(uid, article.Id, model.TYPE_ARTICLE)
hadCollect = service.HadFavorite(uid, article.Id, model.TYPE_ARTICLE)
}
service.Views.Incr(req, model.TYPE_ARTICLE, article.Id)
// 为了阅读数即时看到
article.Viewnum++
// 设置内容模板
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/articles/detail.html,/template/common/comment.html")
// 设置模板数据
filter.SetData(req, map[string]interface{}{"activeArticles": "active", "article": article, "prev": prevNext[0], "next": prevNext[1], "likeflag": likeFlag, "hadcollect": hadCollect})
}
示例2: WRHandler
// 包装链接
func WRHandler(rw http.ResponseWriter, req *http.Request) {
tUrl := req.FormValue("u")
if tUrl == "" {
util.Redirect(rw, req, "/")
return
}
if pUrl, err := url.Parse(tUrl); err != nil {
util.Redirect(rw, req, tUrl)
return
} else {
if !pUrl.IsAbs() {
util.Redirect(rw, req, tUrl)
return
}
// 本站
if strings.Contains(pUrl.Host, config.Config["domain"]) {
util.Redirect(rw, req, tUrl)
return
}
// 检测是否禁止了 iframe 加载
// 看是否在黑名单中
for _, denyHost := range strings.Split(config.Config["iframe_deny"], ",") {
if strings.Contains(pUrl.Host, denyHost) {
util.Redirect(rw, req, tUrl)
return
}
}
// 检测会比较慢,进行异步检测,记录下来,以后分析再加黑名单
go func() {
resp, err := http.Head(tUrl)
if err != nil {
logger.Errorln("[iframe] head url:", tUrl, "error:", err)
return
}
defer resp.Body.Close()
if resp.Header.Get("X-Frame-Options") != "" {
logger.Errorln("[iframe] deny:", tUrl)
return
}
}()
}
// 设置内容模板
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/wr.html")
// 设置模板数据
filter.SetData(req, map[string]interface{}{"url": tUrl})
}
示例3: UserHomeHandler
// 用户个人首页
// URI: /user/{username}
func UserHomeHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
username := vars["username"]
// 获取用户信息
user := service.FindUserByUsername(username)
if user == nil {
util.Redirect(rw, req, "/users")
return
}
topics := service.FindRecentTopics(user.Uid, "5")
resources := service.FindUserRecentResources(user.Uid)
resourceCats := make(map[int]string)
for _, resource := range resources {
resourceCats[resource.Catid] = service.GetCategoryName(resource.Catid)
}
projects := service.FindUserRecentProjects(user.Username)
comments := service.FindRecentComments(user.Uid, -1, "5")
// 设置模板数据
filter.SetData(req, map[string]interface{}{"activeUsers": "active", "topics": topics, "resources": resources, "resource_cats": resourceCats, "projects": projects, "comments": comments, "user": user})
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/profile.html")
}
示例4: IReadingHandler
// 点击 【我要晨读】,记录点击数,跳转
// uri: /readings/{id:[0-9]+}
func IReadingHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
url := service.IReading(vars["id"])
util.Redirect(rw, req, url)
return
}
示例5: ModifyTopicHandler
// 修改主题
// uri: /topics/modify{json:(|.json)}
func ModifyTopicHandler(rw http.ResponseWriter, req *http.Request) {
tid := req.FormValue("tid")
if tid == "" {
util.Redirect(rw, req, "/topics")
return
}
nodes := service.GenNodes()
vars := mux.Vars(req)
// 请求编辑主题页面
if req.Method != "POST" || vars["json"] == "" {
topic := service.FindTopic(tid)
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/new.html")
filter.SetData(req, map[string]interface{}{"nodes": nodes, "topic": topic, "activeTopics": "active"})
return
}
user, _ := filter.CurrentUser(req)
err := service.PublishTopic(user, req.PostForm)
if err != nil {
if err == service.NotModifyAuthorityErr {
rw.WriteHeader(http.StatusForbidden)
return
}
fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`)
return
}
fmt.Fprint(rw, `{"ok": 1, "data":""}`)
}
示例6: ModifyProjectHandler
// 修改项目
// uri: /project/modify{json:(|.json)}
func ModifyProjectHandler(rw http.ResponseWriter, req *http.Request) {
id := req.FormValue("id")
if id == "" {
util.Redirect(rw, req, "/projects")
return
}
vars := mux.Vars(req)
// 请求编辑项目页面
if req.Method != "POST" || vars["json"] == "" {
project := service.FindProject(id)
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/projects/new.html")
filter.SetData(req, map[string]interface{}{"project": project, "activeProjects": "active"})
return
}
user, _ := filter.CurrentUser(req)
err := service.PublishProject(user, req.PostForm)
if err != nil {
if err == service.NotModifyAuthorityErr {
rw.WriteHeader(http.StatusForbidden)
return
}
fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`)
return
}
fmt.Fprint(rw, `{"ok": 1, "data":""}`)
}
示例7: LoginHandler
// 登录
// uri : /account/login
func LoginHandler(rw http.ResponseWriter, req *http.Request) {
username := req.FormValue("username")
if username == "" || req.Method != "POST" {
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/login.html")
return
}
// 处理用户登录
passwd := req.FormValue("passwd")
userLogin, err := service.Login(username, passwd)
if err != nil {
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/login.html")
filter.SetData(req, map[string]interface{}{"username": username, "error": err.Error()})
return
}
logger.Debugf("remember_me is %q\n", req.FormValue("remember_me"))
// 登录成功,种cookie
setCookie(rw, req, userLogin.Username)
// 支持跳转到源页面
uri := "/"
values := filter.NewFlash(rw, req).Flashes("uri")
if values != nil {
uri = values[0].(string)
}
logger.Debugln("uri===", uri)
util.Redirect(rw, req, uri)
}
示例8: TopicDetailHandler
// 社区帖子详细页
// uri: /topics/{tid:[0-9]+}
func TopicDetailHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
topic, replies, err := service.FindTopicByTid(vars["tid"])
if err != nil {
util.Redirect(rw, req, "/topics")
return
}
likeFlag := 0
hadCollect := 0
user, ok := filter.CurrentUser(req)
if ok {
uid := user["uid"].(int)
tid := topic["tid"].(int)
likeFlag = service.HadLike(uid, tid, model.TYPE_TOPIC)
hadCollect = service.HadFavorite(uid, tid, model.TYPE_TOPIC)
}
service.Views.Incr(req, model.TYPE_TOPIC, util.MustInt(vars["tid"]))
// 设置内容模板
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/topics/detail.html,/template/common/comment.html")
// 设置模板数据
filter.SetData(req, map[string]interface{}{"activeTopics": "active", "topic": topic, "replies": replies, "likeflag": likeFlag, "hadcollect": hadCollect})
}
示例9: ProjectDetailHandler
// 项目详情
// uri: /p/{uniq}
func ProjectDetailHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
project := service.FindProject(vars["uniq"])
if project == nil {
util.Redirect(rw, req, "/projects")
return
}
likeFlag := 0
hadCollect := 0
user, ok := filter.CurrentUser(req)
if ok {
uid := user["uid"].(int)
likeFlag = service.HadLike(uid, project.Id, model.TYPE_PROJECT)
hadCollect = service.HadFavorite(uid, project.Id, model.TYPE_PROJECT)
}
service.Views.Incr(req, model.TYPE_PROJECT, project.Id)
// 为了阅读数即时看到
project.Viewnum++
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/projects/detail.html,/template/common/comment.html")
filter.SetData(req, map[string]interface{}{"activeProjects": "active", "project": project, "likeflag": likeFlag, "hadcollect": hadCollect})
}
示例10: ModifyResourceHandler
// 修改資源
// uri: /resources/modify{json:(|.json)}
func ModifyResourceHandler(rw http.ResponseWriter, req *http.Request) {
id := req.FormValue("id")
if id == "" {
util.Redirect(rw, req, "/resources")
return
}
vars := mux.Vars(req)
// 请求编辑資源页面
if req.Method != "POST" || vars["json"] == "" {
resource := service.FindResourceById(id)
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/resources/new.html")
filter.SetData(req, map[string]interface{}{"resource": resource, "activeResources": "active", "categories": service.AllCategory})
return
}
user, _ := filter.CurrentUser(req)
err := service.PublishResource(user, req.PostForm)
if err != nil {
if err == service.NotModifyAuthorityErr {
rw.WriteHeader(http.StatusForbidden)
return
}
fmt.Fprint(rw, `{"ok": 0, "error":"内部服务错误!"}`)
return
}
fmt.Fprint(rw, `{"ok": 1, "data":""}`)
}
示例11: ResetPasswdHandler
// 重置密码
// uri: /account/resetpwd
func ResetPasswdHandler(rw http.ResponseWriter, req *http.Request) {
if _, ok := filter.CurrentUser(req); ok {
util.Redirect(rw, req, "/")
return
}
uuid := req.FormValue("code")
if uuid == "" {
util.Redirect(rw, req, "/account/login")
return
}
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/reset_pwd.html")
data := map[string]interface{}{"activeUsers": "active"}
passwd := req.FormValue("passwd")
email, ok := resetPwdMap[uuid]
if !ok {
// 是提交重置密码
if passwd != "" && req.Method == "POST" {
data["error"] = template.HTML(`非法请求!<p>将在<span id="jumpTo">3</span>秒后跳转到<a href="/" id="jump_url">首页</a></p>`)
} else {
data["error"] = template.HTML(`链接无效或过期,请重新操作。<a href="/account/forgetpwd">忘记密码?</a>`)
}
filter.SetData(req, data)
return
}
data["valid"] = true
data["code"] = uuid
// 提交修改密码
if passwd != "" && req.Method == "POST" {
// 简单校验
if len(passwd) < 6 || len(passwd) > 32 {
data["error"] = "密码长度必须在6到32个字符之间"
} else if passwd != req.FormValue("pass2") {
data["error"] = "两次密码输入不一致"
} else {
// 更新密码
_, err := service.UpdatePasswd(email, passwd)
if err != nil {
data["error"] = "对不起,服务器错误,请重试!"
} else {
data["success"] = template.HTML(`密码重置成功,<p>将在<span id="jumpTo">3</span>秒后跳转到<a href="/account/login" id="jump_url">登录</a>页面</p>`)
}
}
}
filter.SetData(req, data)
}
示例12: LogoutHandler
// 注销
// uri : /account/logout
func LogoutHandler(rw http.ResponseWriter, req *http.Request) {
// 删除cookie信息
session, _ := filter.Store.Get(req, "user")
session.Options = &sessions.Options{Path: "/", MaxAge: -1}
session.Save(req, rw)
// 重定向得到登录页(TODO:重定向到什么页面比较好?)
util.Redirect(rw, req, "/account/login")
}
示例13: PreFilter
func (this *CookieFilter) PreFilter(rw http.ResponseWriter, req *http.Request) bool {
user, _ := CurrentUser(req)
// 已登录且请求登录页面
if user != nil && req.RequestURI == "/account/login" {
util.Redirect(rw, req, "/")
}
return true
}
示例14: PreFilter
func (this *LoginFilter) PreFilter(rw http.ResponseWriter, req *http.Request) bool {
logger.Debugln("LoginFilter PreFilter...")
if _, ok := CurrentUser(req); !ok {
logger.Debugln("需要登录")
// 没有登录
util.Redirect(rw, req, "/account/login")
return false
}
return true
}
示例15: EmailUnsubHandler
// 邮件订阅/退订页面
// URI: /user/email/unsubscribe{json:(|.json)}
func EmailUnsubHandler(rw http.ResponseWriter, req *http.Request) {
token := req.FormValue("u")
if token == "" {
util.Redirect(rw, req, "/")
return
}
// 校验 token 的合法性
email := req.FormValue("email")
user := service.FindUserByEmail(email)
if user.Email == "" {
util.Redirect(rw, req, "/")
return
}
realToken := service.GenUnsubscribeToken(user)
if token != realToken {
util.Redirect(rw, req, "/")
return
}
vars := mux.Vars(req)
if req.Method != "POST" || vars["json"] == "" {
filter.SetData(req, map[string]interface{}{
"email": email,
"token": token,
"unsubscribe": user.Unsubscribe,
})
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/user/email_unsub.html")
return
}
unsubscribe, _ := strconv.Atoi(req.PostFormValue("unsubscribe"))
service.EmailSubscribe(user.Uid, unsubscribe)
fmt.Fprint(rw, `{"ok": 1, "msg":"保存成功"}`)
}