Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Sep 23, 2024
1 parent 7628962 commit 3f2c8c8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 22 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,6 @@ github.com/volatiletech/strmangle v0.0.6 h1:AdOYE3B2ygRDq4rXDij/MMwq6KVK/pWAYxpC
github.com/volatiletech/strmangle v0.0.6/go.mod h1:ycDvbDkjDvhC0NUU8w3fWwl5JEMTV56vTKXzR3GeR+0=
github.com/webx-top/captcha v0.1.0 h1:uBTGMevM0tlII5Zyj4QbJPI6vTPI0uF+BA4zKLA8avU=
github.com/webx-top/captcha v0.1.0/go.mod h1:E7chb3O5Dqbcta3hBEGRGXGreItjbjPy72ihuqS4+d4=
github.com/webx-top/com v1.3.2 h1:ruR2zSx9yDLPZq2HQG2cutIAo2CDz0b1M1mvJ4BjsRg=
github.com/webx-top/com v1.3.2/go.mod h1:DDfATzu1w5+vD5XmG3YRTfLjaIqZWi/yeJ7HQEGsM2Q=
github.com/webx-top/com v1.3.3 h1:EZih/Js5BT6hnz72HjkAi1Iwnl4yxKmOwxxA3sMH6LY=
github.com/webx-top/com v1.3.3/go.mod h1:DDfATzu1w5+vD5XmG3YRTfLjaIqZWi/yeJ7HQEGsM2Q=
github.com/webx-top/poolx v0.0.0-20210912044716-5cfa2d58e380 h1:YUDmvTQjrixwGCtlJFm1piLLo7lxMEBnWUqMrlnqyxM=
Expand Down
83 changes: 63 additions & 20 deletions subdomains/subdomains.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package subdomains

import (
"sort"
"strings"

"github.com/admpub/log"
Expand All @@ -15,7 +16,7 @@ var Default = New()

func New() *Subdomains {
s := &Subdomains{
Hosts: map[string]string{},
Hosts: map[string][]string{},
Alias: map[string]*Info{},
Default: ``,
Protocol: `http`,
Expand Down Expand Up @@ -82,8 +83,9 @@ func (info *Info) RelativeURLByName(s *Subdomains, name string, args ...interfac
type Dispatcher func(r engine.Request, w engine.Response) (*echo.Echo, bool)

type Subdomains struct {
Hosts map[string]string //{host:name}
Hosts map[string][]string //{host:name}
Alias map[string]*Info
Prefixes []string
Default string //default name
Protocol string //http/https
Boot string
Expand Down Expand Up @@ -116,20 +118,29 @@ func (s *Subdomains) Add(name string, e *echo.Echo) *Subdomains {
}
}
for _, host := range hosts {
s.Hosts[host] = name
if _, ok := s.Hosts[host]; !ok {
s.Hosts[host] = []string{name}
} else {
s.Hosts[host] = append(s.Hosts[host], name)
}
}
info := &Info{
Protocol: `http`,
Name: name,
Host: hosts[0],
Echo: e,
}
info2 := strings.SplitN(info.Host, `://`, 2)
if len(info2) == 2 {
info.Protocol = info2[0]
info.Host = info2[1]
if len(info.Protocol) == 0 {
info.Protocol = "http"
if strings.HasPrefix(info.Host, `/`) {
info.Host = ``
}
if len(info.Host) > 0 {
info2 := strings.SplitN(info.Host, `://`, 2)
if len(info2) == 2 {
info.Protocol = info2[0]
info.Host = info2[1]
if len(info.Protocol) == 0 {
info.Protocol = "http"
}
}
}
s.Alias[name] = info
Expand Down Expand Up @@ -209,26 +220,56 @@ func (s *Subdomains) RelativeURLByName(name string, params ...interface{}) strin
return info.RelativeURLByName(s, name, params...)
}

func (s *Subdomains) FindByDomain(host string) (*echo.Echo, bool) {
name, exists := s.Hosts[host]
if !exists {
if p := strings.LastIndexByte(host, ':'); p > -1 {
name, exists = s.Hosts[host[0:p]]
}
func (s *Subdomains) sort(names []string) []string {
sort.Slice(names, func(i, j int) bool {
return len(s.Alias[names[i]].Prefix()) > len(s.Alias[names[j]].Prefix())
})
return names
}

func (s *Subdomains) sortHosts() {
for k := range s.Hosts {
s.Hosts[k] = s.sort(s.Hosts[k])
}
}

func (s *Subdomains) FindByDomain(host string, upath string) (*echo.Echo, bool) {
var (
names []string
exists bool
)
if len(s.Hosts) == 1 && len(s.Hosts[``]) > 0 {
names = s.Hosts[``]
exists = true
} else {
names, exists = s.Hosts[host]
if !exists {
name = s.Default
if p := strings.LastIndexByte(host, ':'); p > -1 {
names, exists = s.Hosts[host[0:p]]
if !exists {
names, exists = s.Hosts[``]
}
}
}
}
var info *Info
info, exists = s.Alias[name]
if exists {
return info.Echo, true
for _, name := range names {
info, exists = s.Alias[name]
if exists && strings.HasPrefix(upath, info.Prefix()) {
return info.Echo, exists
}
}
}
info, exists = s.Alias[s.Default]
if exists {
return info.Echo, exists
}
return nil, false
return nil, exists
}

func (s *Subdomains) DefaultDispatcher(r engine.Request, w engine.Response) (*echo.Echo, bool) {
return s.FindByDomain(r.Host())
return s.FindByDomain(r.Host(), r.URL().Path())
}

func (s *Subdomains) ServeHTTP(r engine.Request, w engine.Response) {
Expand All @@ -244,6 +285,8 @@ func (s *Subdomains) Run(args ...interface{}) {
if s.dispatcher == nil {
s.dispatcher = s.DefaultDispatcher
}
s.sortHosts()
echo.Dump(s.Hosts)
var eng engine.Engine
var arg interface{}
size := len(args)
Expand Down
19 changes: 19 additions & 0 deletions subdomains/subdomains_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package subdomains

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/webx-top/echo"
)

func TestSortHosts(t *testing.T) {
a := New()
e := echo.New()
a.Add(`frontend`, e)
e2 := echo.New()
e2.SetPrefix(`/admin`)
a.Add(`backend`, e2)
a.sortHosts()
assert.Equal(t, []string{`backend`, `frontend`}, a.Hosts[``])
}

0 comments on commit 3f2c8c8

Please sign in to comment.