-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
164 lines (135 loc) 路 4.32 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
package main
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/go-redis/redis/v9"
"github.com/go-zookeeper/zk"
"github.com/gocql/gocql"
"github.com/gorilla/mux"
"github.com/hugosrc/shortlink/config"
"github.com/hugosrc/shortlink/internal/adapter/cassandra"
"github.com/hugosrc/shortlink/internal/adapter/cassandra/repository"
kafkaAdapter "github.com/hugosrc/shortlink/internal/adapter/kafka"
"github.com/hugosrc/shortlink/internal/adapter/keycloak"
redisAdapter "github.com/hugosrc/shortlink/internal/adapter/redis"
"github.com/hugosrc/shortlink/internal/adapter/zookeeper"
"github.com/hugosrc/shortlink/internal/core/service"
"github.com/hugosrc/shortlink/internal/handler/rest"
"github.com/jxskiss/base62"
"go.uber.org/zap"
)
func main() {
logger, err := zap.NewProduction()
if err != nil {
log.Fatalf("couldn't initialize zap logger: %v", err)
os.Exit(1)
}
config, err := config.Init()
if err != nil {
logger.Error("couldn't initialize configuration", zap.Error(err))
os.Exit(1)
}
cassandraConn, err := cassandra.New(config)
if err != nil {
logger.Error("couldn't connect to cassandra", zap.Error(err))
os.Exit(1)
}
redisConn, err := redisAdapter.New(config)
if err != nil {
logger.Error("couldn't connect to redis", zap.Error(err))
os.Exit(1)
}
zookeeperConn, err := zookeeper.New(config)
if err != nil {
logger.Error("couldn't connect to zookeeper", zap.Error(err))
os.Exit(1)
}
keycloakAuth := keycloak.NewOpenIDAuth(config)
kafkaProducer, err := kafkaAdapter.NewProducer(config)
if err != nil {
logger.Error("couldn't connect to kafka", zap.Error(err))
os.Exit(1)
}
logMiddleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Info("received request", zap.String("method", r.Method), zap.String("uri", r.RequestURI))
next.ServeHTTP(w, r)
})
}
server := newServer(serverConf{
Address: fmt.Sprintf(":%d", 3000),
Auth: keycloakAuth,
Cassandra: cassandraConn,
Redis: redisConn,
Zookeeper: zookeeperConn,
Kafka: kafkaProducer,
MetricsTopic: config.GetString("KAFKA_METRICS_PRODUCER_TOPIC_NAME"),
Middlewares: []func(next http.Handler) http.Handler{logMiddleware},
})
go func() {
logger.Info("server started")
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error("couldn't start the server", zap.Error(err))
}
}()
done := make(chan os.Signal)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-done
logger.Info("shutdown signal received")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() {
if err := redisConn.Close(); err != nil {
logger.Error("error closing redis connection", zap.Error(err))
}
cassandraConn.Close()
zookeeperConn.Close()
kafkaProducer.Close()
cancel()
close(done)
}()
if err := server.Shutdown(ctx); err != nil {
logger.Error("couldn't gracefully shutdown the server", zap.Error(err))
os.Exit(1)
}
logger.Info("shutdown performed successfully")
}
type serverConf struct {
Address string
Auth *keycloak.OpenIDAuth
Cassandra *gocql.Session
Redis *redis.Client
Zookeeper *zk.Conn
Kafka *kafka.Producer
MetricsTopic string
Middlewares []func(next http.Handler) http.Handler
}
func newServer(conf serverConf) *http.Server {
r := mux.NewRouter()
for _, middleware := range conf.Middlewares {
r.Use(middleware)
}
counter := zookeeper.NewCounter(conf.Zookeeper)
_ = counter.UpdateCounterBase() // TODO: goroutine
encoder := base62.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
caching := redisAdapter.NewRedisCaching(conf.Redis)
repo := repository.NewLinkRepository(conf.Cassandra)
service := service.NewLinkService(counter, encoder, caching, repo)
metricsProducer := kafkaAdapter.NewKafkaMetricsProducer(conf.MetricsTopic, conf.Kafka)
rest.NewLinkHandler(conf.Auth, metricsProducer, service).Register(r)
return &http.Server{
Addr: conf.Address,
Handler: r,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 5 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
}
}