From 11155128178ff3d8ea803a70dd7749e594b247a9 Mon Sep 17 00:00:00 2001 From: Lizzy Hunt Date: Sun, 10 Mar 2024 01:25:22 -0700 Subject: remove bad database flag, bad curl options, and add sigint listener --- html/fruitvote/main.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'html/fruitvote/main.go') diff --git a/html/fruitvote/main.go b/html/fruitvote/main.go index 73c1c17..b41c88f 100644 --- a/html/fruitvote/main.go +++ b/html/fruitvote/main.go @@ -3,11 +3,14 @@ package main import ( "flag" "fmt" + "log" "net" "net/http" "os" "os/exec" + "os/signal" "strings" + "syscall" ) func indexHandler(w http.ResponseWriter, r *http.Request) { @@ -15,6 +18,11 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello, this is a Unix socket HTTP server in Go!")) } +func healthCheckHandler(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("healthy")) +} + func main() { socketPath, users := getArgs() os.Remove(socketPath) @@ -24,6 +32,17 @@ func main() { panic(err) } os.Chmod(socketPath, 0700) + + sigc := make(chan os.Signal, 1) + signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM) + go func(c chan os.Signal) { + // Wait for a SIGINT or SIGKILL: + sig := <-c + log.Printf("Caught signal %s: shutting down.", sig) + + listener.Close() + os.Exit(0) + }(sigc) defer listener.Close() for _, user := range strings.Split(users, ",") { @@ -32,6 +51,7 @@ func main() { mux := http.NewServeMux() mux.HandleFunc("/", indexHandler) + mux.HandleFunc("/health", healthCheckHandler) http.Serve(listener, mux) } -- cgit v1.2.3-70-g09d2