Skip to content

Commit 5875b0d

Browse files
committed
Support routing forge clients
1 parent be0f2a8 commit 5875b0d

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

server/routes.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/gorilla/mux"
66
"github.com/sirupsen/logrus"
77
"net/http"
8+
"strings"
89
"sync"
910
)
1011

@@ -95,6 +96,14 @@ type IRoutes interface {
9596

9697
var Routes IRoutes = &routesImpl{}
9798

99+
func NewRoutes() IRoutes {
100+
r := &routesImpl{
101+
mappings: make(map[string]string),
102+
}
103+
104+
return r
105+
}
106+
98107
func (r *routesImpl) RegisterAll(mappings map[string]string) {
99108
r.Lock()
100109
defer r.Unlock()
@@ -120,11 +129,13 @@ func (r *routesImpl) FindBackendForServerAddress(serverAddress string) string {
120129
r.RLock()
121130
defer r.RUnlock()
122131

132+
addressParts := strings.Split(serverAddress, `\x00`)
133+
123134
if r.mappings == nil {
124135
return r.defaultRoute
125136
} else {
126137

127-
if route, exists := r.mappings[serverAddress]; exists {
138+
if route, exists := r.mappings[addressParts[0]]; exists {
128139
return route
129140
} else {
130141
return r.defaultRoute

server/routes_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package server
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func Test_routesImpl_FindBackendForServerAddress(t *testing.T) {
8+
type args struct {
9+
serverAddress string
10+
}
11+
type mapping struct {
12+
serverAddress string
13+
backend string
14+
}
15+
tests := []struct {
16+
name string
17+
mapping mapping
18+
args args
19+
want string
20+
}{
21+
{
22+
name: "typical",
23+
mapping: mapping{
24+
serverAddress: "typical.my.domain", backend: "backend:25565",
25+
},
26+
args: args{
27+
serverAddress: `typical.my.domain`,
28+
},
29+
want: "backend:25565",
30+
},
31+
{
32+
name: "forge",
33+
mapping: mapping{
34+
serverAddress: "forge.my.domain", backend: "backend:25566",
35+
},
36+
args: args{
37+
serverAddress: `forge.my.domain\x00FML2\x00`,
38+
},
39+
want: "backend:25566",
40+
},
41+
}
42+
for _, tt := range tests {
43+
t.Run(tt.name, func(t *testing.T) {
44+
r := NewRoutes()
45+
46+
r.CreateMapping(tt.mapping.serverAddress, tt.mapping.backend)
47+
48+
if got := r.FindBackendForServerAddress(tt.args.serverAddress); got != tt.want {
49+
t.Errorf("routesImpl.FindBackendForServerAddress() = %v, want %v", got, tt.want)
50+
}
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)