about summary refs log tree commit diff stats
path: root/cmd/server/main.go
diff options
context:
space:
mode:
authorAlan Pearce2024-06-18 15:54:10 +0200
committerAlan Pearce2024-06-18 15:54:10 +0200
commitf69b6432cd1f921909b254c1adc93080e844f2f5 (patch)
tree65dbea05eac30a8381d464d19049439e55f9a495 /cmd/server/main.go
parent6f50ed8e5437495a69c448513ff819343d4c4015 (diff)
downloadwebsite-f69b6432cd1f921909b254c1adc93080e844f2f5.tar.lz
website-f69b6432cd1f921909b254c1adc93080e844f2f5.tar.zst
website-f69b6432cd1f921909b254c1adc93080e844f2f5.zip
fix/simplify server startup/shutdown
Diffstat (limited to 'cmd/server/main.go')
-rw-r--r--cmd/server/main.go40
1 files changed, 16 insertions, 24 deletions
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 51a9e52..7e6e1ec 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -1,10 +1,10 @@
 package main
 
 import (
+	"context"
 	"fmt"
 	"os"
 	"os/signal"
-	"sync"
 
 	"website/internal/log"
 	"website/internal/server"
@@ -39,34 +39,26 @@ func main() {
 		defer os.RemoveAll(tmpdir)
 	}
 
-	c := make(chan os.Signal, 2)
-	signal.Notify(c, os.Interrupt)
 	sv, err := server.New(&runtimeConfig)
 	if err != nil {
-		log.Fatal("error setting up server", "error", err)
+		log.Error("could not create server", "error", err)
+
+		return
 	}
-	wg := &sync.WaitGroup{}
-	wg.Add(1)
-	go func() {
-		defer wg.Done()
-		sig := <-c
-		log.Info("signal captured", "sig", sig)
-		<-sv.Stop()
-		log.Debug("server stopped")
-	}()
 
-	sErr := make(chan error)
-	wg.Add(1)
+	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
+	defer cancel()
+
 	go func() {
-		defer wg.Done()
-		sErr <- sv.Start()
+		err = sv.Start()
+		if err != nil {
+			// Error starting or closing listener:
+			log.Fatal("error %v", err)
+		}
 	}()
-	log.Info("server listening", "address", sv.Addr)
 
-	err = <-sErr
-	if err != nil {
-		// Error starting or closing listener:
-		log.Fatal("error", "error", err)
-	}
-	wg.Wait()
+	<-ctx.Done()
+	log.Debug("calling stop")
+	sv.Stop()
+	log.Debug("done")
 }