config.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. Concurrency int
  54. TTL int
  55. MaxSrcDimension int
  56. Quality int
  57. GZipCompression int
  58. Key []byte
  59. Salt []byte
  60. Secret string
  61. }
  62. var conf = config{
  63. Bind: ":8080",
  64. ReadTimeout: 10,
  65. WriteTimeout: 10,
  66. Concurrency: 100,
  67. TTL: 3600,
  68. MaxSrcDimension: 4096,
  69. Quality: 80,
  70. GZipCompression: 5,
  71. }
  72. func init() {
  73. keypath := flag.String("keypath", "", "path of the file with hex-encoded key")
  74. saltpath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
  75. flag.Parse()
  76. strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
  77. intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
  78. intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
  79. intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
  80. intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
  81. intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
  82. intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
  83. intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
  84. hexEnvConfig(&conf.Key, "IMGPROXY_KEY")
  85. hexEnvConfig(&conf.Salt, "IMGPROXY_SALT")
  86. hexFileConfig(&conf.Key, *keypath)
  87. hexFileConfig(&conf.Salt, *saltpath)
  88. strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
  89. if len(conf.Key) == 0 {
  90. log.Fatalln("Key is not defined")
  91. }
  92. if len(conf.Salt) == 0 {
  93. log.Fatalln("Salt is not defined")
  94. }
  95. if len(conf.Bind) == 0 {
  96. log.Fatalln("Bind address is not defined")
  97. }
  98. if conf.ReadTimeout <= 0 {
  99. log.Fatalf("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout)
  100. }
  101. if conf.WriteTimeout <= 0 {
  102. log.Fatalf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
  103. }
  104. if conf.Concurrency <= 0 {
  105. log.Fatalf("Concurrency should be greater than 0, now - %d\n", conf.Concurrency)
  106. }
  107. if conf.TTL <= 0 {
  108. log.Fatalf("TTL should be greater than 0, now - %d\n", conf.TTL)
  109. }
  110. if conf.MaxSrcDimension <= 0 {
  111. log.Fatalf("Max src dimension should be greater than 0, now - %d\n", conf.MaxSrcDimension)
  112. }
  113. if conf.Quality <= 0 {
  114. log.Fatalf("Quality should be greater than 0, now - %d\n", conf.Quality)
  115. } else if conf.Quality > 100 {
  116. log.Fatalf("Quality can't be greater than 100, now - %d\n", conf.Quality)
  117. }
  118. if conf.GZipCompression < 0 {
  119. log.Fatalf("GZip compression should be greater than or quual to 0, now - %d\n", conf.GZipCompression)
  120. } else if conf.GZipCompression > 9 {
  121. log.Fatalf("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression)
  122. }
  123. }