Require frontend blocks to have the name "frontend"

This allows us to easily add other kind of toplevel directives, e.g. for
global configuration options.
This commit is contained in:
Simon Ser 2020-09-09 11:00:00 +02:00
parent af78c6600c
commit 8d2b9202b5
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 6 additions and 6 deletions

View File

@ -47,7 +47,7 @@ func (d *Directive) ChildByName(name string) *Directive {
return nil return nil
} }
func Load(path string) ([]*Directive, error) { func Load(path string) (*Directive, error) {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
@ -57,7 +57,7 @@ func Load(path string) ([]*Directive, error) {
return Parse(f) return Parse(f)
} }
func Parse(r io.Reader) ([]*Directive, error) { func Parse(r io.Reader) (*Directive, error) {
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
var directives []*Directive var directives []*Directive
@ -66,7 +66,7 @@ func Parse(r io.Reader) ([]*Directive, error) {
l := scanner.Text() l := scanner.Text()
words, err := shlex.Split(l) words, err := shlex.Split(l)
if err != nil { if err != nil {
return directives, fmt.Errorf("failed to parse config file: %v", err) return nil, fmt.Errorf("failed to parse config file: %v", err)
} else if len(words) == 0 { } else if len(words) == 0 {
continue continue
} }
@ -98,5 +98,5 @@ func Parse(r io.Reader) ([]*Directive, error) {
return nil, fmt.Errorf("failed to read config file: %v", err) return nil, fmt.Errorf("failed to read config file: %v", err)
} }
return directives, nil return &Directive{Children: directives}, nil
} }

View File

@ -10,14 +10,14 @@ import (
) )
func main() { func main() {
directives, err := Load("config") cfg, err := Load("config")
if err != nil { if err != nil {
log.Fatalf("failed to load config file: %v", err) log.Fatalf("failed to load config file: %v", err)
} }
srv := NewServer() srv := NewServer()
for _, d := range directives { for _, d := range cfg.ChildrenByName("frontend") {
if err := parseFrontend(srv, d); err != nil { if err := parseFrontend(srv, d); err != nil {
log.Fatalf("failed to parse frontend: %v", err) log.Fatalf("failed to parse frontend: %v", err)
} }