Commit d00bb87f authored by Matthew Holt's avatar Matthew Holt

-port flag to override default port

Default port used if none is specified in config
parent 0f332bd9
...@@ -11,14 +11,17 @@ import ( ...@@ -11,14 +11,17 @@ import (
) )
const ( const (
defaultHost = "localhost" DefaultHost = "localhost"
defaultPort = "2015" DefaultPort = "2015"
defaultRoot = "." DefaultRoot = "."
// The default configuration file to load if none is specified // The default configuration file to load if none is specified
DefaultConfigFile = "Caddyfile" DefaultConfigFile = "Caddyfile"
) )
// Port is configurable via command line flag
var Port = DefaultPort
// config represents a server configuration. It // config represents a server configuration. It
// is populated by parsing a config file (via the // is populated by parsing a config file (via the
// Load function). // Load function).
...@@ -113,9 +116,9 @@ func IsNotFound(err error) bool { ...@@ -113,9 +116,9 @@ func IsNotFound(err error) bool {
func Default() []Config { func Default() []Config {
cfg := []Config{ cfg := []Config{
Config{ Config{
Root: defaultRoot, Root: DefaultRoot,
Host: defaultHost, Host: DefaultHost,
Port: defaultPort, Port: Port,
}, },
} }
return cfg return cfg
......
...@@ -170,8 +170,8 @@ func TestParserBasicWithMultipleHostsPerBlock(t *testing.T) { ...@@ -170,8 +170,8 @@ func TestParserBasicWithMultipleHostsPerBlock(t *testing.T) {
if confs[0].Host != "host1.com" { if confs[0].Host != "host1.com" {
t.Errorf("Expected host of first conf to be 'host1.com', got '%s'", confs[0].Host) t.Errorf("Expected host of first conf to be 'host1.com', got '%s'", confs[0].Host)
} }
if confs[0].Port != defaultPort { if confs[0].Port != DefaultPort {
t.Errorf("Expected port of first conf to be '%s', got '%s'", defaultPort, confs[0].Port) t.Errorf("Expected port of first conf to be '%s', got '%s'", DefaultPort, confs[0].Port)
} }
if confs[0].Root != "/public_html" { if confs[0].Root != "/public_html" {
t.Errorf("Expected root of first conf to be '/public_html', got '%s'", confs[0].Root) t.Errorf("Expected root of first conf to be '/public_html', got '%s'", confs[0].Root)
...@@ -255,8 +255,8 @@ func TestParserBasicWithAlternateAddressStyles(t *testing.T) { ...@@ -255,8 +255,8 @@ func TestParserBasicWithAlternateAddressStyles(t *testing.T) {
if confs[4].Host != "host" { if confs[4].Host != "host" {
t.Errorf("Expected conf[4] Host='host', got '%#v'", confs[4]) t.Errorf("Expected conf[4] Host='host', got '%#v'", confs[4])
} }
if confs[4].Port != defaultPort { if confs[4].Port != DefaultPort {
t.Errorf("Expected conf[4] Port='%s', got '%#v'", defaultPort, confs[4].Port) t.Errorf("Expected conf[4] Port='%s', got '%#v'", DefaultPort, confs[4].Port)
} }
} }
......
...@@ -47,7 +47,7 @@ func (p *parser) addresses() error { ...@@ -47,7 +47,7 @@ func (p *parser) addresses() error {
schemePort = "http" schemePort = "http"
str = str[7:] str = str[7:]
} else if !strings.Contains(str, ":") { } else if !strings.Contains(str, ":") {
str += ":" + defaultPort str += ":" + Port
} }
host, port, err = net.SplitHostPort(str) host, port, err = net.SplitHostPort(str)
......
...@@ -27,6 +27,7 @@ func init() { ...@@ -27,6 +27,7 @@ func init() {
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")
flag.StringVar(&config.Port, "port", config.DefaultPort, "Default port")
flag.Parse() flag.Parse()
} }
......
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