imgproxy.go 7.2 KB

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