-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomain.go
304 lines (261 loc) · 8.74 KB
/
domain.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package server
import (
"fmt"
"html/template"
"net/http"
"os"
"strings"
)
// Domain rapresents a website domain with all its
// subdomains. It's possible to set:
// - global headers, that will be applied in every connection
// - an error template, that will be used in case your logic
// will throw any error, so you will have a constant look
type Domain struct {
Name string
subdomains map[string]*Subdomain
srv *Server
headers http.Header
errTemplate *template.Template
}
// Subdomain rapresents a particular subdomain in a domain with all the
// logic. It's required a serve function, which will determine the logic
// of the website, and a Website, with all its options.
// It's possible to set:
// - default headers, that will be applied in every connection
// - an error template, that will be used in case your logic
// will throw any error, so you will have a constant look
// - the subdomain to be offline, can be reverted
// - an initializer function, called when the server is starting up
// - a cleanup function, called when the server is shutting down
type Subdomain struct {
Name string
website *Website
serveF ServeFunction
initF InitCloseFunction
closeF InitCloseFunction
headers http.Header
errTemplate *template.Template
offline bool
}
// SubdomainConfig is used to create a Subdomain. The Website and ServeF
// fields must not be nil, instead InitF and CloseF are optional
type SubdomainConfig struct {
Website Website
ServeF ServeFunction
InitF InitCloseFunction
CloseF InitCloseFunction
}
// RegisterDomain registers a domain in the server. It's asked to specify a
// display name used in the logs and the effective URL of the domain (do
// not specify any protocol or port). If the domain name is an empy string
// it will be treated as the default domain (see srv.RegisterDefaultDomain)
func (srv *Server) RegisterDomain(displayName, domain string) *Domain {
d := &Domain{
Name: displayName,
subdomains: make(map[string]*Subdomain),
srv: srv,
headers: make(http.Header),
}
srv.domains[domain] = d
return d
}
// RegisterDefaultDomain registers a domain that is called if no other domain
// matches perfectly the incoming connection
func (srv *Server) RegisterDefaultDomain(displayName string) *Domain {
return srv.RegisterDomain(displayName, "")
}
// Domain returns the domain with the given name registered in the server, if found
func (srv *Server) Domain(domain string) *Domain {
return srv.domains[domain]
}
// DefaultDomain returns the default domain, if set
func (srv *Server) DefaultDomain() *Domain {
return srv.domains[""]
}
// This is a shortcut for registering the default logic applied for every
// connection not matching any other specific domain and subdomain. It's
// the combination of srv.RegisterDefaultDomain(displayName).RegisterDefaultSubdomain(c)
func (srv *Server) RegisterDefaultRoute(displayName string, c SubdomainConfig) (*Domain, *Subdomain) {
d := srv.RegisterDefaultDomain(displayName)
sd := d.RegisterDefaultSubdomain(c)
return d, sd
}
// RegisterSubdomain registers a subdomain in the domain. It's asked to specify the
// subdomain name (with or without trailing dot) and its configuration. It the Website
// Dir field is empty it will be used the default value of "<srv.ServerPath>/public",
// instead if it's not absolute it will be relative to the srv.ServerPath
func (d *Domain) RegisterSubdomain(subdomain string, c SubdomainConfig) *Subdomain {
subdomain = prepSubdomainName(subdomain)
if c.ServeF == nil {
c.Website.AllFolders = []string{""}
if c.Website.Dir == "" {
c.Website.Dir = d.srv.ServerPath + "/public"
}
c.ServeF = func(route *Route) { route.StaticServe(true) }
}
if !isAbs(c.Website.Dir) {
if c.Website.Dir == "" {
c.Website.Dir = d.srv.ServerPath + "/public"
} else {
c.Website.Dir = d.srv.ServerPath + "/" + c.Website.Dir
}
} else {
if strings.HasPrefix(c.Website.Dir, "~") {
home, err := os.UserHomeDir()
if err == nil {
c.Website.Dir = strings.Replace(c.Website.Dir, "~", home, 1)
}
}
}
ws := new(Website)
*ws = c.Website
sd := &Subdomain{
Name: subdomain, website: ws,
serveF: c.ServeF, initF: c.InitF, closeF: c.CloseF,
headers: make(http.Header),
}
d.subdomains[subdomain] = sd
return sd
}
// RegisterDefaultSubdomain registers a subdomain that is called if no other one
// matches perfectly the incoming connection for the same domain
func (d *Domain) RegisterDefaultSubdomain(c SubdomainConfig) *Subdomain {
return d.RegisterSubdomain("*", c)
}
// Subdomain returns the subdomain with the given name, if found
func (d *Domain) Subdomain(subdomain string) *Subdomain {
return d.subdomains[subdomain]
}
// Subdomain returns the default subdomain, if set
func (d *Domain) DefaultSubdomain() *Subdomain {
return d.subdomains["*"]
}
// SetHeader adds a header to the collection of headers used in every connection
func (d *Domain) SetHeader(name, value string) *Domain {
d.headers.Set(name, value)
return d
}
// SetHeaders adds headers to the collection of headers used in every connection.
// This is a faster way to set multiple headers at the same time, instead of using
// domain.SetHeader. The headers must be provided in this way:
// headers := [][2]string {
// { "name1", "value1" },
// { "name2", "value2" },
// }
// d.SetHeaders(headers)
func (d *Domain) SetHeaders(headers [][2]string) *Domain {
for _, header := range headers {
d.SetHeader(header[0], header[1])
}
return d
}
// RemoveHeader removes a header with the given name
func (d *Domain) RemoveHeader(name string) *Domain {
d.headers.Del(name)
return d
}
// Header returns the default headers
func (d *Domain) Header() http.Header {
return d.headers
}
// EnableSubdomain enables a subdomain
func (d *Domain) EnableSubdomain(name string) *Domain {
name = prepSubdomainName(name)
sd := d.subdomains[name]
if sd != nil {
sd.Enable()
}
return d
}
// DisableSubdomain disables a subdomain
func (d *Domain) DisableSubdomain(name string) *Domain {
name = prepSubdomainName(name)
sd := d.subdomains[name]
if sd != nil {
sd.Disable()
}
return d
}
// RemoveSubdomain unregisters a subdomain
func (d *Domain) RemoveSubdomain(name string) *Domain {
delete(d.subdomains, prepSubdomainName(name))
return d
}
// SetHeader adds a header to the collection of headers used in every connection
func (sd *Subdomain) SetHeader(name, value string) *Subdomain {
sd.headers.Set(name, value)
return sd
}
// SetHeaders adds headers to the collection of headers used in every connection.
// This is a faster way to set multiple headers at the same time, instead of using
// subdomain.SetHeader. The headers must be provided in this way:
// headers := [][2]string {
// { "name1", "value1" },
// { "name2", "value2" },
// }
// d.SetHeaders(headers)
func (sd *Subdomain) SetHeaders(headers [][2]string) *Subdomain {
for _, header := range headers {
sd.SetHeader(header[0], header[1])
}
return sd
}
// RemoveHeader removes a header with the given name
func (sd *Subdomain) RemoveHeader(name string) *Subdomain {
sd.headers.Del(name)
return sd
}
// Header returns the default headers
func (sd *Subdomain) Header() http.Header {
return sd.headers
}
// Enable sets the subdomain to be online and working
func (sd *Subdomain) Enable() *Subdomain {
sd.offline = false
return sd
}
// Disable sets the subdomain to be offline
func (sd *Subdomain) Disable() *Subdomain {
sd.offline = true
return sd
}
// SetErrorTemplate sets the error template used server-wise. It's
// required an html that contains two specific fields, a .Code one and
// a .Message one, for example like so:
// <h2>Error {{ .Code }}</h2>
// <p>{{ .Message }}</p>
func (srv *Server) SetErrorTemplate(content string) error {
t, err := template.New("error.html").Parse(content)
if err != nil {
return fmt.Errorf("error parsing template file: %w", err)
}
srv.errTemplate = t
return nil
}
// SetErrorTemplate sets the error template used server-wise. It's
// required an html that contains two specific fields, a .Code one and
// a .Message one, for example like so:
// <h2>Error {{ .Code }}</h2>
// <p>{{ .Message }}</p>
func (d *Domain) SetErrorTemplate(content string) error {
t, err := template.New("error.html").Parse(content)
if err != nil {
return fmt.Errorf("error parsing template file: %w", err)
}
d.errTemplate = t
return nil
}
// SetErrorTemplate sets the error template used server-wise. It's
// required an html that contains two specific fields, a .Code one and
// a .Message one, for example like so:
// <h2>Error {{ .Code }}</h2>
// <p>{{ .Message }}</p>
func (sd *Subdomain) SetErrorTemplate(content string) error {
t, err := template.New("error.html").Parse(content)
if err != nil {
return fmt.Errorf("error parsing template file: %w", err)
}
sd.errTemplate = t
return nil
}