mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-20 22:52:58 -05:00
Refactored the dispenser/controller
This commit is contained in:
parent
ae2a2d5b00
commit
62d7d61381
3 changed files with 54 additions and 44 deletions
|
@ -5,6 +5,48 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// newController returns a new controller.
|
||||||
|
func newController(p *parser) *controller {
|
||||||
|
return &controller{
|
||||||
|
dispenser: dispenser{
|
||||||
|
cursor: -1,
|
||||||
|
parser: p,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// controller is a dispenser of tokens and also
|
||||||
|
// facilitates setup with the server by providing
|
||||||
|
// access to its configuration. It implements
|
||||||
|
// the middleware.Controller interface.
|
||||||
|
type controller struct {
|
||||||
|
dispenser
|
||||||
|
}
|
||||||
|
|
||||||
|
// Startup registers a function to execute when the server starts.
|
||||||
|
func (c *controller) Startup(fn func() error) {
|
||||||
|
c.parser.cfg.Startup = append(c.parser.cfg.Startup, fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Root returns the server root file path.
|
||||||
|
func (c *controller) Root() string {
|
||||||
|
if c.parser.cfg.Root == "" {
|
||||||
|
return "."
|
||||||
|
} else {
|
||||||
|
return c.parser.cfg.Root
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Host returns the hostname the server is bound to.
|
||||||
|
func (c *controller) Host() string {
|
||||||
|
return c.parser.cfg.Host
|
||||||
|
}
|
||||||
|
|
||||||
|
// Port returns the port that the server is listening on.
|
||||||
|
func (c *controller) Port() string {
|
||||||
|
return c.parser.cfg.Port
|
||||||
|
}
|
||||||
|
|
||||||
// dispenser is a type that gets exposed to middleware
|
// dispenser is a type that gets exposed to middleware
|
||||||
// generators so that they can parse tokens to configure
|
// generators so that they can parse tokens to configure
|
||||||
// their instance.
|
// their instance.
|
||||||
|
@ -18,14 +60,6 @@ type dispenser struct {
|
||||||
tokens []token
|
tokens []token
|
||||||
}
|
}
|
||||||
|
|
||||||
// newDispenser returns a new dispenser.
|
|
||||||
func newDispenser(p *parser) *dispenser {
|
|
||||||
d := new(dispenser)
|
|
||||||
d.cursor = -1
|
|
||||||
d.parser = p
|
|
||||||
return d
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next loads the next token. Returns true if a token
|
// Next loads the next token. Returns true if a token
|
||||||
// was loaded; false otherwise. If false, all tokens
|
// was loaded; false otherwise. If false, all tokens
|
||||||
// have been consumed.
|
// have been consumed.
|
||||||
|
@ -153,27 +187,3 @@ func (d *dispenser) Args(targets ...*string) bool {
|
||||||
}
|
}
|
||||||
return enough
|
return enough
|
||||||
}
|
}
|
||||||
|
|
||||||
// Startup registers a function to execute when the server starts.
|
|
||||||
func (d *dispenser) Startup(fn func() error) {
|
|
||||||
d.parser.cfg.Startup = append(d.parser.cfg.Startup, fn)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Root returns the server root file path.
|
|
||||||
func (d *dispenser) Root() string {
|
|
||||||
if d.parser.cfg.Root == "" {
|
|
||||||
return "."
|
|
||||||
} else {
|
|
||||||
return d.parser.cfg.Root
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Host returns the hostname the server is bound to.
|
|
||||||
func (d *dispenser) Host() string {
|
|
||||||
return d.parser.cfg.Host
|
|
||||||
}
|
|
||||||
|
|
||||||
// Port returns the port that the server is listening on.
|
|
||||||
func (d *dispenser) Port() string {
|
|
||||||
return d.parser.cfg.Port
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ type parser struct {
|
||||||
filename string // the name of the file that we're parsing
|
filename string // the name of the file that we're parsing
|
||||||
lexer lexer // the lexer that is giving us tokens from the raw input
|
lexer lexer // the lexer that is giving us tokens from the raw input
|
||||||
cfg Config // each server gets one Config; this is the one we're currently building
|
cfg Config // each server gets one Config; this is the one we're currently building
|
||||||
other map[string]*dispenser // tokens to be parsed later by others (middleware generators)
|
other map[string]*controller // tokens to be parsed later by others (middleware generators)
|
||||||
unused bool // sometimes the token won't be immediately consumed
|
unused bool // sometimes the token won't be immediately consumed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ func (p *parser) next() bool {
|
||||||
func (p *parser) parseOne() error {
|
func (p *parser) parseOne() error {
|
||||||
p.cfg = Config{}
|
p.cfg = Config{}
|
||||||
|
|
||||||
p.other = make(map[string]*dispenser)
|
p.other = make(map[string]*controller)
|
||||||
|
|
||||||
err := p.begin()
|
err := p.begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -115,18 +115,18 @@ func (p *parser) collectTokens() error {
|
||||||
line := p.line()
|
line := p.line()
|
||||||
nesting := 0
|
nesting := 0
|
||||||
breakOk := false
|
breakOk := false
|
||||||
disp := newDispenser(p)
|
cont := newController(p)
|
||||||
|
|
||||||
// Re-use a duplicate directive's dispenser from before
|
// Re-use a duplicate directive's controller from before
|
||||||
// (the parsing logic in the middleware generator must
|
// (the parsing logic in the middleware generator must
|
||||||
// account for multiple occurrences of its directive, even
|
// account for multiple occurrences of its directive, even
|
||||||
// if that means returning an error or overwriting settings)
|
// if that means returning an error or overwriting settings)
|
||||||
if existing, ok := p.other[directive]; ok {
|
if existing, ok := p.other[directive]; ok {
|
||||||
disp = existing
|
cont = existing
|
||||||
}
|
}
|
||||||
|
|
||||||
// The directive is appended as a relevant token
|
// The directive is appended as a relevant token
|
||||||
disp.tokens = append(disp.tokens, p.lexer.token)
|
cont.tokens = append(cont.tokens, p.lexer.token)
|
||||||
|
|
||||||
for p.next() {
|
for p.next() {
|
||||||
if p.tkn() == "{" {
|
if p.tkn() == "{" {
|
||||||
|
@ -140,13 +140,13 @@ func (p *parser) collectTokens() error {
|
||||||
} else if p.tkn() == "}" && nesting == 0 {
|
} else if p.tkn() == "}" && nesting == 0 {
|
||||||
return p.err("Syntax", "Unexpected '}' because no matching open curly brace '{'")
|
return p.err("Syntax", "Unexpected '}' because no matching open curly brace '{'")
|
||||||
}
|
}
|
||||||
disp.tokens = append(disp.tokens, p.lexer.token)
|
cont.tokens = append(cont.tokens, p.lexer.token)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !breakOk || nesting > 0 {
|
if !breakOk || nesting > 0 {
|
||||||
return p.eofErr()
|
return p.eofErr()
|
||||||
}
|
}
|
||||||
|
|
||||||
p.other[directive] = disp
|
p.other[directive] = cont
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue