go-proxy/examples/basic_rewrite/main.go

41 lines
826 B
Go

package main
import (
"log"
"net/http"
"net/url"
"forge.cadoles.com/Cadoles/go-proxy"
"forge.cadoles.com/Cadoles/go-proxy/middleware"
)
func main() {
// We declare our url rewritting mappings
mappings := map[string]*url.URL{
"127.0.0.1:*": mustParse("https://gitea.com"),
"localhost:*": mustParse("https://www.cadoles.com"),
}
// We create a new proxy with our mappings
proxy := proxy.New(
middleware.WithRewriteHosts(mappings),
)
// We start a HTTP server, listening on all interfaces with our proxy
// as an handler
if err := http.ListenAndServe("127.0.0.1:3000", proxy); err != nil {
log.Fatalf("%v", err)
}
}
// Simple utility function to transform raw urls
// to *url.URL
func mustParse(rawURL string) *url.URL {
url, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
return url
}