imgproxy.go 6.3 KB

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