2015-01-31 12:15:17 -05:00
|
|
|
package config
|
|
|
|
|
2015-03-03 11:49:01 -05:00
|
|
|
import "github.com/mholt/caddy/middleware"
|
|
|
|
|
2015-01-31 12:15:17 -05:00
|
|
|
// 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
|
2015-03-03 11:49:01 -05:00
|
|
|
parser *parser
|
|
|
|
pathScope string
|
2015-01-31 12:15:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// newController returns a new controller.
|
|
|
|
func newController(p *parser) *controller {
|
|
|
|
return &controller{
|
|
|
|
dispenser: dispenser{
|
2015-02-08 00:17:15 -05:00
|
|
|
cursor: -1,
|
|
|
|
filename: p.filename,
|
2015-01-31 12:15:17 -05:00
|
|
|
},
|
2015-02-08 00:17:15 -05:00
|
|
|
parser: p,
|
2015-01-31 12:15:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2015-03-27 00:22:48 -05:00
|
|
|
// Shutdown registers a function to execute when the server exits.
|
|
|
|
func (c *controller) Shutdown(fn func() error) {
|
|
|
|
c.parser.cfg.Shutdown = append(c.parser.cfg.Shutdown, fn)
|
|
|
|
}
|
|
|
|
|
2015-01-31 12:15:17 -05:00
|
|
|
// Root returns the server root file path.
|
|
|
|
func (c *controller) Root() string {
|
|
|
|
if c.parser.cfg.Root == "" {
|
|
|
|
return "."
|
|
|
|
} else {
|
|
|
|
return c.parser.cfg.Root
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 19:11:54 -05:00
|
|
|
// Context returns the path scope that the Controller is in.
|
2015-03-03 11:49:01 -05:00
|
|
|
func (c *controller) Context() middleware.Path {
|
|
|
|
return middleware.Path(c.pathScope)
|
|
|
|
}
|