commands: Fix broken --appendPort=false

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

Fixes #4111
This commit is contained in:
Bjørn Erik Pedersen 2017-11-24 08:43:09 +01:00
parent 42fbf15fb7
commit 8afd7d9ceb
2 changed files with 13 additions and 11 deletions

View file

@ -25,6 +25,8 @@ type commandeer struct {
pathSpec *helpers.PathSpec pathSpec *helpers.PathSpec
visitedURLs *types.EvictingStringQueue visitedURLs *types.EvictingStringQueue
serverPorts []int
configured bool configured bool
} }

View file

@ -145,6 +145,9 @@ func server(cmd *cobra.Command, args []string) error {
serverPorts := make([]int, 1) serverPorts := make([]int, 1)
if languages.IsMultihost() { if languages.IsMultihost() {
if !serverAppend {
return newSystemError("--appendPort=false not supported when in multihost mode")
}
serverPorts = make([]int, len(languages)) serverPorts = make([]int, len(languages))
} }
@ -171,6 +174,8 @@ func server(cmd *cobra.Command, args []string) error {
currentServerPort = serverPorts[i] + 1 currentServerPort = serverPorts[i] + 1
} }
c.serverPorts = serverPorts
c.Set("port", serverPort) c.Set("port", serverPort)
if liveReloadPort != -1 { if liveReloadPort != -1 {
c.Set("liveReloadPort", liveReloadPort) c.Set("liveReloadPort", liveReloadPort)
@ -246,16 +251,15 @@ func server(cmd *cobra.Command, args []string) error {
} }
type fileServer struct { type fileServer struct {
ports []int
baseURLs []string baseURLs []string
roots []string roots []string
c *commandeer 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] baseURL := f.baseURLs[i]
root := f.roots[i] root := f.roots[i]
port := f.ports[i] port := f.c.serverPorts[i]
publishDir := f.c.Cfg.GetString("publishDir") publishDir := f.c.Cfg.GetString("publishDir")
@ -286,7 +290,7 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, error) {
// We're only interested in the path // We're only interested in the path
u, err := url.Parse(baseURL) u, err := url.Parse(baseURL)
if err != nil { 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 { decorate := func(h http.Handler) http.Handler {
@ -317,7 +321,7 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, error) {
endpoint := net.JoinHostPort(serverInterface, strconv.Itoa(port)) endpoint := net.JoinHostPort(serverInterface, strconv.Itoa(port))
return mu, endpoint, nil return mu, u.String(), endpoint, nil
} }
func (c *commandeer) serve() { func (c *commandeer) serve() {
@ -327,24 +331,20 @@ func (c *commandeer) serve() {
var ( var (
baseURLs []string baseURLs []string
roots []string roots []string
ports []int
) )
if isMultiHost { if isMultiHost {
for _, s := range Hugo.Sites { for _, s := range Hugo.Sites {
baseURLs = append(baseURLs, s.BaseURL.String()) baseURLs = append(baseURLs, s.BaseURL.String())
roots = append(roots, s.Language.Lang) roots = append(roots, s.Language.Lang)
ports = append(ports, s.Info.ServerPort())
} }
} else { } else {
s := Hugo.Sites[0] s := Hugo.Sites[0]
baseURLs = []string{s.BaseURL.String()} baseURLs = []string{s.BaseURL.String()}
roots = []string{""} roots = []string{""}
ports = append(ports, s.Info.ServerPort())
} }
srv := &fileServer{ srv := &fileServer{
ports: ports,
baseURLs: baseURLs, baseURLs: baseURLs,
roots: roots, roots: roots,
c: c, c: c,
@ -357,13 +357,13 @@ func (c *commandeer) serve() {
} }
for i, _ := range baseURLs { for i, _ := range baseURLs {
mu, endpoint, err := srv.createEndpoint(i) mu, serverURL, endpoint, err := srv.createEndpoint(i)
if doLiveReload { if doLiveReload {
mu.HandleFunc("/livereload.js", livereload.ServeJS) mu.HandleFunc("/livereload.js", livereload.ServeJS)
mu.HandleFunc("/livereload", livereload.Handler) 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() { go func() {
err = http.ListenAndServe(endpoint, mu) err = http.ListenAndServe(endpoint, mu)
if err != nil { if err != nil {