config.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Global (Server-Wide) Settings
  2. package config
  3. import (
  4. "errors"
  5. "log"
  6. "math/rand/v2"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. var GlobalConfig ServerConfig
  13. type ServerConfig struct {
  14. // Required
  15. Token []string
  16. ProxyServer url.URL // proxy server, may contain prefix as well
  17. // can be left empty
  18. Host string
  19. // One of two is required
  20. Port string
  21. UnixSocket string
  22. UserAgent string
  23. AcceptLanguage string
  24. RequestLimit int // if 0, request limit is disabled
  25. StartingTime string
  26. Version string
  27. InDevelopment bool
  28. ProxyCheckInterval time.Duration // Proxy check interval
  29. }
  30. func (s *ServerConfig) LoadConfig() error {
  31. s.setVersion()
  32. CollectAllEnv()
  33. token, hasToken := LookupEnv("PIXIVFE_TOKEN")
  34. if !hasToken {
  35. log.Fatalln("PIXIVFE_TOKEN is required, but was not set.")
  36. return errors.New("PIXIVFE_TOKEN is required, but was not set.\n")
  37. }
  38. // TODO Maybe add some testing?
  39. s.Token = strings.Split(token, ",")
  40. port, hasPort := LookupEnv("PIXIVFE_PORT")
  41. socket, hasSocket := LookupEnv("PIXIVFE_UNIXSOCKET")
  42. if !hasPort && !hasSocket {
  43. log.Fatalln("Either PIXIVFE_PORT or PIXIVFE_UNIXSOCKET has to be set.")
  44. return errors.New("Either PIXIVFE_PORT or PIXIVFE_UNIXSOCKET has to be set.")
  45. }
  46. s.Port = port
  47. s.UnixSocket = socket
  48. _, hasDev := LookupEnv("PIXIVFE_DEV")
  49. s.InDevelopment = hasDev
  50. s.Host = GetEnv("PIXIVFE_HOST")
  51. s.UserAgent = GetEnv("PIXIVFE_USERAGENT")
  52. s.AcceptLanguage = GetEnv("PIXIVFE_ACCEPTLANGUAGE")
  53. s.SetRequestLimit(GetEnv("PIXIVFE_REQUESTLIMIT"))
  54. s.SetProxyServer(GetEnv("PIXIVFE_IMAGEPROXY"))
  55. s.SetProxyCheckInterval(GetEnv("PIXIVFE_PROXY_CHECK_INTERVAL"))
  56. AnnounceAllEnv()
  57. s.setStartingTime()
  58. return nil
  59. }
  60. func (s *ServerConfig) SetProxyServer(v string) {
  61. proxyUrl, err := url.Parse(v)
  62. if err != nil {
  63. panic(err)
  64. }
  65. s.ProxyServer = *proxyUrl
  66. if (proxyUrl.Scheme == "") != (proxyUrl.Host == "") {
  67. log.Panicf("proxy server url is weird: %s\nPlease specify e.g. https://example.com", proxyUrl.String())
  68. }
  69. if strings.HasSuffix(proxyUrl.Path, "/") {
  70. log.Panicf("proxy server path (%s) has cannot end in /: %s\nPixivFE does not support this now, sorry", proxyUrl.Path, proxyUrl.String())
  71. }
  72. }
  73. func (s *ServerConfig) SetRequestLimit(v string) {
  74. if v == "" {
  75. s.RequestLimit = 0
  76. } else {
  77. t, err := strconv.Atoi(v)
  78. if err != nil {
  79. panic(err)
  80. }
  81. s.RequestLimit = t
  82. }
  83. }
  84. func (s *ServerConfig) SetProxyCheckInterval(v string) {
  85. const defaultInterval = 480
  86. if v == "" {
  87. s.ProxyCheckInterval = defaultInterval * time.Minute
  88. } else {
  89. minutes, err := strconv.Atoi(v)
  90. if err != nil {
  91. log.Printf("Invalid PIXIVFE_PROXY_CHECK_INTERVAL value: %s. Using default of %d minutes.\n", v, defaultInterval)
  92. s.ProxyCheckInterval = defaultInterval * time.Minute
  93. } else {
  94. s.ProxyCheckInterval = time.Duration(minutes) * time.Minute
  95. }
  96. }
  97. log.Printf("Proxy check interval set to: %v\n", s.ProxyCheckInterval)
  98. }
  99. func (s *ServerConfig) setStartingTime() {
  100. s.StartingTime = time.Now().UTC().Format("2006-01-02 15:04")
  101. log.Printf("Set starting time to: %s\n", s.StartingTime)
  102. }
  103. func (s *ServerConfig) setVersion() {
  104. s.Version = "v2.8"
  105. log.Printf("PixivFE %s\n", s.Version)
  106. }
  107. func GetRandomDefaultToken() string {
  108. defaultToken := GlobalConfig.Token[rand.IntN(len(GlobalConfig.Token))]
  109. return defaultToken
  110. }