This repository has been archived by the owner on Jul 11, 2018. It is now read-only.
/
main.go
62 lines (52 loc) · 1.43 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"net/http"
"log"
"flag"
"os"
"github.com/NYTimes/gziphandler"
"time"
"fmt"
"net/http/httputil"
"net/url"
)
const VERSION = "0.0.0"
func main() {
bind := flag.String("server.bind", ":1313", "Web server bind address")
ver := flag.Bool("v", false, "Print version and exit")
faktAPIHost := flag.String("api.fakt.host", "http://localhost:8080/api/v1", "Proxy api to avoid CORS crap")
flag.Parse()
if *ver {
fmt.Printf("%s", VERSION)
os.Exit(0)
}
//static assets
var staticFileServer http.Handler
if os.Getenv("DEV") == "true" {
log.Print("DEV is enabled")
staticFileServer = http.FileServer(http.Dir("ui/static"))
} else {
//in production use embedded files
staticFileServer = http.FileServer(FS(false))
}
//routing
mux := http.NewServeMux()
mux.Handle("/ui/", http.RedirectHandler("/", http.StatusMovedPermanently))
mux.Handle("/", http.StripPrefix("/", gziphandler.GzipHandler(staticFileServer)))
//API proxy
apiHostParsed, err := url.Parse(*faktAPIHost)
if err != nil {
log.Fatalf("Invalid URL for api.host: %s", *faktAPIHost)
}
log.Printf("Proxying API calls to: %s", apiHostParsed)
mux.Handle(
"/api/v1/",
http.StripPrefix("/api/v1/", httputil.NewSingleHostReverseProxy(apiHostParsed)),
)
for true {
log.Printf("Listening on %s", *bind)
err := http.ListenAndServe(*bind, mux)
log.Printf("SERVER FAILED: %s", err.Error())
time.Sleep(1 * time.Second) //retry in 1 second
}
}