Skip to content

Commit 733e2c6

Browse files
committed
Place root (/) paths to the bottom of the config (fixes #151)
1 parent c9c274f commit 733e2c6

File tree

2 files changed

+73
-48
lines changed

2 files changed

+73
-48
lines changed

proxy/ha_proxy_test.go

+54-47
Original file line numberDiff line numberDiff line change
@@ -433,53 +433,60 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsSortedContentFrontE
433433
s.Equal(expectedData, actualData)
434434
}
435435

436-
//func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_PutsServicesWithRootPathToTheEnd() {
437-
// var actualData string
438-
// tmpl := s.TemplateContent
439-
// expectedData := fmt.Sprintf(
440-
// `%s
441-
// acl url_acl11111 path_beg /path
442-
// use_backend my-second-service-be1111 if url_acl11111
443-
// acl url_acl21111 path_beg /path
444-
// use_backend my-first-service-be1111 if url_acl21111
445-
// acl url_the-last-service1111 path_beg /path
446-
// use_backend the-last-service-be1111 if url_the-last-service1111%s`,
447-
// tmpl,
448-
// s.ServicesContent,
449-
// )
450-
// writeFile = func(filename string, data []byte, perm os.FileMode) error {
451-
// actualData = string(data)
452-
// return nil
453-
// }
454-
// p := NewHaProxy(s.TemplatesPath, s.ConfigsPath)
455-
// // Will be listed second because of AclName
456-
// data.Services["my-first-service"] = Service{
457-
// ServiceName: "my-first-service",
458-
// AclName: "acl2",
459-
// ServiceDest: []ServiceDest{
460-
// {Port: "1111", ServicePath: []string{"/path"}},
461-
// },
462-
// }
463-
// // Will be listed last because of ServiceName (there is no AclName)
464-
// data.Services["the-last-service"] = Service{
465-
// ServiceName: "the-last-service",
466-
// ServiceDest: []ServiceDest{
467-
// {Port: "1111", ServicePath: []string{"/path"}},
468-
// },
469-
// }
470-
// // Will be listed first because of AclName
471-
// data.Services["my-second-service"] = Service{
472-
// ServiceName: "my-second-service",
473-
// AclName: "acl1",
474-
// ServiceDest: []ServiceDest{
475-
// {Port: "1111", ServicePath: []string{"/path"}},
476-
// },
477-
// }
478-
//
479-
// p.CreateConfigFromTemplates()
480-
//
481-
// s.Equal(expectedData, actualData)
482-
//}
436+
func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_PutsServicesWithRootPathToTheEnd() {
437+
var actualData string
438+
tmpl := s.TemplateContent
439+
expectedData := fmt.Sprintf(
440+
`%s
441+
acl url_01-first-service1111 path_beg /path
442+
use_backend 01-first-service-be1111 if url_01-first-service1111
443+
acl url_03-third-service1111 path_beg /path
444+
use_backend 03-third-service-be1111 if url_03-third-service1111
445+
acl url_02-another-root-service1111 path_beg /
446+
use_backend 02-another-root-service-be1111 if url_02-another-root-service1111
447+
acl url_02-root-service1111 path_beg /
448+
use_backend 02-root-service-be1111 if url_02-root-service1111%s`,
449+
tmpl,
450+
s.ServicesContent,
451+
)
452+
writeFile = func(filename string, data []byte, perm os.FileMode) error {
453+
actualData = string(data)
454+
return nil
455+
}
456+
p := NewHaProxy(s.TemplatesPath, s.ConfigsPath)
457+
// Will be listed first
458+
data.Services["01-first-service"] = Service{
459+
ServiceName: "01-first-service",
460+
ServiceDest: []ServiceDest{
461+
{Port: "1111", ServicePath: []string{"/path"}},
462+
},
463+
}
464+
// Will be listed last bacause of the root path and service name
465+
data.Services["02-root-service"] = Service{
466+
ServiceName: "02-root-service",
467+
ServiceDest: []ServiceDest{
468+
{Port: "1111", ServicePath: []string{"/"}},
469+
},
470+
}
471+
// Will be listed third because of the root path and service name
472+
data.Services["02-another-root-service"] = Service{
473+
ServiceName: "02-another-root-service",
474+
ServiceDest: []ServiceDest{
475+
{Port: "1111", ServicePath: []string{"/"}},
476+
},
477+
}
478+
// Will be listed second
479+
data.Services["03-third-service"] = Service{
480+
ServiceName: "03-third-service",
481+
ServiceDest: []ServiceDest{
482+
{Port: "1111", ServicePath: []string{"/path"}},
483+
},
484+
}
485+
486+
p.CreateConfigFromTemplates()
487+
488+
s.Equal(expectedData, actualData)
489+
}
483490

484491
func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEndTcp() {
485492
var actualData string

proxy/types.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,25 @@ func (slice Services) Len() int {
109109
}
110110

111111
func (slice Services) Less(i, j int) bool {
112-
return slice[i].AclName < slice[j].AclName
112+
firstHasRoot := hasRoot(slice[i])
113+
secondHasRoot := hasRoot(slice[j])
114+
if (firstHasRoot && secondHasRoot) || (!firstHasRoot && !secondHasRoot) {
115+
return slice[i].AclName < slice[j].AclName
116+
} else if firstHasRoot {
117+
return false
118+
}
119+
return true
120+
}
121+
122+
func hasRoot(service Service) bool {
123+
for _, sd := range service.ServiceDest {
124+
for _, path := range sd.ServicePath {
125+
if path == "/" {
126+
return true
127+
}
128+
}
129+
}
130+
return false
113131
}
114132

115133
func (slice Services) Swap(i, j int) {

0 commit comments

Comments
 (0)