12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package domains
- import (
- "bufio"
- "io/ioutil"
- "os"
- "strings"
- "sync"
- "golang-fave/engine/utils"
- )
- type Domains struct {
- sync.RWMutex
- hosts map[string]string
- }
- func New(www_dir string) *Domains {
- r := Domains{}
- r.hosts = map[string]string{}
- files, err := ioutil.ReadDir(www_dir)
- if err == nil {
- for _, file := range files {
- domains_file := www_dir + string(os.PathSeparator) + file.Name() +
- string(os.PathSeparator) + "config" + string(os.PathSeparator) + ".domains"
- if utils.IsFileExists(domains_file) {
- if f, err := os.Open(domains_file); err == nil {
- defer f.Close()
- reader := bufio.NewReader(f)
- var domain string
- for {
- domain, err = reader.ReadString('\n')
- if err != nil {
- break
- }
- if strings.TrimSpace(domain) != "" {
- r.addDmain(file.Name(), strings.TrimSpace(domain))
- }
- }
- }
- }
- }
- }
- return &r
- }
- func (this *Domains) addDmain(host string, domain string) {
- this.Lock()
- defer this.Unlock()
- if _, ok := this.hosts[domain]; ok == false {
- this.hosts[domain] = host
- }
- }
- func (this *Domains) GetHost(domain string) string {
- this.Lock()
- defer this.Unlock()
- if value, ok := this.hosts[domain]; ok == true {
- return value
- }
- return ""
- }
|