Skip to content

Commit 729ce38

Browse files
committed
update
1 parent cdac0d4 commit 729ce38

2 files changed

Lines changed: 130 additions & 3 deletions

File tree

subdomains/subdomains.go

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package subdomains
22

33
import (
4+
"fmt"
45
"slices"
56
"sort"
67
"strings"
@@ -17,14 +18,17 @@ import (
1718

1819
var Default = New()
1920

20-
func New() *Subdomains {
21+
func New(opts ...func(*Subdomains)) *Subdomains {
2122
s := &Subdomains{
2223
Hosts: InitSafeMap[*[]string](),
2324
Alias: InitSafeMap[*Info](),
2425
Default: ``,
2526
Protocol: `http`,
2627
}
2728
s.dispatcher = s.DefaultDispatcher
29+
for _, opt := range opts {
30+
opt(s)
31+
}
2832
return s
2933
}
3034

@@ -122,6 +126,22 @@ func (s *Subdomains) SetDispatcher(dispatcher Dispatcher) *Subdomains {
122126
return s
123127
}
124128

129+
func (s *Subdomains) SetDefault(defaultAlias string) *Subdomains {
130+
s.Default = defaultAlias
131+
return s
132+
}
133+
134+
func (s *Subdomains) SetBoot(bootAlias string) *Subdomains {
135+
s.Boot = bootAlias
136+
return s
137+
}
138+
139+
// SetProtocol http/https
140+
func (s *Subdomains) SetProtocol(protocol string) *Subdomains {
141+
s.Protocol = protocol
142+
return s
143+
}
144+
125145
// Add 添加子域名,name的值支持以下三种格式:
126146
// 1. 别名@域名 ———— 一个别名可以对应多个域名,每个域名之间用半角逗号“,”分隔
127147
// 2. 域名 ———— 可以添加多个域名,每个域名之间用半角逗号“,”分隔。这里会自动将第一个域名中的首个点号“.”前面的部分作为别名,例如“blog.webx.top,news.webx.top”会自动将“blog”作为别名
@@ -198,8 +218,39 @@ func (s *Subdomains) Add(name string, e *echo.Echo) *Subdomains {
198218
}
199219

200220
func (s *Subdomains) RemoveHost(host string) {
221+
names := s.Hosts.Get(host)
222+
if names == nil {
223+
return
224+
}
201225
s.Hosts.Remove(host)
202-
s.hostsNum.Store(int32(s.Hosts.Size()))
226+
227+
var chg bool
228+
aliases, ok := s.Hosts.GetOk(``)
229+
if !ok {
230+
cloned := make([]string, len(*names))
231+
copy(cloned, *names)
232+
aliases = &cloned
233+
s.Hosts.Set(``, aliases)
234+
chg = true
235+
} else {
236+
for _, name := range *names {
237+
if !com.InSlice(name, *aliases) {
238+
*aliases = append(*aliases, name)
239+
chg = true
240+
}
241+
}
242+
}
243+
if chg {
244+
s.sort(*aliases)
245+
}
246+
247+
//s.PrintAlias()
248+
249+
n := s.Hosts.Size()
250+
// println()
251+
// println(host, `:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>`, n)
252+
// s.PrintHosts()
253+
s.hostsNum.Store(int32(n))
203254
}
204255

205256
func (s *Subdomains) Get(args ...string) *Info {
@@ -329,12 +380,15 @@ func (s *Subdomains) FindByDomain(host string, upath string) (*echo.Echo, bool)
329380
if exists && names != nil {
330381
infos := s.Alias.Gets(*names...)
331382
cleaned, foundLocale := FixLocalePath(upath)
383+
//fmt.Printf("-----------------> upath=%s; cleaned=%s; foundLocale=%v; names=%+v; infos=%d\n", upath, cleaned, foundLocale, *names, len(infos))
332384
for _, info := range infos {
385+
//fmt.Printf("found: %s: %+v\n", info.Prefix(), *info)
333386
if info.MatchPath(upath, cleaned, foundLocale) {
334387
return info.Echo, exists
335388
}
336389
}
337390
}
391+
// s.PrintAlias()
338392
var info *Info
339393
info, exists = s.Alias.GetOk(s.Default)
340394
if exists {
@@ -343,6 +397,28 @@ func (s *Subdomains) FindByDomain(host string, upath string) (*echo.Echo, bool)
343397
return nil, exists
344398
}
345399

400+
func (s *Subdomains) PrintAlias() {
401+
fmt.Println()
402+
fmt.Println(`+------------------------[PrintAlias]----------------------+`)
403+
s.Alias.Range(func(key string, val *Info) bool {
404+
fmt.Printf("| %s: %+v\n", key, *val)
405+
return true
406+
})
407+
fmt.Println(`+----------------------------------------------------------+`)
408+
fmt.Println()
409+
}
410+
411+
func (s *Subdomains) PrintHosts() {
412+
fmt.Println()
413+
fmt.Println(`+------------------------[PrintHosts]----------------------+`)
414+
s.Hosts.Range(func(key string, val *[]string) bool {
415+
fmt.Printf("| %s: %+v\n", key, *val)
416+
return true
417+
})
418+
fmt.Println(`+----------------------------------------------------------+`)
419+
fmt.Println()
420+
}
421+
346422
func (s *Subdomains) DefaultDispatcher(r engine.Request, w engine.Response) (*echo.Echo, bool) {
347423
return s.FindByDomain(r.Host(), r.URL().Path())
348424
}

subdomains/subdomains_test.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ func TestSortHosts(t *testing.T) {
2323
e.Get(`/`, func(c echo.Context) error {
2424
return c.String(`frontend`)
2525
})
26+
e.Get(`/index`, func(c echo.Context) error {
27+
return c.String(`frontend-index`)
28+
})
2629
a.Add(`frontend`, e)
2730
e2 := echo.New()
2831
e2.SetPrefix(`/admin`)
@@ -80,18 +83,66 @@ func TestSortHosts(t *testing.T) {
8083
})
8184
assert.Equal(t, http.StatusNotFound, c)
8285

83-
a.Add(`backend@new.coscms.com`, e3)
86+
a.Add(`backend@new.coscms.com,10.0.0.1`, e3)
87+
88+
assert.Equal(t, []string{`frontend`}, *a.Hosts.Get(``))
89+
assert.Equal(t, []string{`backend`}, *a.Hosts.Get(`new.coscms.com`))
90+
assert.Equal(t, []string{`backend`}, *a.Hosts.Get(`10.0.0.1`))
91+
assert.Equal(t, int32(3), a.hostsNum.Load())
92+
8493
c, b = request(echo.GET, "/index", a, func(r *http.Request) {
8594
r.Host = `new.coscms.com`
8695
})
8796
assert.Equal(t, http.StatusOK, c)
8897
assert.Equal(t, `backend-index`, b)
8998

99+
a.SetDefault(`frontend`)
100+
c, b = request(echo.GET, "/index", a, func(r *http.Request) {
101+
r.Host = `new2.coscms.com`
102+
})
103+
assert.Equal(t, http.StatusOK, c)
104+
assert.Equal(t, `frontend-index`, b)
105+
90106
e4 := echo.New()
91107
e4.SetPrefix(`/portal`)
92108
e4.Get(`/`, func(c echo.Context) error {
93109
return c.String(`portal`)
94110
})
95111
a.Add(`portal`, e4)
96112
assert.Equal(t, []string{`portal`, `frontend`}, *a.Hosts.Get(``))
113+
114+
// ----------------------------
115+
// a.PrintAlias()
116+
// a.PrintHosts()
117+
e3.SetPrefix(`/admin`)
118+
e3.Get(``, func(c echo.Context) error {
119+
return c.String(`backend`)
120+
})
121+
e3.Get(`/index`, func(c echo.Context) error {
122+
return c.String(`backend-index`)
123+
})
124+
e3.Commit()
125+
a.Add(`backend@new.coscms.com,10.0.0.1`, e3)
126+
// a.PrintAlias()
127+
// a.PrintHosts()
128+
a.RemoveHost(`new.coscms.com`)
129+
a.RemoveHost(`10.0.0.1`)
130+
// a.PrintAlias()
131+
// a.PrintHosts()
132+
133+
assert.Equal(t, "/admin", e3.Prefix())
134+
assert.Equal(t, []string{"portal", "backend", "frontend"}, *a.Hosts.Get(``))
135+
assert.Equal(t, int32(1), a.hostsNum.Load())
136+
137+
c, b = request(echo.GET, "/index", a, func(r *http.Request) {
138+
r.Host = `new.coscms.com`
139+
})
140+
assert.Equal(t, http.StatusOK, c)
141+
assert.Equal(t, `frontend-index`, b)
142+
143+
c, b = request(echo.GET, "/admin", a, func(r *http.Request) {
144+
r.Host = `new.coscms.com`
145+
})
146+
assert.Equal(t, http.StatusOK, c)
147+
assert.Equal(t, `backend`, b)
97148
}

0 commit comments

Comments
 (0)