Commit 2dbd14b6 authored by Matthew Holt's avatar Matthew Holt

Consistent app name/version info; pipe config data through stdin

parent 085f6e95
...@@ -19,13 +19,6 @@ const ( ...@@ -19,13 +19,6 @@ const (
DefaultConfigFile = "Caddyfile" DefaultConfigFile = "Caddyfile"
) )
// These three defaults are configurable through the command line
var (
Root = DefaultRoot
Host = DefaultHost
Port = DefaultPort
)
func Load(filename string, input io.Reader) ([]server.Config, error) { func Load(filename string, input io.Reader) ([]server.Config, error) {
var configs []server.Config var configs []server.Config
...@@ -46,6 +39,9 @@ func Load(filename string, input io.Reader) ([]server.Config, error) { ...@@ -46,6 +39,9 @@ func Load(filename string, input io.Reader) ([]server.Config, error) {
Host: sb.Host, Host: sb.Host,
Port: sb.Port, Port: sb.Port,
Middleware: make(map[string][]middleware.Middleware), Middleware: make(map[string][]middleware.Middleware),
ConfigFile: filename,
AppName: AppName,
AppVersion: AppVersion,
} }
// It is crucial that directives are executed in the proper order. // It is crucial that directives are executed in the proper order.
...@@ -105,3 +101,14 @@ func Default() server.Config { ...@@ -105,3 +101,14 @@ func Default() server.Config {
Port: Port, Port: Port,
} }
} }
// These three defaults are configurable through the command line
var (
Root = DefaultRoot
Host = DefaultHost
Port = DefaultPort
)
// The application should set these so that various middlewares
// can access the proper information for their own needs.
var AppName, AppVersion string
...@@ -199,7 +199,7 @@ func (d *Dispenser) Err(msg string) error { ...@@ -199,7 +199,7 @@ func (d *Dispenser) Err(msg string) error {
// Errf is like Err, but for formatted error messages // Errf is like Err, but for formatted error messages
func (d *Dispenser) Errf(format string, args ...interface{}) error { func (d *Dispenser) Errf(format string, args ...interface{}) error {
return d.Err(fmt.Sprintf(format, args...)) // TODO: I think args needs to be args... return d.Err(fmt.Sprintf(format, args...))
} }
// numLineBreaks counts how many line breaks are in the token // numLineBreaks counts how many line breaks are in the token
......
...@@ -25,9 +25,10 @@ func FastCGI(c *Controller) (middleware.Middleware, error) { ...@@ -25,9 +25,10 @@ func FastCGI(c *Controller) (middleware.Middleware, error) {
Next: next, Next: next,
Rules: rules, Rules: rules,
Root: root, Root: root,
SoftwareName: "Caddy", // TODO: Once generators are not in the same pkg as handler, obtain this from some global const SoftwareName: c.AppName,
SoftwareVersion: "", // TODO: Get this from some global const too SoftwareVersion: c.AppVersion,
// TODO: Set ServerName and ServerPort to correct values... (as user defined in config) ServerName: c.Host,
ServerPort: c.Port,
} }
}, nil }, nil
} }
......
...@@ -68,15 +68,10 @@ func WebSocket(c *Controller) (middleware.Middleware, error) { ...@@ -68,15 +68,10 @@ func WebSocket(c *Controller) (middleware.Middleware, error) {
}) })
} }
websockets.GatewayInterface = envGatewayInterface websockets.GatewayInterface = c.AppName + "-CGI/1.1"
websockets.ServerSoftware = envServerSoftware websockets.ServerSoftware = c.AppName + "/" + c.AppVersion
return func(next middleware.Handler) middleware.Handler { return func(next middleware.Handler) middleware.Handler {
return websockets.WebSockets{Next: next, Sockets: websocks} return websockets.WebSockets{Next: next, Sockets: websocks}
}, nil }, nil
} }
const (
envGatewayInterface = "caddy-CGI/1.1"
envServerSoftware = "caddy/" // TODO: Version
)
package main package main
import ( import (
"bytes"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net" "net"
"os" "os"
...@@ -22,10 +24,11 @@ var ( ...@@ -22,10 +24,11 @@ var (
http2 bool // TODO: temporary flag until http2 is standard http2 bool // TODO: temporary flag until http2 is standard
quiet bool quiet bool
cpu string cpu string
confBody []byte // configuration data to use, piped from stdin
) )
func init() { func init() {
flag.StringVar(&conf, "conf", config.DefaultConfigFile, "The configuration file to use") flag.StringVar(&conf, "conf", "", "Configuration file to use")
flag.BoolVar(&http2, "http2", true, "Enable HTTP/2 support") // TODO: temporary flag until http2 merged into std lib flag.BoolVar(&http2, "http2", true, "Enable HTTP/2 support") // TODO: temporary flag until http2 merged into std lib
flag.BoolVar(&quiet, "quiet", false, "Quiet mode (no initialization output)") flag.BoolVar(&quiet, "quiet", false, "Quiet mode (no initialization output)")
flag.StringVar(&cpu, "cpu", "100%", "CPU cap") flag.StringVar(&cpu, "cpu", "100%", "CPU cap")
...@@ -33,6 +36,21 @@ func init() { ...@@ -33,6 +36,21 @@ func init() {
flag.StringVar(&config.Host, "host", config.DefaultHost, "Default host") flag.StringVar(&config.Host, "host", config.DefaultHost, "Default host")
flag.StringVar(&config.Port, "port", config.DefaultPort, "Default port") flag.StringVar(&config.Port, "port", config.DefaultPort, "Default port")
flag.Parse() flag.Parse()
config.AppName = "Caddy"
config.AppVersion = "0.6.0"
// Load piped configuration data, if any
fi, err := os.Stdin.Stat()
if err != nil {
log.Fatal(err)
}
if fi.Mode()&os.ModeCharDevice == 0 {
confBody, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
}
} }
func main() { func main() {
...@@ -45,7 +63,7 @@ func main() { ...@@ -45,7 +63,7 @@ func main() {
} }
// Load config from file // Load config from file
allConfigs, err := loadConfigs(conf) allConfigs, err := loadConfigs()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
...@@ -82,36 +100,37 @@ func main() { ...@@ -82,36 +100,37 @@ func main() {
wg.Wait() wg.Wait()
} }
// loadConfigs loads configuration from a file. // loadConfigs loads configuration from a file or stdin (piped).
func loadConfigs(confPath string) ([]server.Config, error) { // Configuration is obtained from one of three sources, tried
var allConfigs []server.Config // in this order: 1. -conf flag, 2. stdin, 3. Caddyfile.
// If none of those are available, a default configuration is
file, err := os.Open(confPath) // loaded.
if err == nil { func loadConfigs() ([]server.Config, error) {
defer file.Close() // -conf flag
allConfigs, err = config.Load(path.Base(confPath), file) if conf != "" {
file, err := os.Open(conf)
if err != nil { if err != nil {
return allConfigs, err return []server.Config{}, err
}
} else {
if os.IsNotExist(err) {
// This is only a problem if the user
// explicitly specified a config file
if confPath != config.DefaultConfigFile {
return allConfigs, err
}
} else {
// ... but anything else is always a problem
return allConfigs, err
} }
defer file.Close()
return config.Load(path.Base(conf), file)
} }
// If config file was empty or didn't exist, use default // stdin
if len(allConfigs) == 0 { if len(confBody) > 0 {
allConfigs = []server.Config{config.Default()} return config.Load("stdin", bytes.NewReader(confBody))
} }
return allConfigs, nil // Caddyfile
file, err := os.Open(config.DefaultConfigFile)
if err != nil {
if os.IsNotExist(err) {
return []server.Config{config.Default()}, nil
}
return []server.Config{}, err
}
defer file.Close()
return config.Load(config.DefaultConfigFile, file)
} }
// arrangeBindings groups configurations by their bind address. For example, // arrangeBindings groups configurations by their bind address. For example,
......
...@@ -63,7 +63,7 @@ func (ws WebSocket) buildEnv(cmdPath string) (metavars []string, err error) { ...@@ -63,7 +63,7 @@ func (ws WebSocket) buildEnv(cmdPath string) (metavars []string, err error) {
`PATH_TRANSLATED=`, // TODO `PATH_TRANSLATED=`, // TODO
`QUERY_STRING=` + ws.URL.RawQuery, `QUERY_STRING=` + ws.URL.RawQuery,
`REMOTE_ADDR=` + remoteHost, `REMOTE_ADDR=` + remoteHost,
`REMOTE_HOST=` + remoteHost, // TODO (Host lookups are slow; make this configurable) `REMOTE_HOST=` + remoteHost, // Host lookups are slow - don't do them
`REMOTE_IDENT=`, // Not used `REMOTE_IDENT=`, // Not used
`REMOTE_PORT=` + remotePort, `REMOTE_PORT=` + remotePort,
`REMOTE_USER=`, // Not used, `REMOTE_USER=`, // Not used,
......
...@@ -34,6 +34,12 @@ type Config struct { ...@@ -34,6 +34,12 @@ type Config struct {
// The path to the configuration file from which this was loaded // The path to the configuration file from which this was loaded
ConfigFile string ConfigFile string
// The name of the application
AppName string
// The application's version
AppVersion string
} }
// Address returns the host:port of c as a string. // Address returns the host:port of c as a string.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment