From 508186c4db0502b4f554ec7faf1ff933ab08fda2 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Wed, 17 Feb 2021 15:51:30 +0800 Subject: [PATCH] add go program to serve localhost with proper CORS and range support --- examples/serve_localhost.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 examples/serve_localhost.go diff --git a/examples/serve_localhost.go b/examples/serve_localhost.go new file mode 100644 index 0000000..544c9db --- /dev/null +++ b/examples/serve_localhost.go @@ -0,0 +1,22 @@ +package main + +import ( + "flag" + "log" + "net/http" +) + +func main() { + port := flag.String("p", "7000", "port to serve on") + directory := flag.String("d", ".", "the directory containing PMTiles archives") + flag.Parse() + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Headers", "Range") + http.ServeFile(w, r, r.URL.Path[1:]) + }) + + log.Printf("Serving %s on HTTP port: %s\n", *directory, *port) + log.Fatal(http.ListenAndServe(":"+*port, nil)) +} \ No newline at end of file