Created
September 23, 2023 20:16
-
-
Save cholick/0598835599f0c22a150b4f88461bc98f to your computer and use it in GitHub Desktop.
Tiny Go proxy for dev use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"net/http/httputil" | |
"net/url" | |
"strings" | |
) | |
//Usage: go run proxy.go --port 8888 --host localhost:3010 | |
func main() { | |
portPtr := flag.Int("port", 3030, "local port to bind") | |
hostPtr := flag.String("host", "", "host to proxy") | |
flag.Parse() | |
host := strings.TrimPrefix(*hostPtr, "http://") | |
port := *portPtr | |
if host == "" { | |
log.Fatal("host is required") | |
} | |
proxy := httputil.NewSingleHostReverseProxy(&url.URL{ | |
Scheme: "http", | |
Host: host, | |
}) | |
println(fmt.Sprintf("Proxying for %s on local port %v", host, port)) | |
err := http.ListenAndServe(fmt.Sprintf(":%d", port), proxy) | |
if err != nil { | |
println(err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment