imgproxy.go 6.7 KB

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