mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-26 10:13:39 +08:00
making directives externally registerable
This commit is contained in:
parent
5b7e0361dd
commit
e72fc20c78
|
@ -68,6 +68,23 @@ var directiveOrder = []directive{
|
||||||
{"browse", setup.Browse},
|
{"browse", setup.Browse},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegisterDirective adds the given directive to caddy's list of directives.
|
||||||
|
// Pass the name of a directive you want it to be placed after,
|
||||||
|
// otherwise it will be placed at the bottom of the stack.
|
||||||
|
func RegisterDirective(name string, setup SetupFunc, after string) {
|
||||||
|
dir := directive{name: name, setup: setup}
|
||||||
|
idx := len(directiveOrder)
|
||||||
|
for i := range directiveOrder {
|
||||||
|
if directiveOrder[i].name == after {
|
||||||
|
idx = i + 1
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newDirectives := append(directiveOrder[:idx], append([]directive{dir}, directiveOrder[idx:]...)...)
|
||||||
|
directiveOrder = newDirectives
|
||||||
|
parse.ValidDirectives[name] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
// directive ties together a directive name with its setup function.
|
// directive ties together a directive name with its setup function.
|
||||||
type directive struct {
|
type directive struct {
|
||||||
name string
|
name string
|
||||||
|
|
31
caddy/directives_test.go
Normal file
31
caddy/directives_test.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package caddy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRegister(t *testing.T) {
|
||||||
|
directives := []directive{
|
||||||
|
{"dummy", nil},
|
||||||
|
{"dummy2", nil},
|
||||||
|
}
|
||||||
|
directiveOrder = directives
|
||||||
|
RegisterDirective("foo", nil, "dummy")
|
||||||
|
if len(directiveOrder) != 3 {
|
||||||
|
t.Fatal("Should have 3 directives now")
|
||||||
|
}
|
||||||
|
getNames := func() (s []string) {
|
||||||
|
for _, d := range directiveOrder {
|
||||||
|
s = append(s, d.name)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(getNames(), []string{"dummy", "foo", "dummy2"}) {
|
||||||
|
t.Fatalf("directive order doesn't match: %s", getNames())
|
||||||
|
}
|
||||||
|
RegisterDirective("bar", nil, "ASDASD")
|
||||||
|
if !reflect.DeepEqual(getNames(), []string{"dummy", "foo", "dummy2", "bar"}) {
|
||||||
|
t.Fatalf("directive order doesn't match: %s", getNames())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user