1
0

imgproxy.go 5.8 KB

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