forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgproxy.go
More file actions
305 lines (248 loc) · 7.81 KB
/
Copy pathimgproxy.go
File metadata and controls
305 lines (248 loc) · 7.81 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package imgproxy
import (
"context"
"net"
"time"
"github.com/imgproxy/imgproxy/v4/auximageprovider"
"github.com/imgproxy/imgproxy/v4/clientfeatures"
"github.com/imgproxy/imgproxy/v4/cookies"
"github.com/imgproxy/imgproxy/v4/errorreport"
"github.com/imgproxy/imgproxy/v4/fetcher"
healthhandler "github.com/imgproxy/imgproxy/v4/handlers/health"
landinghandler "github.com/imgproxy/imgproxy/v4/handlers/landing"
processinghandler "github.com/imgproxy/imgproxy/v4/handlers/processing"
streamhandler "github.com/imgproxy/imgproxy/v4/handlers/stream"
"github.com/imgproxy/imgproxy/v4/httpheaders/conditionalheaders"
"github.com/imgproxy/imgproxy/v4/imagedata"
"github.com/imgproxy/imgproxy/v4/memory"
"github.com/imgproxy/imgproxy/v4/monitoring"
optionsparser "github.com/imgproxy/imgproxy/v4/options/parser"
"github.com/imgproxy/imgproxy/v4/processing"
"github.com/imgproxy/imgproxy/v4/security"
"github.com/imgproxy/imgproxy/v4/server"
"github.com/imgproxy/imgproxy/v4/workers"
)
const (
faviconPath = "/favicon.ico"
healthPath = "/health"
wellKnownPath = "/.well-known/*"
)
// ImgproxyHandlers holds the handlers for imgproxy.
type ImgproxyHandlers struct {
Health *healthhandler.Handler
Landing *landinghandler.Handler
Processing *processinghandler.Handler
Stream *streamhandler.Handler
}
// Imgproxy holds all the components needed for imgproxy to function.
type Imgproxy struct {
workers *workers.Workers
fallbackImage auximageprovider.Provider
watermarkImage auximageprovider.Provider
fetcher *fetcher.Fetcher
imageDataFactory imagedata.Factory
clientFeaturesDetector *clientfeatures.Detector
handlers ImgproxyHandlers
securityChecker *security.Checker
optionsParser *optionsparser.Parser
processor *processing.Processor
cookies *cookies.Cookies
monitoring *monitoring.Monitoring
config *Config
errorReporter *errorreport.Reporter
conditionalHeaders *conditionalheaders.Factory
}
// New creates a new imgproxy instance.
func New(ctx context.Context, config *Config) (*Imgproxy, error) {
if err := config.Validate(); err != nil {
return nil, err
}
monitoring, err := monitoring.New(ctx, &config.Monitoring, config.Workers.WorkersNumber)
if err != nil {
return nil, err
}
errorReporter, err := errorreport.New(&config.ErrorReport)
if err != nil {
return nil, err
}
securityChecker, err := security.New(&config.Security)
if err != nil {
return nil, err
}
fetcher, err := fetcher.New(&config.Fetcher)
if err != nil {
return nil, err
}
idf := imagedata.NewFactory(fetcher, monitoring)
clientFeaturesDetector := clientfeatures.NewDetector(&config.ClientFeatures)
fallbackImage, err := auximageprovider.NewStaticProvider(ctx, &config.FallbackImage, "fallback", idf)
if err != nil {
return nil, err
}
watermarkImage, err := auximageprovider.NewStaticProvider(ctx, &config.WatermarkImage, "watermark", idf)
if err != nil {
return nil, err
}
workers, err := workers.New(&config.Workers)
if err != nil {
return nil, err
}
processor, err := processing.New(&config.Processing, securityChecker, watermarkImage)
if err != nil {
return nil, err
}
optionsParser, err := optionsparser.New(ctx, &config.OptionsParser)
if err != nil {
return nil, err
}
cookies, err := cookies.New(&config.Cookies)
if err != nil {
return nil, err
}
conditionalHeaders, err := conditionalheaders.NewFactory(&config.ConditionalHeaders)
if err != nil {
return nil, err
}
imgproxy := &Imgproxy{
workers: workers,
fallbackImage: fallbackImage,
watermarkImage: watermarkImage,
fetcher: fetcher,
imageDataFactory: idf,
clientFeaturesDetector: clientFeaturesDetector,
config: config,
securityChecker: securityChecker,
optionsParser: optionsParser,
processor: processor,
cookies: cookies,
monitoring: monitoring,
errorReporter: errorReporter,
conditionalHeaders: conditionalHeaders,
}
imgproxy.handlers.Health = healthhandler.New()
imgproxy.handlers.Landing = landinghandler.New()
imgproxy.handlers.Stream, err = streamhandler.New(imgproxy, &config.Handlers.Stream)
if err != nil {
return nil, err
}
imgproxy.handlers.Processing, err = processinghandler.New(
imgproxy, imgproxy.handlers.Stream, &config.Handlers.Processing,
)
if err != nil {
return nil, err
}
return imgproxy, nil
}
// BuildRouter sets up the HTTP routes and middleware
func (i *Imgproxy) BuildRouter() (*server.Router, error) {
r, err := server.NewRouter(&i.config.Server, i.monitoring, i.errorReporter)
if err != nil {
return nil, err
}
r.GET("/", i.handlers.Landing.Execute)
r.GET("", i.handlers.Landing.Execute)
r.GET(faviconPath, r.NotFoundHandler).Silent()
r.GET(healthPath, i.handlers.Health.Execute).Silent()
r.GET(wellKnownPath, r.NotFoundHandler).Silent()
if i.config.Server.HealthCheckPath != "" {
r.GET(i.config.Server.HealthCheckPath, i.handlers.Health.Execute).Silent()
}
r.GET(
"/*", i.handlers.Processing.Execute,
r.WithSecret, r.WithCORS, r.WithPanic, r.WithReportError, r.WithMonitoring,
)
r.HEAD("/*", r.OkHandler, r.WithCORS)
r.OPTIONS("/*", r.OkHandler, r.WithCORS)
return r, nil
}
// StartServer runs the imgproxy server. This function blocks until the context is cancelled.
// If hasStarted is not nil, it will be notified with the server address once
// the server is ready or about to be ready to accept requests.
func (i *Imgproxy) StartServer(ctx context.Context, hasStarted chan net.Addr) error {
go i.startMemoryTicker(ctx)
ctx, cancel := context.WithCancel(ctx)
if err := i.monitoring.StartPrometheus(cancel); err != nil {
return err
}
router, err := i.BuildRouter()
if err != nil {
return err
}
s, err := server.Start(cancel, router)
if err != nil {
return err
}
defer s.Shutdown(context.Background())
if hasStarted != nil {
hasStarted <- s.Addr
close(hasStarted)
}
<-ctx.Done()
return nil
}
// Close gracefully shuts down the imgproxy instance
func (i *Imgproxy) Close(ctx context.Context) {
i.monitoring.Stop(ctx)
i.errorReporter.Close()
if i.fallbackImage != nil {
i.fallbackImage.Close() //nolint:errcheck
}
if i.watermarkImage != nil {
i.watermarkImage.Close() //nolint:errcheck
}
}
func (i *Imgproxy) Fetcher() *fetcher.Fetcher {
return i.fetcher
}
func (i *Imgproxy) Workers() *workers.Workers {
return i.workers
}
func (i *Imgproxy) FallbackImage() auximageprovider.Provider {
return i.fallbackImage
}
func (i *Imgproxy) WatermarkImage() auximageprovider.Provider {
return i.watermarkImage
}
func (i *Imgproxy) ImageDataFactory() imagedata.Factory {
return i.imageDataFactory
}
func (i *Imgproxy) ClientFeaturesDetector() *clientfeatures.Detector {
return i.clientFeaturesDetector
}
func (i *Imgproxy) Security() *security.Checker {
return i.securityChecker
}
func (i *Imgproxy) OptionsParser() *optionsparser.Parser {
return i.optionsParser
}
func (i *Imgproxy) Processor() *processing.Processor {
return i.processor
}
func (i *Imgproxy) Cookies() *cookies.Cookies {
return i.cookies
}
func (i *Imgproxy) Monitoring() *monitoring.Monitoring {
return i.monitoring
}
func (i *Imgproxy) ErrorReporter() *errorreport.Reporter {
return i.errorReporter
}
func (i *Imgproxy) ConditionalHeaders() *conditionalheaders.Factory {
return i.conditionalHeaders
}
// startMemoryTicker starts a ticker that periodically frees memory and optionally logs memory stats
func (i *Imgproxy) startMemoryTicker(ctx context.Context) {
ticker := time.NewTicker(i.config.Server.FreeMemoryInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
memory.Free()
if i.config.Server.LogMemStats {
memory.LogStats()
}
}
}
}