Created
December 13, 2017 09:43
-
-
Save jittuu/52209a8f277a76b82547fb8a63b99840 to your computer and use it in GitHub Desktop.
proxy for metabase to run behind IIS
This file contains hidden or 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 ( | |
"fmt" | |
"log" | |
"net/http" | |
"net/http/httputil" | |
"os" | |
"os/exec" | |
"time" | |
) | |
func main() { | |
err := startMetabase() | |
if err != nil { | |
log.Panicf("could not start metabase %s", err.Error()) | |
} | |
proxy := setupProxy() | |
http.Handle("/", proxy) | |
fmt.Println("Listening at " + os.Getenv("HTTP_PLATFORM_PORT")) | |
log.Fatal(http.ListenAndServe("localhost:"+os.Getenv("HTTP_PLATFORM_PORT"), nil)) | |
} | |
func startMetabase() error { | |
cmd := exec.Command("java.exe", "-jar", ".\\metabase.jar") | |
cmd.Stderr = os.Stderr | |
cmd.Stdout = os.Stdout | |
return cmd.Start() | |
} | |
func setupProxy() *httputil.ReverseProxy { | |
metabasePort := os.Getenv("MB_JETTY_PORT") | |
director := func(req *http.Request) { | |
waitReady() | |
req.URL.Scheme = "http" | |
req.URL.Host = "localhost:" + metabasePort | |
req.Host = req.URL.Host | |
} | |
return &httputil.ReverseProxy{Director: director} | |
} | |
var metabaseReady = false | |
func waitReady() { | |
for !metabaseReady { | |
metabasePort := os.Getenv("MB_JETTY_PORT") | |
resp, err := http.Get("http://localhost:" + metabasePort) | |
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 400 { | |
metabaseReady = true | |
} else { | |
time.Sleep(300 * time.Millisecond) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment