imgproxy.go 5.2 KB

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