-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
54 lines (46 loc) · 1.31 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
// +build !testing
package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"runtime"
)
var (
proxiedURL = flag.String("url", "", "Fully qualified, absolute URL to proxy (e.g. https://example.com)")
dataDir = flag.String("data", "", "Path to a directory in which to hold the responses for this url")
host = flag.String("host", "localhost:6005", "Host/port on which to bind")
cHasher = flag.String("hasher", "", "Custom hasher program for all requests (e.g. python ./hasher.py)")
verbose = flag.Bool("verbose", false, "Turn on verbose logging")
)
func main() {
flag.Parse()
if *proxiedURL == "" || *dataDir == "" {
flag.Usage()
os.Exit(-1)
}
serverURL, err := url.Parse(*proxiedURL)
if err != nil {
log.Fatal(err)
}
if !*verbose {
log.SetOutput(ioutil.Discard)
}
runtime.GOMAXPROCS(runtime.NumCPU())
log.Printf("Starting proxy for '%v' on %v\n", serverURL.String(), *host)
var hasher Hasher
if *cHasher != "" {
hasher = CmdHasher{Command: *cHasher, Commander: DefaultCommander{}}
} else {
hasher = DefaultHasher{}
}
cacher := NewDiskCacher(*dataDir)
cacher.SeedCache()
mux := http.NewServeMux()
mux.Handle("/_seed", PreseedHandler(cacher, hasher))
mux.Handle("/", CachedProxyHandler(serverURL, cacher, hasher))
log.Fatal(http.ListenAndServe(*host, mux))
}