-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (48 loc) · 2.25 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
package main
import (
"log"
"net/http"
"voltgate-proxy/caching"
"voltgate-proxy/config"
"voltgate-proxy/handler"
"voltgate-proxy/management"
"voltgate-proxy/monitoring"
"voltgate-proxy/proxy"
"voltgate-proxy/ratelimiting"
)
func main() {
println(" .__ __ __ \n___ ______ | |_/ |_ _________ _/ |_ ____ \n\\ \\/ / _ \\| |\\ __\\/ ___\\__ \\\\ __\\/ __ \\ \n \\ ( <_> ) |_| | / /_/ > __ \\| | \\ ___/ \n \\_/ \\____/|____/__| \\___ (____ /__| \\___ >\n /_____/ \\/ \\/ ")
proxyServer := proxy.NewProxyServer()
initialConfig, rateLimitRules, cacheRules := config.LoadConfig(proxyServer, "config.yaml")
monitoring.InitMetrics()
switch initialConfig.RateLimitAppConfig.Storage {
case "redis":
proxyServer.RateLimiterStorage = ratelimiting.MakeRedisRateLimiterStorage(initialConfig.Storage.Redis)
case "memory":
proxyServer.RateLimiterStorage = ratelimiting.MakeInMemoryRateLimiterStorage()
}
switch initialConfig.CacheAppConfig.Storage {
case "redis":
proxyServer.CacherStorage = caching.MakeRedisCacherStorage(initialConfig.Storage.Redis)
case "memory":
proxyServer.CacherStorage = caching.MakeInMemoryCacherStorage()
}
if len(rateLimitRules.EndpointRateLimitRules) != 0 && proxyServer.RateLimiterStorage == nil {
log.Printf("ERROR: Found rate limiting rules but rate limiter storage not initialized, please define storage in config")
panic("")
}
if len(cacheRules.EndpointCacheRules) != 0 && proxyServer.CacherStorage == nil {
log.Printf("ERROR: Found cache rules but cacher storage not initialized, please define storage in config")
panic("")
}
if initialConfig.ReloadConfigInterval != 0 {
log.Println("Config reloading enabled, interval set to", initialConfig.ReloadConfigInterval, "seconds")
go config.ReloadConfig(proxyServer, initialConfig.ReloadConfigInterval, "config.yaml")
}
go management.StartManagementServer(initialConfig)
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
handler.HandleRequest(proxyServer, &rateLimitRules, &cacheRules, writer, request)
})
log.Println("Proxy server started on", initialConfig.ProxyConfig.Address)
log.Fatal(http.ListenAndServe(initialConfig.ProxyConfig.Address, nil))
}