1
0

init.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/processing"
  14. "github.com/imgproxy/imgproxy/v3/vips"
  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. logCfg := logger.LoadConfigFromEnv(nil)
  22. if err := logger.Init(logCfg); 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. maxprocs.Set(maxprocs.Logger(func(msg string, args ...any) {
  33. slog.Debug(fmt.Sprintf(msg, args...))
  34. }))
  35. if err := monitoring.Init(); err != nil {
  36. return err
  37. }
  38. if err := vips.Init(); err != nil {
  39. return err
  40. }
  41. errorreport.Init()
  42. if err := processing.ValidatePreferredFormats(); err != nil {
  43. vips.Shutdown()
  44. return err
  45. }
  46. return nil
  47. }
  48. // Shutdown performs global cleanup
  49. func Shutdown() {
  50. monitoring.Stop()
  51. errorreport.Close()
  52. vips.Shutdown()
  53. }