【问题标题】:HTTP Redirect in GolangGolang 中的 HTTP 重定向
【发布时间】:2015-04-10 18:29:48
【问题描述】:

我有这个域http://mynewurl.com/,哪个帧转发到- http://oldurl.com:8000 我正在为博客运行的 Go 服务器。

我需要正确地为每个帖子地图设置 URL

目前访问主页上的链接完全掩盖了 URL,因此任何共享(希望使用 webmentions)都不会引用该帖子 URL,而是主 URL……

但我想知道是否可以让 Golang 玩得更好并在新 URL 上收听?

func main() {
  http.HandleFunc("/", handlerequest)
  http.ListenAndServe(":8000", nil)
}

【问题讨论】:

  • 一个建议是 index.php 将远程页面数据拉入并将其注入到位于 newurl.com 的实际页面中
  • 又名 - CURL PHP github.com/joshdick/miniproxy

标签: go proxy httprequest reverse-proxy


【解决方案1】:

httputil.ReverseProxy

我用它来实现请求转储器,它基本上可以执行您所描述的操作 - 侦听特定端口并转发到某个 url。

这是示例代码。里程可能会有所不同,因为我只是去掉了与我在代理中所做的实际工作相关的部分,并留下了做路由的部分。 但这可能是一个起点。

func SetProxy(targetUrl string) (newUrl string, err error) {
    var target *url.URL
    target, err = url.Parse(targetUrl)
    if err != nil {
        return "", err
    }
    origHost := target.Host
    origScheme := target.Scheme
    d := func(req *http.Request) {
        req.URL.Host = origHost
        req.URL.Scheme = origScheme
    }

    p := &httputil.ReverseProxy{Director: d,}
    http.HandleFunc("/", p)

    target.Host = "localhost:8000"
    target.Scheme = "http"
    newUrl = target.String()
    go func() {
        err = http.ListenAndServe(":"+localPort, nil)
        if err != nil {
            panic(err)
        }
    }()

    return newUrl, nil
}

【讨论】:

  • 谢谢,我认为根据我的用例,这将比我预期的要复杂
猜你喜欢
  • 2021-11-23
  • 2012-08-09
  • 2018-03-27
  • 2016-12-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
相关资源
最近更新 更多