shithub: hugo

Download patch

ref: 8afd7d9ceb0d168300e3399c6e87a355a88c9a28
parent: 42fbf15fb7727a95d135488f6e3d84d04d717361
author: Bjørn Erik Pedersen <[email protected]>
date: Fri Nov 24 03:43:09 EST 2017

commands: Fix broken --appendPort=false

Also make sure to log the correct server URL to the console.

Fixes #4111

--- a/commands/commandeer.go
+++ b/commands/commandeer.go
@@ -25,6 +25,8 @@
 	pathSpec    *helpers.PathSpec
 	visitedURLs *types.EvictingStringQueue
 
+	serverPorts []int
+
 	configured bool
 }
 
--- a/commands/server.go
+++ b/commands/server.go
@@ -145,6 +145,9 @@
 	serverPorts := make([]int, 1)
 
 	if languages.IsMultihost() {
+		if !serverAppend {
+			return newSystemError("--appendPort=false not supported when in multihost mode")
+		}
 		serverPorts = make([]int, len(languages))
 	}
 
@@ -171,6 +174,8 @@
 		currentServerPort = serverPorts[i] + 1
 	}
 
+	c.serverPorts = serverPorts
+
 	c.Set("port", serverPort)
 	if liveReloadPort != -1 {
 		c.Set("liveReloadPort", liveReloadPort)
@@ -246,16 +251,15 @@
 }
 
 type fileServer struct {
-	ports    []int
 	baseURLs []string
 	roots    []string
 	c        *commandeer
 }
 
-func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, error) {
+func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, error) {
 	baseURL := f.baseURLs[i]
 	root := f.roots[i]
-	port := f.ports[i]
+	port := f.c.serverPorts[i]
 
 	publishDir := f.c.Cfg.GetString("publishDir")
 
@@ -286,7 +290,7 @@
 	// We're only interested in the path
 	u, err := url.Parse(baseURL)
 	if err != nil {
-		return nil, "", fmt.Errorf("Invalid baseURL: %s", err)
+		return nil, "", "", fmt.Errorf("Invalid baseURL: %s", err)
 	}
 
 	decorate := func(h http.Handler) http.Handler {
@@ -317,7 +321,7 @@
 
 	endpoint := net.JoinHostPort(serverInterface, strconv.Itoa(port))
 
-	return mu, endpoint, nil
+	return mu, u.String(), endpoint, nil
 }
 
 func (c *commandeer) serve() {
@@ -327,7 +331,6 @@
 	var (
 		baseURLs []string
 		roots    []string
-		ports    []int
 	)
 
 	if isMultiHost {
@@ -334,17 +337,14 @@
 		for _, s := range Hugo.Sites {
 			baseURLs = append(baseURLs, s.BaseURL.String())
 			roots = append(roots, s.Language.Lang)
-			ports = append(ports, s.Info.ServerPort())
 		}
 	} else {
 		s := Hugo.Sites[0]
 		baseURLs = []string{s.BaseURL.String()}
 		roots = []string{""}
-		ports = append(ports, s.Info.ServerPort())
 	}
 
 	srv := &fileServer{
-		ports:    ports,
 		baseURLs: baseURLs,
 		roots:    roots,
 		c:        c,
@@ -357,13 +357,13 @@
 	}
 
 	for i, _ := range baseURLs {
-		mu, endpoint, err := srv.createEndpoint(i)
+		mu, serverURL, endpoint, err := srv.createEndpoint(i)
 
 		if doLiveReload {
 			mu.HandleFunc("/livereload.js", livereload.ServeJS)
 			mu.HandleFunc("/livereload", livereload.Handler)
 		}
-		jww.FEEDBACK.Printf("Web Server is available at %s (bind address %s)\n", endpoint, serverInterface)
+		jww.FEEDBACK.Printf("Web Server is available at %s (bind address %s)\n", serverURL, serverInterface)
 		go func() {
 			err = http.ListenAndServe(endpoint, mu)
 			if err != nil {