2015-05-04 12:04:17 -05:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-05-05 16:50:42 -05:00
|
|
|
"net/http"
|
2015-05-04 12:04:17 -05:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
"github.com/mholt/caddy/middleware/fastcgi"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FastCGI configures a new FastCGI middleware instance.
|
|
|
|
func FastCGI(c *Controller) (middleware.Middleware, error) {
|
2015-05-05 16:50:42 -05:00
|
|
|
absRoot, err := filepath.Abs(c.Root)
|
2015-05-04 12:04:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rules, err := fastcgiParse(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(next middleware.Handler) middleware.Handler {
|
|
|
|
return fastcgi.Handler{
|
|
|
|
Next: next,
|
|
|
|
Rules: rules,
|
2015-05-05 16:50:42 -05:00
|
|
|
Root: c.Root,
|
|
|
|
AbsRoot: absRoot,
|
|
|
|
FileSys: http.Dir(c.Root),
|
2015-05-04 17:23:16 -05:00
|
|
|
SoftwareName: c.AppName,
|
|
|
|
SoftwareVersion: c.AppVersion,
|
|
|
|
ServerName: c.Host,
|
2015-08-05 12:33:51 -05:00
|
|
|
ServerPort: c.Port, // BUG: This is not known until the server blocks are split up...
|
2015-05-04 12:04:17 -05:00
|
|
|
}
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fastcgiParse(c *Controller) ([]fastcgi.Rule, error) {
|
|
|
|
var rules []fastcgi.Rule
|
|
|
|
|
|
|
|
for c.Next() {
|
|
|
|
var rule fastcgi.Rule
|
|
|
|
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
case 1:
|
|
|
|
rule.Path = "/"
|
|
|
|
rule.Address = args[0]
|
|
|
|
case 2:
|
|
|
|
rule.Path = args[0]
|
|
|
|
rule.Address = args[1]
|
|
|
|
case 3:
|
|
|
|
rule.Path = args[0]
|
|
|
|
rule.Address = args[1]
|
|
|
|
err := fastcgiPreset(args[2], &rule)
|
|
|
|
if err != nil {
|
|
|
|
return rules, c.Err("Invalid fastcgi rule preset '" + args[2] + "'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "ext":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
rule.Ext = c.Val()
|
|
|
|
case "split":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
rule.SplitPath = c.Val()
|
|
|
|
case "index":
|
2015-05-05 16:50:42 -05:00
|
|
|
args := c.RemainingArgs()
|
|
|
|
if len(args) == 0 {
|
2015-05-04 12:04:17 -05:00
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
2015-05-05 16:50:42 -05:00
|
|
|
rule.IndexFiles = args
|
2015-05-04 12:04:17 -05:00
|
|
|
case "env":
|
|
|
|
envArgs := c.RemainingArgs()
|
|
|
|
if len(envArgs) < 2 {
|
|
|
|
return rules, c.ArgErr()
|
|
|
|
}
|
|
|
|
rule.EnvVars = append(rule.EnvVars, [2]string{envArgs[0], envArgs[1]})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rules, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fastcgiPreset configures rule according to name. It returns an error if
|
|
|
|
// name is not a recognized preset name.
|
|
|
|
func fastcgiPreset(name string, rule *fastcgi.Rule) error {
|
|
|
|
switch name {
|
|
|
|
case "php":
|
|
|
|
rule.Ext = ".php"
|
|
|
|
rule.SplitPath = ".php"
|
2015-05-05 16:50:42 -05:00
|
|
|
rule.IndexFiles = []string{"index.php"}
|
2015-05-04 12:04:17 -05:00
|
|
|
default:
|
|
|
|
return errors.New(name + " is not a valid preset name")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|