domains.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package domains
  2. import (
  3. "bufio"
  4. "io/ioutil"
  5. "os"
  6. "strings"
  7. "sync"
  8. "golang-fave/engine/utils"
  9. )
  10. type Domains struct {
  11. sync.RWMutex
  12. hosts map[string]string
  13. }
  14. func New(www_dir string) *Domains {
  15. r := Domains{}
  16. r.hosts = map[string]string{}
  17. files, err := ioutil.ReadDir(www_dir)
  18. if err == nil {
  19. for _, file := range files {
  20. domains_file := www_dir + string(os.PathSeparator) + file.Name() +
  21. string(os.PathSeparator) + "config" + string(os.PathSeparator) + ".domains"
  22. if utils.IsFileExists(domains_file) {
  23. if f, err := os.Open(domains_file); err == nil {
  24. defer f.Close()
  25. reader := bufio.NewReader(f)
  26. var domain string
  27. for {
  28. domain, err = reader.ReadString('\n')
  29. if err != nil {
  30. break
  31. }
  32. if strings.TrimSpace(domain) != "" {
  33. r.addDmain(file.Name(), strings.TrimSpace(domain))
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. return &r
  41. }
  42. func (this *Domains) addDmain(host string, domain string) {
  43. this.Lock()
  44. defer this.Unlock()
  45. if _, ok := this.hosts[domain]; ok == false {
  46. this.hosts[domain] = host
  47. }
  48. }
  49. func (this *Domains) GetHost(domain string) string {
  50. this.Lock()
  51. defer this.Unlock()
  52. if value, ok := this.hosts[domain]; ok == true {
  53. return value
  54. }
  55. return ""
  56. }