-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
79 lines (70 loc) · 1.65 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"log/slog"
"net/http"
"os"
"github.com/gotha/blitz-proxy/storage"
"github.com/gotha/blitz-proxy/storage/dynamodb"
"github.com/gotha/blitz-proxy/storage/file"
)
func getCacheStore(storeType StoreType) storage.IStorage {
var cacheStore storage.IStorage
switch storeType {
case StoreTypeFile:
cacheStore = &file.CacheStore{}
case StoreTypeDynamodb:
store, err := dynamodb.NewCacheStoreWithEnvConfig()
if err != nil {
slog.Error("could not create dynamodb store",
slog.String("err", err.Error()),
)
os.Exit(1)
}
err = store.Init()
if err != nil {
slog.Error("could not initialize dynamodb store",
slog.String("err", err.Error()),
)
os.Exit(1)
}
cacheStore = store
default:
slog.Error("unsupported store")
os.Exit(1)
}
return cacheStore
}
func main() {
conf := NewConfigFromEnv()
registerSlogDefaultLogger(conf.SystemCode, GetLogLevel())
cacheStore := getCacheStore(conf.StoreType)
var proxy http.Handler
proxy = &Proxy{
BackendAddr: conf.BackendAddr,
HTTPClient: &http.Client{},
}
proxy = &CachingProxy{
proxy: proxy,
store: cacheStore,
}
proxy = &HeaderParsingProxy{proxy}
if conf.NoCacheList != nil && len(conf.NoCacheList) > 0 {
proxy = &NocacheProxy{
proxy: proxy,
list: conf.NoCacheList,
}
}
proxy = &CacheBustingProxy{
proxy: proxy,
store: cacheStore,
}
proxy = &LoggingProxy{proxy}
addr := conf.GetAddress()
slog.Info("Starting proxy server on",
slog.String("address", addr),
slog.String("backend", conf.BackendAddr),
)
if err := http.ListenAndServe(addr, proxy); err != nil {
slog.Error("Listen and serve", slog.String("err", err.Error()))
}
}