summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-01-11 10:33:44 -0700
committertjpcc <tjp@ctrl-c.club>2023-01-11 10:33:44 -0700
commit197d8e4cb0170356dd20755efcf1d336c4c38583 (patch)
tree20b2e96231a9790ddf8a83c3ce2bcb1cd334ffa4 /examples
parentcc0c7e6eb5b27c3a263352ba40ce8ee5209272a2 (diff)
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.
Diffstat (limited to 'examples')
-rw-r--r--examples/cgi/main.go14
1 files changed, 12 insertions, 2 deletions
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) {