mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-20 13:21:08 +00:00
properly use non-localhost BaseUrl in server command
This commit is contained in:
parent
a10519643d
commit
1abc2f0b86
1 changed files with 9 additions and 3 deletions
|
@ -18,6 +18,7 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var serverPort int
|
var serverPort int
|
||||||
|
@ -40,11 +41,16 @@ Serve them up.`,
|
||||||
func server(cmd *cobra.Command, args []string) {
|
func server(cmd *cobra.Command, args []string) {
|
||||||
InitializeConfig()
|
InitializeConfig()
|
||||||
|
|
||||||
// Unless command line overrides, we use localhost for the server
|
|
||||||
if BaseUrl == "" {
|
if BaseUrl == "" {
|
||||||
Config.BaseUrl = "http://localhost:" + strconv.Itoa(serverPort)
|
BaseUrl = "http://localhost"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !strings.HasPrefix(BaseUrl, "http://") {
|
||||||
|
BaseUrl = "http://" + BaseUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
Config.BaseUrl = strings.TrimSuffix(BaseUrl, "/") + ":" + strconv.Itoa(serverPort)
|
||||||
|
|
||||||
build(serverWatch)
|
build(serverWatch)
|
||||||
|
|
||||||
// Watch runs its own server as part of the routine
|
// Watch runs its own server as part of the routine
|
||||||
|
@ -64,7 +70,7 @@ func serve(port int) {
|
||||||
fmt.Println("Serving pages from " + Config.GetAbsPath(Config.PublishDir))
|
fmt.Println("Serving pages from " + Config.GetAbsPath(Config.PublishDir))
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Web Server is available at http://localhost:%v\n", port)
|
fmt.Printf("Web server is available at %s\n", Config.BaseUrl)
|
||||||
fmt.Println("Press ctrl+c to stop")
|
fmt.Println("Press ctrl+c to stop")
|
||||||
panic(http.ListenAndServe(":"+strconv.Itoa(port), http.FileServer(http.Dir(Config.GetAbsPath(Config.PublishDir)))))
|
panic(http.ListenAndServe(":"+strconv.Itoa(port), http.FileServer(http.Dir(Config.GetAbsPath(Config.PublishDir)))))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue