-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (57 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
63
64
65
package main
import (
"caching-proxy/pkg/cache"
"caching-proxy/pkg/config"
"caching-proxy/pkg/proxy"
"context"
"flag"
"log"
"net/http"
"time"
)
func main() {
port := flag.String("port", "8080", "port to listen on")
origin := flag.String("origin", "", "origin host")
clearCache := flag.Bool("clear-cache", false, "clear cache")
configFile := flag.String("config", "config.yaml", "config file")
flag.Parse()
if *origin == "" && !*clearCache {
log.Fatal("origin server URL is required")
}
cfg := config.NewConfig()
if err := config.OverrideFromConfigYAML(cfg, *configFile); err != nil {
log.Fatal(err)
return
}
if err := config.OverrideFromEnvironment(cfg); err != nil {
log.Fatal(err)
return
}
cache := cache.New(
&cache.CacheConfig{
TTL: time.Duration(cfg.Cache.TTL),
Capacity: cfg.Cache.Capacity,
RedisAddr: cfg.Cache.Redis.Addr,
RedisDB: cfg.Cache.Redis.DB,
RedisPwd: cfg.Cache.Redis.Password,
RedisUsername: cfg.Cache.Redis.Username,
})
if *clearCache {
if err := cache.RemoveAll(context.Background()); err != nil {
log.Fatal(err)
return
}
log.Println("Cache cleared")
return
}
proxy := proxy.Proxy{
Origin: *origin,
HttpClient: &http.Client{},
Cache: cache,
}
log.Printf("ListenAndServe on port %s ...", *port)
http.HandleFunc("/", proxy.Handler())
if err := http.ListenAndServe(":"+*port, nil); err != nil {
log.Fatal(err)
}
}