forked from andreaskoch/allmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliasindex.go
46 lines (34 loc) · 1.28 KB
/
aliasindex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2015 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package orchestrator
import (
"github.com/andreaskoch/allmark/web/view/viewmodel"
"fmt"
)
// AliasIndexOrchestrator provides alias index entries.
type AliasIndexOrchestrator struct {
*Orchestrator
}
// GetIndexEntries returns a list of all alias index entry models.
func (orchestrator *AliasIndexOrchestrator) GetIndexEntries(hostname, prefix string) []viewmodel.Alias {
itemPathProvider := orchestrator.absolutePather("/")
aliasPathProvider := orchestrator.absolutePather("/" + prefix)
var aliasIndexEntries []viewmodel.Alias
for entry := range orchestrator.getAliasMap().Iter() {
alias := entry.Key
item := entry.Val
aliasIndexEntries = append(aliasIndexEntries, viewmodel.Alias{
Name: fmt.Sprintf("%s%s", prefix, alias),
Route: aliasPathProvider.Path(alias),
TargetRoute: itemPathProvider.Path(item.Route().Value()),
})
}
// sort aliases by name
viewmodel.SortAliasBy(aliasByName).Sort(aliasIndexEntries)
return aliasIndexEntries
}
// aliasByName returns true if alias 1 comes before alias 2; otherwise false.
func aliasByName(alias1, alias2 viewmodel.Alias) bool {
return alias1.Name < alias2.Name
}