init.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/options"
  13. "github.com/imgproxy/imgproxy/v3/processing"
  14. "github.com/imgproxy/imgproxy/v3/vips"
  15. "go.uber.org/automaxprocs/maxprocs"
  16. )
  17. // Init performs the global resources initialization. This should be done once per process.
  18. func Init() error {
  19. if err := loadenv.Load(); err != nil {
  20. return err
  21. }
  22. if err := logger.Init(); err != nil {
  23. return err
  24. }
  25. // NOTE: This is temporary workaround. We have to load env vars in config.go before
  26. // actually configuring ImgProxy instance because for now we use it as a source of truth.
  27. // Will be removed once we move env var loading to imgproxy.go
  28. if err := config.Configure(); err != nil {
  29. return err
  30. }
  31. // NOTE: End of temporary workaround.
  32. gliblog.Init()
  33. maxprocs.Set(maxprocs.Logger(log.Debugf))
  34. if err := monitoring.Init(); err != nil {
  35. return err
  36. }
  37. if err := vips.Init(); err != nil {
  38. return err
  39. }
  40. errorreport.Init()
  41. if err := processing.ValidatePreferredFormats(); err != nil {
  42. vips.Shutdown()
  43. return err
  44. }
  45. if err := options.ParsePresets(config.Presets); err != nil {
  46. vips.Shutdown()
  47. return err
  48. }
  49. if err := options.ValidatePresets(); err != nil {
  50. vips.Shutdown()
  51. return err
  52. }
  53. return nil
  54. }
  55. // Shutdown performs global cleanup
  56. func Shutdown() {
  57. monitoring.Stop()
  58. errorreport.Close()
  59. vips.Shutdown()
  60. }