config.go 3.9 KB

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