1
0

imgproxy.go 5.5 KB

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