Skip to content

Instantly share code, notes, and snippets.

@elithrar
Last active August 18, 2016 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elithrar/a47af11adb818571d364cc5797ce0fb6 to your computer and use it in GitHub Desktop.
Save elithrar/a47af11adb818571d364cc5797ce0fb6 to your computer and use it in GitHub Desktop.
server.go w/ HTTP/2 proxy issues: golang/go, Issue #16788 - https://github.com/golang/go/issues/16788
package main
import (
"flag"
"net/http"
"net/http/httputil"
"net/url"
"time"
"log"
)
// Rev is the git revision set by -ldflags
var Rev string
type config struct {
Bundled bool
Entry string
Host string
Port string
Proxy string
Static string
}
func main() {
conf, err := parseConfig()
if err != nil {
log.Fatal(err)
}
r := http.NewServeMux()
// Proxy calls to the CloudFlare API.
target, err := url.Parse(conf.Proxy)
if err != nil {
log.Fatal(err)
}
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Director = func(r *http.Request) {
r.Host = target.Host
r.URL.Scheme = target.Scheme
r.URL.Host = target.Host
log.Printf("%s - %s - %s", r.Method, r.URL.String(), r.RemoteAddr)
}
r.Handle("/", proxy)
srv := &http.Server{
Handler: r,
Addr: conf.Host + ":" + conf.Port,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Printf("listening on host %s - port %s", conf.Host, conf.Port)
log.Fatal(srv.ListenAndServe())
}
func parseConfig() (*config, error) {
conf := &config{}
flag.StringVar(&conf.Entry,
"entry",
"./dist/index.html",
"the entrypoint to serve.")
flag.StringVar(&conf.Static,
"static",
"./dist/static",
"the directory to serve static files from.")
flag.StringVar(&conf.Proxy,
"proxy",
"https://api.cloudflare.com/client/v4/",
"the URL to `proxy` to")
flag.StringVar(&conf.Host,
"host",
"127.0.0.1",
"the `host` to listen on.")
flag.StringVar(&conf.Port,
"port",
"8000",
"the `port` to listen on.")
flag.BoolVar(&conf.Bundled,
"bundled",
false,
"`bundled` will use assets bundled into the binary")
flag.Parse()
return conf, nil
}
func IndexHandler(entrypoint string) func(w http.ResponseWriter, r *http.Request) {
fn := func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, entrypoint)
}
return http.HandlerFunc(fn)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment