imgproxy.go 4.9 KB

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