-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
79 lines (65 loc) · 2.01 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 (
"flag"
"net/http"
"os"
"time"
"github.com/belimawr/graceful-degradation/cache"
"github.com/belimawr/graceful-degradation/handlers"
"github.com/belimawr/graceful-degradation/resolver"
"github.com/belimawr/graceful-degradation/services/teams"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
)
var scoresURL = "https://scores-api.onefootball.com"
var addr = ":3000"
var redisURL = "localhost:6379"
var redisTimeout = 300 * time.Millisecond
var requestTimeout = 1 * time.Second
var verbose bool
var languages = []string{"en", "br", "de"}
func main() {
flag.StringVar(&redisURL, "redisURL", redisURL, "Defines redis URL")
flag.StringVar(&scoresURL, "scoresURL", scoresURL, "Defines scores URL")
flag.StringVar(&addr, "addr", addr, "Web server address")
flag.BoolVar(&verbose, "verbose", false, "Verbose logging")
flag.DurationVar(
&redisTimeout,
"redisTimeout",
redisTimeout,
"Defines redis timeouts")
flag.DurationVar(
&requestTimeout,
"requestTimeout",
requestTimeout,
"Defines the timeout set on the context of incoming requests")
flag.Parse()
if verbose {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
logger := zerolog.New(os.Stderr).
With().
Timestamp().
Logger().
Output(zerolog.ConsoleWriter{Out: os.Stderr})
fetcher := teams.New(scoresURL)
redis := cache.New(redisURL, redisTimeout)
naive := resolver.NewNaive(fetcher, languages)
cached := resolver.NewCached(redis, fetcher, languages)
r := chi.NewRouter()
r.Use(middleware.Timeout(requestTimeout))
r.Use(hlog.NewHandler(logger))
r.Use(hlog.RequestIDHandler("request_id", "Request-ID"))
r.Use(middleware.Logger)
r.Get("/naive", handlers.NewResolverHandler(naive))
r.Get("/cached", handlers.NewResolverHandler(cached))
logger.Info().Msgf("listening on: %s", addr)
if err := http.ListenAndServe(addr, r); err != nil {
logger.Fatal().Err(err).Msg("http server")
}
logger.Info().Msg("Done")
}