import (
"bytes"
+ "context"
"fmt"
"io"
"net"
"time"
"github.com/gohugoio/hugo/common/paths"
+ "golang.org/x/sync/errgroup"
"github.com/pkg/errors"
var serverCfgInit sync.Once
- cfgInit := func(c *commandeer) error {
+ cfgInit := func(c *commandeer) (rerr error) {
c.Set("renderToMemory", !sc.renderToDisk)
if cmd.Flags().Changed("navigateToChanged") {
c.Set("navigateToChanged", sc.navigateToChanged)
return nil
}
- var err error
-
// We can only do this once.
serverCfgInit.Do(func() {
serverPorts = make([]int, 1)
if c.languages.IsMultihost() {
if !sc.serverAppend {
- err = newSystemError("--appendPort=false not supported when in multihost mode")
+ rerr = newSystemError("--appendPort=false not supported when in multihost mode")
}
serverPorts = make([]int, len(c.languages))
}
} else {
if i == 0 && sc.cmd.Flags().Changed("port") {
// port set explicitly by user -- he/she probably meant it!
- err = newSystemErrorF("Server startup failed: %s", err)
+ rerr = newSystemErrorF("Server startup failed: %s", err)
+ return
}
c.logger.Println("port", sc.serverPort, "already in use, attempting to use an available port")
sp, err := helpers.FindAvailablePort()
if err != nil {
- err = newSystemError("Unable to find alternative port to use:", err)
+ rerr = newSystemError("Unable to find alternative port to use:", err)
+ return
}
serverPorts[i] = sp.Port
}
}
})
+ if rerr != nil {
+ return
+ }
+
c.serverPorts = serverPorts
c.Set("port", sc.serverPort)
}
}
- return err
+ return
}
if err := memStats(); err != nil {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
+ var servers []*http.Server
for i := range baseURLs {
mu, serverURL, endpoint, err := srv.createEndpoint(i)
+ srv := &http.Server{
+ Addr: endpoint,
+ Handler: mu,
+ }
+ servers = append(servers, srv)
if doLiveReload {
u, err := url.Parse(helpers.SanitizeURL(baseURLs[i]))
}
jww.FEEDBACK.Printf("Web Server is available at %s (bind address %s)\n", serverURL, s.serverInterface)
go func() {
- err = http.ListenAndServe(endpoint, mu)
- if err != nil {
+ err = srv.ListenAndServe()
+ if err != nil && err != http.ErrServerClosed {
c.logger.Errorf("Error: %s\n", err.Error())
os.Exit(1)
}
c.hugo().Close()
- return nil
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+ wg, ctx := errgroup.WithContext(ctx)
+ for _, srv := range servers {
+ srv := srv
+ wg.Go(func() error {
+ return srv.Shutdown(ctx)
+ })
+ }
+
+ return wg.Wait()
}
// fixURL massages the baseURL into a form needed for serving
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/helpers"
+ "golang.org/x/net/context"
+ "golang.org/x/sync/errgroup"
qt "github.com/frankban/quicktest"
)
defer clean()
c.Assert(err, qt.IsNil)
- // Let us hope that this port is available on all systems ...
- port := 1331
+ sp, err := helpers.FindAvailablePort()
+ c.Assert(err, qt.IsNil)
+ port := sp.Port
defer func() {
os.RemoveAll(dir)
}()
- errors := make(chan error)
stop := make(chan bool)
b := newCommandsBuilder()
args = append([]string{"-s=" + dir, fmt.Sprintf("-p=%d", port)}, args...)
cmd.SetArgs(args)
- go func() {
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+ wg, ctx := errgroup.WithContext(ctx)
+
+ wg.Go(func() error {
_, err := cmd.ExecuteC()
- if err != nil {
- errors <- err
- }
- }()
+ return err
+ })
select {
// There is no way to know exactly when the server is ready for connections.
// We could improve by something like https://golang.org/pkg/net/http/httptest/#Server
// But for now, let us sleep and pray!
case <-time.After(2 * time.Second):
- case err := <-errors:
- result.err = err
+ case <-ctx.Done():
+ result.err = wg.Wait()
return
}
- resp, err := http.Get("http://localhost:1331/")
+ resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", port))
c.Assert(err, qt.IsNil)
defer resp.Body.Close()
homeContent := helpers.ReaderToString(resp.Body)
result.publicDirnames[f.Name()] = true
}
+ result.err = wg.Wait()
return
}