forked from vektah/ior
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathior.go
64 lines (53 loc) · 1.39 KB
/
ior.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
63
64
package main
import (
"flag"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
)
var ignoreDirs []string = []string{"vendor", "node_modules", "bundler", "public", "assets"}
var bindir = flag.String("bindir", mustCwd()+"/bin", "The location to put the binary")
var binary = flag.String("binary", "", "The binary to run after installing")
var port = flag.String("port", "3000", "The port the app should listen on, will set PORT")
var upstream = flag.String("upstream", "", "Where to connect to access the app")
var listen = flag.String("listen", ":3030", "Where to listen")
var race = flag.Bool("race", false, "Build binary with -race instrumentation")
func mustCwd() string {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
return wd
}
func main() {
runtime.GOMAXPROCS(1)
flag.Parse()
if *binary == "" {
*binary = *bindir + "/" + filepath.Base(mustCwd())
}
if *upstream == "" {
*upstream = "http://localhost:" + *port
}
rpURL, err := url.Parse(*upstream)
if err != nil {
log.Fatal(err)
}
d := &Daemon{
race: *race,
}
d.Refresh()
log.Print(http.ListenAndServe(*listen, reloadMiddleware(d, NewForwarder(rpURL))))
}
func reloadMiddleware(d *Daemon, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := d.Refresh()
if err != nil {
http.Error(w, err.Error(), 400)
return
}
next.ServeHTTP(w, r)
})
}