diff --git a/pkg/serverconfig/genconfig.go b/pkg/serverconfig/genconfig.go index 132c3c399..0e5c04057 100644 --- a/pkg/serverconfig/genconfig.go +++ b/pkg/serverconfig/genconfig.go @@ -232,7 +232,8 @@ func genLowLevelPrefixes(params *configPrefixesParams) jsonconfig.Obj { func GenLowLevelConfig(conf *Config) (lowLevelConf *Config, err error) { var ( - baseUrl = conf.RequiredString("listen") + baseURL = conf.OptionalString("baseURL", "") + listen = conf.RequiredString("listen") auth = conf.RequiredString("auth") keyId = conf.RequiredString("identity") secretRing = conf.RequiredString("identitySecretRing") @@ -266,10 +267,20 @@ func GenLowLevelConfig(conf *Config) (lowLevelConf *Config, err error) { obj["TLSKeyFile"] = "config/selfgen_key.pem" } } - obj["baseURL"] = scheme + "://" + baseUrl + if baseURL == "" { + baseURL = scheme + "://" + listen + } + if strings.HasSuffix(baseURL, "/") { + baseURL = baseURL[:len(baseURL)-1] + } + obj["baseURL"] = baseURL obj["https"] = tlsOn obj["auth"] = auth + if listen != "" { + obj["listen"] = listen + } + if dbname == "" { username := os.Getenv("USER") if username == "" { diff --git a/pkg/serverconfig/serverconfig_test.go b/pkg/serverconfig/serverconfig_test.go index 8ba61a0c8..9f3e19c52 100644 --- a/pkg/serverconfig/serverconfig_test.go +++ b/pkg/serverconfig/serverconfig_test.go @@ -124,6 +124,7 @@ func testConfig(name string, t *testing.T) { if err != nil { t.Fatal(err) } + baseName := strings.Replace(filepath.Base(name), ".json", "", 1) wantFile := strings.Replace(name, ".json", "-want.json", 1) wantConf, err := configParser().ReadFile(wantFile) if err != nil { @@ -133,8 +134,8 @@ func testConfig(name string, t *testing.T) { prettyPrint(&got, lowLevelConf.Obj, 0) prettyPrint(&want, wantConf, 0) if got.String() != want.String() { - tempGot := tempFile(got.Bytes()) - tempWant := tempFile(want.Bytes()) + tempGot := tempFile(baseName + "-got", got.Bytes()) + tempWant := tempFile(baseName + "-want", want.Bytes()) defer os.Remove(tempGot.Name()) defer os.Remove(tempWant.Name()) diff, err := exec.Command("diff", "-u", tempWant.Name(), tempGot.Name()).Output() @@ -146,8 +147,8 @@ func testConfig(name string, t *testing.T) { } } -func tempFile(b []byte) *os.File { - f, err := ioutil.TempFile("", "") +func tempFile(prefix string, b []byte) *os.File { + f, err := ioutil.TempFile("", prefix + "-") if err != nil { panic(err) } diff --git a/pkg/serverconfig/testdata/default-want.json b/pkg/serverconfig/testdata/default-want.json index 75da9c16d..2cffc7d11 100644 --- a/pkg/serverconfig/testdata/default-want.json +++ b/pkg/serverconfig/testdata/default-want.json @@ -1,4 +1,5 @@ { + "listen": "localhost:3179", "baseURL": "http://localhost:3179", "auth": "userpass:camlistore:pass3179", "https": false, diff --git a/pkg/serverconfig/testdata/listenbase-want.json b/pkg/serverconfig/testdata/listenbase-want.json new file mode 100644 index 000000000..370c62dc8 --- /dev/null +++ b/pkg/serverconfig/testdata/listenbase-want.json @@ -0,0 +1,96 @@ +{ + "baseURL": "http://foo.com", + "listen": "1.2.3.4:80", + "auth": "userpass:camlistore:pass3179", + "https": false, + "prefixes": { + "/": { + "handler": "root", + "handlerArgs": { + "stealth": false + } + }, + + "/ui/": { + "handler": "ui", + "handlerArgs": { + "blobRoot": "/bs-and-maybe-also-index/", + "searchRoot": "/my-search/", + "jsonSignRoot": "/sighelper/", + "cache": "/cache/", + "scaledImage": "lrucache" + } + }, + + "/setup/": { + "handler": "setup" + }, + + "/sync/": { + "handler": "sync", + "handlerArgs": { + "from": "/bs/", + "to": "/index-mem/" + } + }, + + "/sighelper/": { + "handler": "jsonsign", + "handlerArgs": { + "secretRing": "/path/to/secring", + "keyId": "26F5ABDA", + "publicKeyDest": "/bs-and-index/" + } + }, + + "/bs-and-index/": { + "handler": "storage-replica", + "handlerArgs": { + "backends": ["/bs/", "/index-mem/"] + } + }, + + "/bs-and-maybe-also-index/": { + "handler": "storage-cond", + "handlerArgs": { + "write": { + "if": "isSchema", + "then": "/bs-and-index/", + "else": "/bs/" + }, + "read": "/bs/" + } + }, + + "/bs/": { + "handler": "storage-filesystem", + "handlerArgs": { + "path": "/tmp/blobs" + } + }, + + "/cache/": { + "handler": "storage-filesystem", + "handlerArgs": { + "path": "/tmp/blobs/cache" + } + }, + + "/index-mem/": { + "handler": "storage-memory-only-dev-indexer", + "handlerArgs": { + "blobSource": "/bs/" + } + }, + + "/my-search/": { + "handler": "search", + "handlerArgs": { + "index": "/index-mem/", + "owner": "sha1-f2b0b7da718b97ce8c31591d8ed4645c777f3ef4" + } + } + } + +} + diff --git a/pkg/serverconfig/testdata/listenbase.json b/pkg/serverconfig/testdata/listenbase.json new file mode 100644 index 000000000..63307ec55 --- /dev/null +++ b/pkg/serverconfig/testdata/listenbase.json @@ -0,0 +1,8 @@ +{ + "listen": "1.2.3.4:80", + "baseURL": "http://foo.com/", + "auth": "userpass:camlistore:pass3179", + "blobPath": "/tmp/blobs", + "identity": "26F5ABDA", + "identitySecretRing": "/path/to/secring" +} diff --git a/pkg/serverconfig/testdata/tls-want.json b/pkg/serverconfig/testdata/tls-want.json index 84a46b657..2c0c6ec8b 100644 --- a/pkg/serverconfig/testdata/tls-want.json +++ b/pkg/serverconfig/testdata/tls-want.json @@ -1,4 +1,5 @@ { + "listen": "1.2.3.4:443", "baseURL": "https://1.2.3.4:443", "auth": "userpass:camlistore:pass3179", "https": true, diff --git a/pkg/serverconfig/testdata/with_blog-want.json b/pkg/serverconfig/testdata/with_blog-want.json index dd352c976..57ed3a204 100644 --- a/pkg/serverconfig/testdata/with_blog-want.json +++ b/pkg/serverconfig/testdata/with_blog-want.json @@ -1,4 +1,5 @@ { + "listen": "localhost:3179", "baseURL": "http://localhost:3179", "auth": "userpass:camlistore:pass3179", "https": false, diff --git a/pkg/serverconfig/testdata/with_gallery-want.json b/pkg/serverconfig/testdata/with_gallery-want.json index e750609d8..75a0ef693 100644 --- a/pkg/serverconfig/testdata/with_gallery-want.json +++ b/pkg/serverconfig/testdata/with_gallery-want.json @@ -1,4 +1,5 @@ { + "listen": "localhost:3179", "baseURL": "http://localhost:3179", "auth": "userpass:camlistore:pass3179", "https": false, diff --git a/server/camlistored/camlistored.go b/server/camlistored/camlistored.go index 9518e2740..f009de0e7 100644 --- a/server/camlistored/camlistored.go +++ b/server/camlistored/camlistored.go @@ -307,9 +307,14 @@ func main() { baseURL := config.RequiredString("baseURL") listen := *webserver.Listen if listen == "" { - // if command line was empty, use value in config - listen = strings.TrimLeft(baseURL, "http://") - listen = strings.TrimLeft(listen, "https://") + // If command-line is empty, try the "listen" value from + // the config file, else try to get it from the baseURL. + listen = config.OptionalString("listen", "") + if listen == "" { + // if command line was empty, use value in config + listen = strings.TrimLeft(baseURL, "http://") + listen = strings.TrimLeft(listen, "https://") + } } else { // else command line takes precedence scheme := strings.Split(baseURL, "://")[0]