Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
62d7d613
Commit
62d7d613
authored
Jan 30, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored the dispenser/controller
parent
ae2a2d5b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
44 deletions
+54
-44
config/dispenser.go
config/dispenser.go
+42
-32
config/parser.go
config/parser.go
+6
-6
config/parsing.go
config/parsing.go
+6
-6
No files found.
config/dispenser.go
View file @
62d7d613
...
...
@@ -5,6 +5,48 @@ import (
"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
// generators so that they can parse tokens to configure
// their instance.
...
...
@@ -18,14 +60,6 @@ type dispenser struct {
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
// was loaded; false otherwise. If false, all tokens
// have been consumed.
...
...
@@ -153,27 +187,3 @@ func (d *dispenser) Args(targets ...*string) bool {
}
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
}
config/parser.go
View file @
62d7d613
...
...
@@ -9,11 +9,11 @@ import (
// parser is a type which can parse config files.
type
parser
struct
{
filename
string
// the name of the file that we're parsing
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
other
map
[
string
]
*
dispens
er
// tokens to be parsed later by others (middleware generators)
unused
bool
// sometimes the token won't be immediately consumed
filename
string
// the name of the file that we're parsing
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
other
map
[
string
]
*
controll
er
// tokens to be parsed later by others (middleware generators)
unused
bool
// sometimes the token won't be immediately consumed
}
// newParser makes a new parser and prepares it for parsing, given
...
...
@@ -84,7 +84,7 @@ func (p *parser) next() bool {
func
(
p
*
parser
)
parseOne
()
error
{
p
.
cfg
=
Config
{}
p
.
other
=
make
(
map
[
string
]
*
dispens
er
)
p
.
other
=
make
(
map
[
string
]
*
controll
er
)
err
:=
p
.
begin
()
if
err
!=
nil
{
...
...
config/parsing.go
View file @
62d7d613
...
...
@@ -115,18 +115,18 @@ func (p *parser) collectTokens() error {
line
:=
p
.
line
()
nesting
:=
0
breakOk
:=
false
disp
:=
newDispens
er
(
p
)
cont
:=
newControll
er
(
p
)
// Re-use a duplicate directive's
dispens
er from before
// Re-use a duplicate directive's
controll
er from before
// (the parsing logic in the middleware generator must
// account for multiple occurrences of its directive, even
// if that means returning an error or overwriting settings)
if
existing
,
ok
:=
p
.
other
[
directive
];
ok
{
disp
=
existing
cont
=
existing
}
// 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
()
{
if
p
.
tkn
()
==
"{"
{
...
...
@@ -140,13 +140,13 @@ func (p *parser) collectTokens() error {
}
else
if
p
.
tkn
()
==
"}"
&&
nesting
==
0
{
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
{
return
p
.
eofErr
()
}
p
.
other
[
directive
]
=
disp
p
.
other
[
directive
]
=
cont
return
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment