-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
56 lines (50 loc) · 1.26 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
package main
import (
"context"
"fmt"
"github.com/ErezLevip/fx-medium-example/cache"
"github.com/ErezLevip/fx-medium-example/handlers"
"github.com/go-redis/redis"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttprouter"
"go.uber.org/fx"
"go.uber.org/zap"
"net/http"
"os"
"os/signal"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
kill := make(chan os.Signal, 1)
signal.Notify(kill)
go func() {
<-kill
cancel()
}()
app := fx.New(
fx.Provide(newZapLogger),
fx.Provide(newRedisClient),
fx.Provide(cache.NewMeaningOfLifeCacheRedis),
fx.Provide(handlers.NewMeaningOfLifeHandler),
fx.Invoke(runHttpServer),
)
if err := app.Start(ctx); err != nil {
fmt.Println(err)
}
}
func runHttpServer(lifecycle fx.Lifecycle, molHandler *handlers.MeaningOfLife) {
lifecycle.Append(fx.Hook{OnStart: func(context.Context) error {
r := fasthttprouter.New()
r.Handle(http.MethodGet, "/what-is-the-meaning-of-life", molHandler.Handle)
return fasthttp.ListenAndServe("localhost:8080", r.Handler)
}})
}
func newRedisClient() *redis.ClusterClient {
return redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{"localhost:7000"},
})
}
func newZapLogger() *zap.Logger {
logger, _ := zap.NewProduction()
return logger
}