forked from avtocod/golang-developer-test-task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
187 lines (155 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"context"
"golang-developer-test-task/infrastructure/redclient"
"golang-developer-test-task/structs"
"net/http"
"runtime"
"strconv"
"time"
"github.com/jellydator/ttlcache/v3"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/singleflight"
// /~https://github.com/uber-go/automaxprocs
// _ "go.uber.org/automaxprocs"
"go.uber.org/zap"
)
var (
statusCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "method_status_counter",
Help: "Per method status counter",
},
[]string{"method", "status"})
timings = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "method_timing",
Help: "Per method timing",
},
[]string{"method"})
counter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "method_counter",
Help: "Per method counter",
},
[]string{"method"})
)
// func init() {
// prometheus.MustRegister(statusCounter)
// prometheus.MustRegister(timings)
// prometheus.MustRegister(counter)
//}
// StatusRecorder saves status from http.ResponseWriter
type StatusRecorder struct {
http.ResponseWriter
Status int
}
// WriteHeader saves status for further use
func (r *StatusRecorder) WriteHeader(status int) {
r.Status = status
r.ResponseWriter.WriteHeader(status)
}
func timeTrackingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
recorder := &StatusRecorder{
ResponseWriter: w,
Status: http.StatusOK,
}
next.ServeHTTP(recorder, r)
// TODO
// r.URL.Path приходит от юзера! не делайте так в проде!
status := strconv.Itoa(recorder.Status)
statusCounter.WithLabelValues(r.URL.Path, status).
Inc()
timings.
WithLabelValues(r.URL.Path).
Observe(time.Since(start).Seconds())
counter.
WithLabelValues(r.URL.Path).
Inc()
})
}
// Gzip Compression
// type gzipResponseWriter struct {
// io.Writer
// http.ResponseWriter
//}
//
// func (w gzipResponseWriter) Write(b []byte) (int, error) {
// return w.Writer.Write(b)
//}
// func Gzip(handler http.Handler) http.Handler {
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
// handler.ServeHTTP(w, r)
// return
// }
// w.Header().Set("Content-Encoding", "gzip")
// gz := gzip.NewWriter(w)
// defer gz.Close()
// gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
// handler.ServeHTTP(gzw, r)
// })
// }
func main() {
runtime.GOMAXPROCS(2)
prometheus.MustRegister(statusCounter)
prometheus.MustRegister(timings)
prometheus.MustRegister(counter)
port := "8080"
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
defer func() {
err = logger.Sync()
}()
ctx := context.Background()
conf := redclient.RedisConfig{}
conf.Load()
client := redclient.NewRedisClient(ctx, conf)
defer func() {
err = client.Close()
if err != nil {
panic(err)
}
}()
s := &singleflight.Group{}
timeout := 5 * time.Minute
cache := ttlcache.New[string, structs.PaginationObject](
ttlcache.WithTTL[string, structs.PaginationObject](timeout))
go cache.Start()
// respCache := ttlcache.New[string, string](
// ttlcache.WithTTL[string, string](timeout))
// go respCache.Start()
// var pool = &sync.Pool{
// New: func() interface{} {
// s := structs.SearchObject{}
// return &s
// }}
// var pool1 = &sync.Pool{
// New: func() interface{} {
// s := structs.PaginationObject{}
// return &s
// },
//}
// dbLogic := NewDBProcessor(client, logger, s, cache, pool, pool1)
dbLogic := NewDBProcessor(client, logger, s, cache)
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/api/load_file", dbLogic.HandleLoadFile)
mux.HandleFunc("/api/load_from_url", dbLogic.HandleLoadFromURL)
mux.HandleFunc("/api/load_from_json", dbLogic.HandleLoadJSON)
//https://nimblehq.co/blog/getting-started-with-redisearch
mux.HandleFunc("/api/search", dbLogic.HandleSearch)
mux.HandleFunc("/", dbLogic.HandleMainPage)
wrappedHandler := timeTrackingMiddleware(mux)
// wrappedHandler := Gzip(timeTrackingMiddleware(mux))
// wrappedHandler := timeTrackingMiddleware(Gzip(mux))
err = http.ListenAndServe(":"+port, wrappedHandler)
if err != nil {
panic(err)
}
}