config.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package config
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "github.com/ghodss/yaml"
  7. "github.com/zu1k/proxypool/pkg/tool"
  8. )
  9. var (
  10. NeedFetch = true
  11. Url = "https://raw.githubusercontent.com/zu1k/proxypool/master/source.yaml"
  12. )
  13. type Source struct {
  14. Type string `json:"type" yaml:"type"`
  15. Options tool.Options `json:"options" yaml:"options"`
  16. }
  17. type Config struct {
  18. Domain string `json:"domain" yaml:"domain"`
  19. CFEmail string `json:"cf_email" yaml:"cf_email"`
  20. CFKey string `json:"cf_key" yaml:"cf_key"`
  21. Sources []Source `json:"sources" yaml:"sources"`
  22. }
  23. var SourceConfig = Config{}
  24. func Parse(path string) (*Config, error) {
  25. fileData, err := readFile(path)
  26. if err != nil {
  27. return nil, err
  28. }
  29. SourceConfig = Config{}
  30. err = yaml.Unmarshal(fileData, &SourceConfig)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if domain := os.Getenv("DOMAIN"); domain != "" {
  35. SourceConfig.Domain = domain
  36. }
  37. if cfEmail := os.Getenv("CF_API_EMAIL"); cfEmail != "" {
  38. SourceConfig.CFEmail = cfEmail
  39. }
  40. if cfKey := os.Getenv("CF_API_KEY"); cfKey != "" {
  41. SourceConfig.CFKey = cfKey
  42. }
  43. return &SourceConfig, nil
  44. }
  45. func readFile(path string) ([]byte, error) {
  46. if _, err := os.Stat(path); os.IsNotExist(err) {
  47. return nil, err
  48. }
  49. data, err := ioutil.ReadFile(path)
  50. if err != nil {
  51. return nil, err
  52. }
  53. if len(data) == 0 {
  54. return nil, fmt.Errorf("Configuration file %s is empty", path)
  55. }
  56. return data, err
  57. }