imgproxy.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package imgproxy
  2. import (
  3. "context"
  4. "net"
  5. "time"
  6. "github.com/imgproxy/imgproxy/v3/auximageprovider"
  7. "github.com/imgproxy/imgproxy/v3/fetcher"
  8. healthhandler "github.com/imgproxy/imgproxy/v3/handlers/health"
  9. landinghandler "github.com/imgproxy/imgproxy/v3/handlers/landing"
  10. processinghandler "github.com/imgproxy/imgproxy/v3/handlers/processing"
  11. streamhandler "github.com/imgproxy/imgproxy/v3/handlers/stream"
  12. "github.com/imgproxy/imgproxy/v3/imagedata"
  13. "github.com/imgproxy/imgproxy/v3/memory"
  14. "github.com/imgproxy/imgproxy/v3/monitoring/prometheus"
  15. "github.com/imgproxy/imgproxy/v3/server"
  16. "github.com/imgproxy/imgproxy/v3/workers"
  17. )
  18. const (
  19. faviconPath = "/favicon.ico"
  20. healthPath = "/health"
  21. )
  22. // ImgproxyHandlers holds the handlers for imgproxy
  23. type ImgproxyHandlers struct {
  24. Health *healthhandler.Handler
  25. Landing *landinghandler.Handler
  26. Processing *processinghandler.Handler
  27. Stream *streamhandler.Handler
  28. }
  29. // Imgproxy holds all the components needed for imgproxy to function
  30. type Imgproxy struct {
  31. workers *workers.Workers
  32. fallbackImage auximageprovider.Provider
  33. watermarkImage auximageprovider.Provider
  34. fetcher *fetcher.Fetcher
  35. imageDataFactory *imagedata.Factory
  36. handlers ImgproxyHandlers
  37. config *Config
  38. }
  39. // New creates a new imgproxy instance
  40. func New(ctx context.Context, config *Config) (*Imgproxy, error) {
  41. fetcher, err := fetcher.New(&config.Fetcher)
  42. if err != nil {
  43. return nil, err
  44. }
  45. idf := imagedata.NewFactory(fetcher)
  46. fallbackImage, err := auximageprovider.NewStaticProvider(ctx, &config.FallbackImage, "fallback", idf)
  47. if err != nil {
  48. return nil, err
  49. }
  50. watermarkImage, err := auximageprovider.NewStaticProvider(ctx, &config.WatermarkImage, "watermark", idf)
  51. if err != nil {
  52. return nil, err
  53. }
  54. workers, err := workers.New(&config.Workers)
  55. if err != nil {
  56. return nil, err
  57. }
  58. imgproxy := &Imgproxy{
  59. workers: workers,
  60. fallbackImage: fallbackImage,
  61. watermarkImage: watermarkImage,
  62. fetcher: fetcher,
  63. imageDataFactory: idf,
  64. config: config,
  65. }
  66. imgproxy.handlers.Health = healthhandler.New()
  67. imgproxy.handlers.Landing = landinghandler.New()
  68. imgproxy.handlers.Stream, err = streamhandler.New(&config.Handlers.Stream, fetcher)
  69. if err != nil {
  70. return nil, err
  71. }
  72. imgproxy.handlers.Processing, err = processinghandler.New(
  73. imgproxy, imgproxy.handlers.Stream, &config.Handlers.Processing,
  74. )
  75. if err != nil {
  76. return nil, err
  77. }
  78. return imgproxy, nil
  79. }
  80. // BuildRouter sets up the HTTP routes and middleware
  81. func (i *Imgproxy) BuildRouter() (*server.Router, error) {
  82. r, err := server.NewRouter(&i.config.Server)
  83. if err != nil {
  84. return nil, err
  85. }
  86. r.GET("/", i.handlers.Landing.Execute)
  87. r.GET("", i.handlers.Landing.Execute)
  88. r.GET(faviconPath, r.NotFoundHandler).Silent()
  89. r.GET(healthPath, i.handlers.Health.Execute).Silent()
  90. if i.config.Server.HealthCheckPath != "" {
  91. r.GET(i.config.Server.HealthCheckPath, i.handlers.Health.Execute).Silent()
  92. }
  93. r.GET(
  94. "/*", i.handlers.Processing.Execute,
  95. r.WithSecret, r.WithCORS, r.WithPanic, r.WithReportError, r.WithMonitoring,
  96. )
  97. r.HEAD("/*", r.OkHandler, r.WithCORS)
  98. r.OPTIONS("/*", r.OkHandler, r.WithCORS)
  99. return r, nil
  100. }
  101. // Start runs the imgproxy server. This function blocks until the context is cancelled.
  102. // If hasStarted is not nil, it will be notified with the server address once
  103. // the server is ready or about to be ready to accept requests.
  104. func (i *Imgproxy) StartServer(ctx context.Context, hasStarted chan net.Addr) error {
  105. go i.startMemoryTicker(ctx)
  106. ctx, cancel := context.WithCancel(ctx)
  107. if err := prometheus.StartServer(cancel); err != nil {
  108. return err
  109. }
  110. router, err := i.BuildRouter()
  111. if err != nil {
  112. return err
  113. }
  114. s, err := server.Start(cancel, router)
  115. if err != nil {
  116. return err
  117. }
  118. defer s.Shutdown(context.Background())
  119. if hasStarted != nil {
  120. hasStarted <- s.Addr
  121. close(hasStarted)
  122. }
  123. <-ctx.Done()
  124. return nil
  125. }
  126. // startMemoryTicker starts a ticker that periodically frees memory and optionally logs memory stats
  127. func (i *Imgproxy) startMemoryTicker(ctx context.Context) {
  128. ticker := time.NewTicker(i.config.Server.FreeMemoryInterval)
  129. defer ticker.Stop()
  130. for {
  131. select {
  132. case <-ctx.Done():
  133. return
  134. case <-ticker.C:
  135. memory.Free()
  136. if i.config.Server.LogMemStats {
  137. memory.LogStats()
  138. }
  139. }
  140. }
  141. }
  142. func (i *Imgproxy) Workers() *workers.Workers {
  143. return i.workers
  144. }
  145. func (i *Imgproxy) FallbackImage() auximageprovider.Provider {
  146. return i.fallbackImage
  147. }
  148. func (i *Imgproxy) WatermarkImage() auximageprovider.Provider {
  149. return i.watermarkImage
  150. }
  151. func (i *Imgproxy) ImageDataFactory() *imagedata.Factory {
  152. return i.imageDataFactory
  153. }