Skip to content

Commit

Permalink
Make server bind interface configurable.
Browse files Browse the repository at this point in the history
Bind by default to 127.0.0.1. Previously it always bound to all interfaces.

Always have log message display that the server is available on the
interface to which it is bound, instead of the baseUrl.

Fixes #1097
  • Loading branch information
hut8 authored and bep committed May 7, 2015
1 parent 24d8ac1 commit 2bfa9fb
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
)

var serverPort int
var serverInterface string
var serverWatch bool
var serverAppend bool
var disableLiveReload bool
Expand All @@ -49,7 +50,8 @@ serve them up.`,
}

func init() {
serverCmd.Flags().IntVarP(&serverPort, "port", "p", 1313, "port to run the server on")
serverCmd.Flags().IntVarP(&serverPort, "port", "p", 1313, "port on which the server will listen")
serverCmd.Flags().StringVarP(&serverInterface, "bind", "", "127.0.0.1", "interface to which the server will bind")
serverCmd.Flags().BoolVarP(&serverWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
serverCmd.Flags().BoolVarP(&serverAppend, "appendPort", "", true, "append port to baseurl")
serverCmd.Flags().BoolVar(&disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild")
Expand All @@ -69,7 +71,7 @@ func server(cmd *cobra.Command, args []string) {
viper.Set("Watch", true)
}

l, err := net.Listen("tcp", ":"+strconv.Itoa(serverPort))
l, err := net.Listen("tcp", net.JoinHostPort(serverInterface, strconv.Itoa(serverPort)))
if err == nil {
l.Close()
} else {
Expand Down Expand Up @@ -121,6 +123,7 @@ func serve(port int) {
httpFs := &afero.HttpFs{SourceFs: hugofs.DestinationFS}
fileserver := http.FileServer(httpFs.Dir(helpers.AbsPathify(viper.GetString("PublishDir"))))

// We're only interested in the path
u, err := url.Parse(viper.GetString("BaseURL"))
if err != nil {
jww.ERROR.Fatalf("Invalid BaseURL: %s", err)
Expand All @@ -131,11 +134,13 @@ func serve(port int) {
http.Handle(u.Path, http.StripPrefix(u.Path, fileserver))
}

u.Host = serverInterface
u.Scheme = "http"
jww.FEEDBACK.Printf("Web Server is available at %s\n", u.String())
fmt.Println("Press Ctrl+C to stop")

err = http.ListenAndServe(":"+strconv.Itoa(port), nil)
endpoint := net.JoinHostPort(serverInterface, strconv.Itoa(port))
err = http.ListenAndServe(endpoint, nil)
if err != nil {
jww.ERROR.Printf("Error: %s\n", err.Error())
os.Exit(1)
Expand Down

0 comments on commit 2bfa9fb

Please sign in to comment.