其他分享
首页 > 其他分享> > go内置的反向代理

go内置的反向代理

作者:互联网

package main

import (
    "log"
    "net/http"
    "net/http/httputil"
    "net/url"
    "os"
    "os/signal"
    "regexp"
    "反向代理/util"
)

type ProxyHandler struct {
}

func (*ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    defer func() {
        if err := recover(); err != nil {
            w.WriteHeader(500)
            log.Println(err)
        }
    }()
    for k, v := range util.ProxyConfigs {
        if match, _ := regexp.MatchString(k, r.URL.Path); match == true {
            target, _ := url.Parse(v) //v是目标网站地址
            proxy := httputil.NewSingleHostReverseProxy(target) //go内置的反向代理
            proxy.ServeHTTP(w, r)
            return
        }
    }
    w.Write([]byte("default index"))
}

func main() {
    http.ListenAndServe(":8080", &ProxyHandler{})
    c := make(chan os.Signal)
    signal.Notify(c, os.Interrupt)
    s := <-c
    log.Println(s)

}


来自为知笔记(Wiz)

标签:内置,http,err,ProxyHandler,反向,func,go,net,os
来源: https://www.cnblogs.com/hualou/p/12070733.html