11package subdomains
22
33import (
4+ "fmt"
45 "slices"
56 "sort"
67 "strings"
@@ -17,14 +18,17 @@ import (
1718
1819var 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
200220func (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
205256func (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+
346422func (s * Subdomains ) DefaultDispatcher (r engine.Request , w engine.Response ) (* echo.Echo , bool ) {
347423 return s .FindByDomain (r .Host (), r .URL ().Path ())
348424}
0 commit comments