From 197d8e4cb0170356dd20755efcf1d336c4c38583 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Wed, 11 Jan 2023 10:33:44 -0700 Subject: Improvements to Server lifecycle. - NewServer doesn't allocate any resources besides the server object itself. So eg context.WithCancel is delayed until s.Serve(). - Add a demonstration of graceful shutdown on signals to the cgi example. --- examples/cgi/main.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'examples/cgi/main.go') diff --git a/examples/cgi/main.go b/examples/cgi/main.go index d1df220..3dfec29 100644 --- a/examples/cgi/main.go +++ b/examples/cgi/main.go @@ -4,6 +4,8 @@ import ( "context" "log" "os" + "os/signal" + "syscall" "tildegit.org/tjp/gus/contrib/cgi" guslog "tildegit.org/tjp/gus/contrib/log" @@ -26,12 +28,20 @@ func main() { // add stdout logging to the request handler handler := guslog.Requests(os.Stdout, nil)(cgiHandler) + // set up signals to trigger graceful shutdown + ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGHUP) + defer stop() + // run the server - server, err := gemini.NewServer(context.Background(), tlsconf, "tcp4", ":1965", handler) + server, err := gemini.NewServer(ctx, tlsconf, "tcp4", ":1965", handler) if err != nil { log.Fatal(err) } - server.Serve() + defer server.Close() + + if err := server.Serve(); err != nil { + log.Fatal(err) + } } func envConfig() (string, string) { -- cgit v1.2.3