init.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // init_once.go contains global initialization/teardown functions that should be called exactly once
  2. // per process.
  3. package imgproxy
  4. import (
  5. "fmt"
  6. "log/slog"
  7. "go.uber.org/automaxprocs/maxprocs"
  8. "github.com/imgproxy/imgproxy/v3/config"
  9. "github.com/imgproxy/imgproxy/v3/config/loadenv"
  10. "github.com/imgproxy/imgproxy/v3/errorreport"
  11. "github.com/imgproxy/imgproxy/v3/logger"
  12. "github.com/imgproxy/imgproxy/v3/monitoring"
  13. "github.com/imgproxy/imgproxy/v3/vips"
  14. )
  15. // Init performs the global resources initialization. This should be done once per process.
  16. func Init() error {
  17. if err := loadenv.Load(); err != nil {
  18. return err
  19. }
  20. logCfg := logger.LoadConfigFromEnv(nil)
  21. if err := logger.Init(logCfg); 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. maxprocs.Set(maxprocs.Logger(func(msg string, args ...any) {
  32. slog.Debug(fmt.Sprintf(msg, args...))
  33. }))
  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. return nil
  42. }
  43. // Shutdown performs global cleanup
  44. func Shutdown() {
  45. monitoring.Stop()
  46. errorreport.Close()
  47. vips.Shutdown()
  48. }