Skip to content

Commit

Permalink
Add support to multiples virtual hosts with same behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Jul 8, 2020
1 parent 5829d17 commit d34f3bc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
18 changes: 13 additions & 5 deletions atreugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,13 @@ func (s *Atreugo) SetLogOutput(output io.Writer) {
// running on each IP address.
//
// The fact that they are running on the same atreugo instance is not apparent to the end user.
func (s *Atreugo) NewVirtualHost(hostname string) *Router {
//
// If you pass multiples hostnames, all of them will have the same behaviour.
func (s *Atreugo) NewVirtualHost(hostnames ...string) *Router {
if len(hostnames) == 0 {
panic("At least 1 hostname is required")
}

if s.virtualHosts == nil {
s.virtualHosts = make(map[string]fasthttp.RequestHandler)
}
Expand All @@ -243,11 +249,13 @@ func (s *Atreugo) NewVirtualHost(hostname string) *Router {
vHost.router.MethodNotAllowed = s.router.MethodNotAllowed
vHost.router.PanicHandler = s.router.PanicHandler

if s.virtualHosts[hostname] != nil {
panicf("a router is already registered for virtual host '%s'", hostname)
}
for _, name := range hostnames {
if s.virtualHosts[name] != nil {
panicf("a router is already registered for virtual host '%s'", name)
}

s.virtualHosts[hostname] = vHost.router.Handler
s.virtualHosts[name] = vHost.router.Handler
}

return vHost
}
40 changes: 31 additions & 9 deletions atreugo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ func TestAtreugo_SetLogOutput(t *testing.T) {

func TestAtreugo_NewVirtualHost(t *testing.T) {
hostname := "localhost"

s := New(testAtreugoConfig)

if s.virtualHosts != nil {
Expand Down Expand Up @@ -512,20 +513,41 @@ func TestAtreugo_NewVirtualHost(t *testing.T) {
t.Error("The new virtual host is not registeded")
}

defer func() {
err := recover()
type conflictArgs struct {
hostnames []string
wantErrMsg string
}

conflictHosts := []conflictArgs{
{
hostnames: []string{hostname},
wantErrMsg: fmt.Sprintf("a router is already registered for virtual host '%s'", hostname),
},
{
hostnames: []string{},
wantErrMsg: "At least 1 hostname is required",
},
{
hostnames: []string{"localhost", "localhost"},
wantErrMsg: fmt.Sprintf("a router is already registered for virtual host '%s'", hostname),
},
}

for _, test := range conflictHosts {
tt := test

err := catchPanic(func() {
s.NewVirtualHost(tt.hostnames...)
})

if err == nil {
t.Error("Expected panic when a virtual host is duplicated")
}

wantErrString := fmt.Sprintf("a router is already registered for virtual host '%s'", hostname)
if err != wantErrString {
t.Errorf("Error string == %s, want %s", err, wantErrString)
if err != tt.wantErrMsg {
t.Errorf("Error string == %s, want %s", err, tt.wantErrMsg)
}
}()

// panic when a virtual host is duplicated
s.NewVirtualHost(hostname)
}
}

// Benchmarks.
Expand Down

0 comments on commit d34f3bc

Please sign in to comment.