aboutsummaryrefslogtreecommitdiffhomepage
path: root/commands
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2022-05-15 21:01:36 +0200
committerBjørn Erik Pedersen <[email protected]>2022-05-15 22:58:05 +0200
commit87a22eb6d609a65471ccf4de35a558e9669a4600 (patch)
tree229a520a61dac3c772104ab720613671d978f046 /commands
parentfc9f315d86e1fe51c3d1eec3b60680113b2e3aa6 (diff)
downloadhugo-87a22eb6d609a65471ccf4de35a558e9669a4600.tar.gz
hugo-87a22eb6d609a65471ccf4de35a558e9669a4600.zip
server: Fix SIGINT handling after loading bad configuration
Also fix the config error messages. Fixes #9664
Diffstat (limited to 'commands')
-rw-r--r--commands/commandeer.go9
-rw-r--r--commands/server.go47
2 files changed, 44 insertions, 12 deletions
diff --git a/commands/commandeer.go b/commands/commandeer.go
index b302cbfe0..444d75987 100644
--- a/commands/commandeer.go
+++ b/commands/commandeer.go
@@ -130,6 +130,15 @@ func (c *commandeerHugoState) hugo() *hugolib.HugoSites {
return c.hugoSites
}
+func (c *commandeerHugoState) hugoTry() *hugolib.HugoSites {
+ select {
+ case <-c.created:
+ return c.hugoSites
+ case <-time.After(time.Millisecond * 100):
+ return nil
+ }
+}
+
func (c *commandeer) errCount() int {
return int(c.logger.LogCounters().ErrorCounter.Count())
}
diff --git a/commands/server.go b/commands/server.go
index f61681003..a339a1760 100644
--- a/commands/server.go
+++ b/commands/server.go
@@ -522,18 +522,20 @@ func (c *commandeer) serve(s *serverCmd) error {
roots = []string{""}
}
+ templHandler := c.hugo().Tmpl()
+ errTempl, found := templHandler.Lookup("_server/error.html")
+ if !found {
+ panic("template server/error.html not found")
+ }
+
srv := &fileServer{
baseURLs: baseURLs,
roots: roots,
c: c,
s: s,
errorTemplate: func(ctx any) (io.Reader, error) {
- templ, found := c.hugo().Tmpl().Lookup("_server/error.html")
- if !found {
- panic("template server/error.html not found")
- }
b := &bytes.Buffer{}
- err := c.hugo().Tmpl().Execute(templ, b, ctx)
+ err := templHandler.Execute(errTempl, b, ctx)
return b, err
},
}
@@ -579,16 +581,37 @@ func (c *commandeer) serve(s *serverCmd) error {
jww.FEEDBACK.Println("Press Ctrl+C to stop")
- if s.stop != nil {
- select {
- case <-sigs:
- case <-s.stop:
+ err := func() error {
+ if s.stop != nil {
+ for {
+ select {
+ case <-sigs:
+ return nil
+ case <-s.stop:
+ return nil
+ case <-ctx.Done():
+ return ctx.Err()
+ }
+ }
+ } else {
+ for {
+ select {
+ case <-sigs:
+ return nil
+ case <-ctx.Done():
+ return ctx.Err()
+ }
+ }
}
- } else {
- <-sigs
+ }()
+
+ if err != nil {
+ jww.ERROR.Println("Error:", err)
}
- c.hugo().Close()
+ if h := c.hugoTry(); h != nil {
+ h.Close()
+ }
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()