-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
208 lines (194 loc) · 6.71 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"github.com/paragor/simple_cdn/pkg/cache"
"github.com/paragor/simple_cdn/pkg/cachebehavior"
"github.com/paragor/simple_cdn/pkg/logger"
"github.com/paragor/simple_cdn/pkg/metrics"
"github.com/paragor/simple_cdn/pkg/upstream"
"github.com/paragor/simple_cdn/pkg/user"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v3"
"net/http"
"net/http/pprof"
"os"
"strings"
"time"
)
func getLogLevel() zapcore.Level {
switch strings.ToLower(os.Getenv("LOG_LEVEL")) {
case "info", "":
return zapcore.InfoLevel
case "debug":
return zapcore.DebugLevel
case "error":
return zapcore.ErrorLevel
case "warn":
return zapcore.WarnLevel
default:
panic("unknown logger level")
}
}
var app = "simple_cdn"
func main() {
logLevel := getLogLevel()
logger.Init(app, logLevel)
metrics.Init(app)
log := logger.Logger()
configPath := flag.String("config", "", "config path in yaml format")
checkConfig := flag.Bool("check-config", false, "only check validation and exit")
flag.Parse()
data, err := os.ReadFile(*configPath)
if err != nil {
panic(err)
}
config, err := ParseConfig(data)
if err != nil {
log.With(zap.Error(err)).Fatal("cant parse config")
}
log.With(zap.String("description", config.CanPersistCache.ToUser().String())).Info("can persist cache config")
log.With(zap.String("description", config.CanLoadCache.ToUser().String())).Info("can load cache config")
log.With(zap.String("description", config.CanForceEmitDebugLogging.ToUser().String())).Info("can force emit debug logging")
if *checkConfig {
log.Info("check-config is set, config is valid")
os.Exit(0)
}
cacheDb := config.Cache.Cache()
handler := cachebehavior.NewCacheBehavior(
config.CanPersistCache.ToUser(),
config.CanLoadCache.ToUser(),
&config.CacheKeyConfig,
config.Upstream.CreateUpstream(),
cacheDb,
config.OrderedCacheControlFallback.ToCacheControlParser(),
)
handler = logger.HttpRecoveryMiddleware(handler)
handler = logger.HttpLoggingMiddleware(handler)
handler = logger.HttpSetLoggerMiddleware(config.CanForceEmitDebugLogging.ToUser(), handler)
mainServer := http.Server{
Addr: config.ListenAddr,
Handler: handler,
}
diagnosticServer := http.Server{
Addr: config.DiagnosticAddr,
Handler: GetDiagnosticServerHandler(cacheDb),
}
diagnosticServer.RegisterOnShutdown(func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()
_ = mainServer.Shutdown(ctx)
})
mainServer.RegisterOnShutdown(func() {
_ = diagnosticServer.Close()
})
go func() {
time.Sleep(time.Second * 5)
log.Debug("starting diagnostic server")
if err = diagnosticServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.With(zap.Error(err)).Error("on listen diagnostic server")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()
_ = mainServer.Shutdown(ctx)
}()
log.Debug("starting main server")
if err = mainServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.With(zap.Error(err)).Fatal("on listen main server")
}
log.Info("good bye")
}
type Config struct {
ListenAddr string `yaml:"listen_addr"`
DiagnosticAddr string `yaml:"diagnostic_addr"`
CanPersistCache user.Config `yaml:"can_persist_cache"`
CanLoadCache user.Config `yaml:"can_load_cache"`
CanForceEmitDebugLogging user.Config `yaml:"can_force_emit_debug_logging"`
CacheKeyConfig cache.KeyConfig `yaml:"cache_key_config"`
Upstream upstream.Config `yaml:"upstream"`
Cache cache.Config `yaml:"cache"`
OrderedCacheControlFallback cachebehavior.OrderedCacheControlFallbackConfig `yaml:"ordered_cache_control_fallback"`
}
func (c *Config) Validate() error {
if c.ListenAddr == "" {
c.ListenAddr = ":8080"
}
if c.DiagnosticAddr == "" {
c.DiagnosticAddr = ":7070"
}
if err := c.CanForceEmitDebugLogging.Validate(); err != nil {
return fmt.Errorf("when_collect_debug_logging invalid: %w", err)
}
if err := c.CanPersistCache.Validate(); err != nil {
return fmt.Errorf("can_persist_cache invalid: %w", err)
}
if err := c.CanLoadCache.Validate(); err != nil {
return fmt.Errorf("can_load_cache invalid: %w", err)
}
if err := c.CacheKeyConfig.Validate(); err != nil {
return fmt.Errorf("cache_key_config invalid: %w", err)
}
if err := c.Upstream.Validate(); err != nil {
return fmt.Errorf("upstream invalid: %w", err)
}
if err := c.Cache.Validate(); err != nil {
return fmt.Errorf("cache invalid: %w", err)
}
if err := c.OrderedCacheControlFallback.Validate(); err != nil {
return fmt.Errorf("ordered_cache_control_fallback invalid: %w", err)
}
return nil
}
func ParseConfig(data []byte) (*Config, error) {
config := &Config{}
decoder := yaml.NewDecoder(bytes.NewReader(data))
decoder.KnownFields(true)
if err := decoder.Decode(config); err != nil {
return nil, fmt.Errorf("error on unmarshal: %w", err)
}
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("error on validation: %w", err)
}
return config, nil
}
func GetDiagnosticServerHandler(cacheDb cache.Cache) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/readyz", func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(200)
_, _ = writer.Write([]byte("ok"))
})
mux.HandleFunc("/healthz", func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(200)
_, _ = writer.Write([]byte("ok"))
})
mux.HandleFunc("/invalidate", func(writer http.ResponseWriter, request *http.Request) {
ctx := request.Context()
if ctx == nil {
var cancel func()
ctx, cancel = context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
}
keyPattern := request.URL.Query().Get("pattern")
if keyPattern == "" {
http.Error(writer, "query 'pattern' is empty", 400)
return
}
if err := cacheDb.Invalidate(ctx, keyPattern); err != nil {
http.Error(writer, "cant invalidate cache:"+err.Error(), 500)
}
writer.WriteHeader(200)
_, _ = writer.Write([]byte("ok"))
})
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
return mux
}