config.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package configs
  2. import (
  3. "errors"
  4. "os"
  5. "time"
  6. )
  7. var Token, BaseURL, Port, UserAgent, ProxyServer, StartingTime, Version, AcceptLanguage string
  8. func parseEnv(key string) (string, error) {
  9. value, ok := os.LookupEnv(key)
  10. if !ok {
  11. return value, errors.New("Failed to get environment variable" + key)
  12. }
  13. return value, nil
  14. }
  15. func parseEnvWithDefault(key string, defaultValue string) string {
  16. value, ok := os.LookupEnv(key)
  17. if !ok {
  18. return defaultValue
  19. }
  20. return value
  21. }
  22. func ParseConfig() error {
  23. var err error
  24. Token, err = parseEnv("PIXIVFE_TOKEN")
  25. if err != nil {
  26. return err
  27. }
  28. BaseURL = parseEnvWithDefault("PIXIVFE_BASEURL", "localhost")
  29. Port = parseEnvWithDefault("PIXIVFE_PORT", "8282")
  30. UserAgent = parseEnvWithDefault("PIXIVFE_USERAGENT", "Mozilla/5.0")
  31. ProxyServer = parseEnvWithDefault("PIXIVFE_IMAGEPROXY", "pximg.cocomi.cf")
  32. AcceptLanguage = parseEnvWithDefault("PIXIVFE_ACCEPTLANGUAGE", "en-US,en;q=0.5")
  33. StartingTime = time.Now().UTC().Format("2006-01-02 15:04")
  34. Version = "v1.0.5"
  35. return nil
  36. }