config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "flag"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "strconv"
  10. )
  11. func intEnvConfig(i *int, name string) {
  12. if env, err := strconv.Atoi(os.Getenv(name)); err == nil {
  13. *i = env
  14. }
  15. }
  16. func strEnvConfig(s *string, name string) {
  17. if env := os.Getenv(name); len(env) > 0 {
  18. *s = env
  19. }
  20. }
  21. func hexEnvConfig(b *[]byte, name string) {
  22. var err error
  23. if env := os.Getenv(name); len(env) > 0 {
  24. if *b, err = hex.DecodeString(env); err != nil {
  25. log.Fatalf("%s expected to be hex-encoded string\n", name)
  26. }
  27. }
  28. }
  29. func hexFileConfig(b *[]byte, filepath string) {
  30. if len(filepath) == 0 {
  31. return
  32. }
  33. f, err := os.Open(filepath)
  34. if err != nil {
  35. log.Fatalf("Can't open file %s\n", filepath)
  36. }
  37. src, err := ioutil.ReadAll(f)
  38. if err != nil {
  39. log.Fatalln(err)
  40. }
  41. src = bytes.TrimSpace(src)
  42. dst := make([]byte, hex.DecodedLen(len(src)))
  43. n, err := hex.Decode(dst, src)
  44. if err != nil {
  45. log.Fatalf("%s expected to contain hex-encoded string\n", filepath)
  46. }
  47. *b = dst[:n]
  48. }
  49. type config struct {
  50. Bind string
  51. ReadTimeout int
  52. WriteTimeout int
  53. MaxSrcDimension int
  54. Quality int
  55. GZipCompression int
  56. Key []byte
  57. Salt []byte
  58. Secret string
  59. }
  60. var conf = config{
  61. Bind: ":8080",
  62. ReadTimeout: 10,
  63. WriteTimeout: 10,
  64. MaxSrcDimension: 4096,
  65. Quality: 80,
  66. GZipCompression: 5,
  67. }
  68. func init() {
  69. keypath := flag.String("keypath", "", "path of the file with hex-encoded key")
  70. saltpath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
  71. flag.Parse()
  72. strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
  73. intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
  74. intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
  75. intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
  76. intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
  77. intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
  78. hexEnvConfig(&conf.Key, "IMGPROXY_KEY")
  79. hexEnvConfig(&conf.Salt, "IMGPROXY_SALT")
  80. hexFileConfig(&conf.Key, *keypath)
  81. hexFileConfig(&conf.Salt, *saltpath)
  82. strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
  83. if len(conf.Key) == 0 {
  84. log.Fatalln("Key is not defined")
  85. }
  86. if len(conf.Salt) == 0 {
  87. log.Fatalln("Salt is not defined")
  88. }
  89. if len(conf.Bind) == 0 {
  90. log.Fatalln("Bind address is not defined")
  91. }
  92. if conf.ReadTimeout <= 0 {
  93. log.Fatalf("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout)
  94. }
  95. if conf.WriteTimeout <= 0 {
  96. log.Fatalf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
  97. }
  98. if conf.MaxSrcDimension <= 0 {
  99. log.Fatalf("Max src dimension should be greater than 0, now - %d\n", conf.MaxSrcDimension)
  100. }
  101. if conf.Quality <= 0 {
  102. log.Fatalf("Quality should be greater than 0, now - %d\n", conf.Quality)
  103. } else if conf.Quality > 100 {
  104. log.Fatalf("Quality can't be greater than 100, now - %d\n", conf.Quality)
  105. }
  106. if conf.GZipCompression < 0 {
  107. log.Fatalf("GZip compression should be greater than or quual to 0, now - %d\n", conf.GZipCompression)
  108. } else if conf.GZipCompression > 9 {
  109. log.Fatalf("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression)
  110. }
  111. }