init.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // init_once.go contains global initialization/teardown functions that should be called exactly once
  2. // per process.
  3. package imgproxy
  4. import (
  5. "github.com/DataDog/datadog-agent/pkg/trace/log"
  6. "github.com/imgproxy/imgproxy/v3/config"
  7. "github.com/imgproxy/imgproxy/v3/config/loadenv"
  8. "github.com/imgproxy/imgproxy/v3/errorreport"
  9. "github.com/imgproxy/imgproxy/v3/gliblog"
  10. "github.com/imgproxy/imgproxy/v3/logger"
  11. "github.com/imgproxy/imgproxy/v3/monitoring"
  12. "github.com/imgproxy/imgproxy/v3/processing"
  13. "github.com/imgproxy/imgproxy/v3/vips"
  14. "go.uber.org/automaxprocs/maxprocs"
  15. )
  16. // Init performs the global resources initialization. This should be done once per process.
  17. func Init() error {
  18. if err := loadenv.Load(); err != nil {
  19. return err
  20. }
  21. if err := logger.Init(); err != nil {
  22. return err
  23. }
  24. // NOTE: This is temporary workaround. We have to load env vars in config.go before
  25. // actually configuring ImgProxy instance because for now we use it as a source of truth.
  26. // Will be removed once we move env var loading to imgproxy.go
  27. if err := config.Configure(); err != nil {
  28. return err
  29. }
  30. // NOTE: End of temporary workaround.
  31. gliblog.Init()
  32. maxprocs.Set(maxprocs.Logger(log.Debugf))
  33. if err := monitoring.Init(); err != nil {
  34. return err
  35. }
  36. if err := vips.Init(); err != nil {
  37. return err
  38. }
  39. errorreport.Init()
  40. if err := processing.ValidatePreferredFormats(); err != nil {
  41. vips.Shutdown()
  42. return err
  43. }
  44. return nil
  45. }
  46. // Shutdown performs global cleanup
  47. func Shutdown() {
  48. monitoring.Stop()
  49. errorreport.Close()
  50. vips.Shutdown()
  51. }