config.go 4.0 KB

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