1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
package main
import (
"bytes"
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"
)
type domains map[string]*domain
type domainsUpdater func(domains domains)
func (d domains) addDomain(rootDomain, group, project string, config *domainConfig) error {
newDomain := &domain{
Group: group,
Project: project,
Config: config,
}
var domainName string
if config != nil {
domainName = config.Domain
} else {
domainName = group + "." + rootDomain
}
domainName = strings.ToLower(domainName)
d[domainName] = newDomain
return nil
}
func (d domains) readProjectConfig(rootDomain, group, project string) (err error) {
var config domainsConfig
err = config.Read(group, project)
if err != nil {
return
}
for _, domainConfig := range config.Domains {
if domainConfig.Valid(rootDomain) {
d.addDomain(rootDomain, group, project, &domainConfig)
}
}
return
}
func (d domains) readProject(rootDomain, group, project string) error {
if strings.HasPrefix(project, ".") {
return errors.New("hidden project")
}
// Ignore projects that have .deleted in name
if strings.HasSuffix(project, ".deleted") {
return errors.New("deleted project")
}
_, err := os.Lstat(filepath.Join(group, project, "public"))
if err != nil {
return errors.New("missing public/ in project")
}
d.readProjectConfig(rootDomain, group, project)
return nil
}
func (d domains) readProjects(rootDomain, group string) (count int) {
projects, err := os.Open(group)
if err != nil {
return
}
defer projects.Close()
fis, err := projects.Readdir(0)
if err != nil {
log.Println("Failed to Readdir for ", group, ":", err)
}
for _, project := range fis {
// Ignore non directories
if !project.IsDir() {
continue
}
err := d.readProject(rootDomain, group, project.Name())
if err == nil {
count++
}
}
return
}
func (d domains) ReadGroups(rootDomain string) error {
groups, err := os.Open(".")
if err != nil {
return err
}
defer groups.Close()
fis, err := groups.Readdir(0)
if err != nil {
log.Println("Failed to Readdir for .:", err)
}
for _, group := range fis {
if !group.IsDir() {
continue
}
if strings.HasPrefix(group.Name(), ".") {
continue
}
count := d.readProjects(rootDomain, group.Name())
if count > 0 {
d.addDomain(rootDomain, group.Name(), "", nil)
}
}
return nil
}
func watchDomains(rootDomain string, updater domainsUpdater, interval time.Duration) {
lastUpdate := []byte("no-update")
for {
update, err := ioutil.ReadFile(".update")
if bytes.Equal(lastUpdate, update) {
if err != nil {
log.Println("Failed to read update timestamp:", err)
time.Sleep(interval)
}
continue
}
lastUpdate = update
started := time.Now()
domains := make(domains)
domains.ReadGroups(rootDomain)
duration := time.Since(started)
log.Println("Updated", len(domains), "domains in", duration)
if updater != nil {
updater(domains)
}
time.Sleep(interval)
}
}
|