config.go 3.9 KB

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