karldoenitz/Tigo

redirctpermanetly无法重定向

Closed this issue · 1 comments

  • go version: all
  • Tigo version (or commit ref): all
  • operating system: all

Description

【问题描述】永久重定向无效

Operation

【操作步骤】
执行如下代码:

package main

import "github.com/karldoenitz/Tigo/TigoWeb"

type HelloWorldHandler struct {
	TigoWeb.BaseHandler
}

func (h *HelloWorldHandler) Get() {
	h.RedirectPermanently("https://www.github.com")
}

var urls = []TigoWeb.Router{
	{"/hello-world", &HelloWorldHandler{}, nil},
}

func main() {
	application := TigoWeb.Application{IPAddress: "0.0.0.0", Port: 8888, UrlRouters: urls}
	application.Run()
}

Analysis(optional)

【问题分析】

  • 先ResponseWriter.WriteHeader,再调用SetHeader,不会生效
  • 需要先进行SetHeader
    将代码改为如下即可
// RedirectPermanently 向客户端永久重定向一个地址
func (baseHandler *BaseHandler) RedirectPermanently(url string) {
	baseHandler.SetHeader("Location", url)
	baseHandler.ResponseWriter.WriteHeader(http.StatusMovedPermanently)
	baseHandler.ResponseWriter.Write(nil)
}

这个问题提的很好,谢谢指正,可以修改后直接发起pull request,我会根据情况进行code merge的。