Skip to content

Instantly share code, notes, and snippets.

@cholick
Created September 23, 2023 20:16
Show Gist options
  • Save cholick/0598835599f0c22a150b4f88461bc98f to your computer and use it in GitHub Desktop.
Save cholick/0598835599f0c22a150b4f88461bc98f to your computer and use it in GitHub Desktop.
Tiny Go proxy for dev use
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