From 11155128178ff3d8ea803a70dd7749e594b247a9 Mon Sep 17 00:00:00 2001 From: Lizzy Hunt Date: Sun, 10 Mar 2024 01:25:22 -0700 Subject: [PATCH] remove bad database flag, bad curl options, and add sigint listener --- html/fruitvote/main.go | 20 +++++++++++++++ html/fruitvote/start.sh | 4 +-- html/public/fruitvote/GoPage.php | 42 +------------------------------- 3 files changed, 23 insertions(+), 43 deletions(-) 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) } diff --git a/html/fruitvote/start.sh b/html/fruitvote/start.sh index 3c274a8..dd3036b 100755 --- a/html/fruitvote/start.sh +++ b/html/fruitvote/start.sh @@ -3,5 +3,5 @@ nice -n 19 /home/simponic/fruitvote/fruitvote \ --users simponic,nginx \ --socket-path /home/simponic/fruitvote/http.sock \ - --database /home/simponic/fruitvote/db.sqlite \ - & + & +# --database /home/simponic/fruitvote/db.sqlite \ diff --git a/html/public/fruitvote/GoPage.php b/html/public/fruitvote/GoPage.php index b6e205a..27fc361 100644 --- a/html/public/fruitvote/GoPage.php +++ b/html/public/fruitvote/GoPage.php @@ -29,53 +29,13 @@ class GoPage { public function go() { $ch = curl_init(); $url = "http://localhost".$this->page; - curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, $this->socket); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - // forward headers - $headers = array(); - foreach ($_SERVER as $key => $value) { - if (substr($key, 0, 5) == "HTTP_") { - $key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5))))); - $headers[] = "$key: $value"; - } - } - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - - // forward params depending on http method - if ($_SERVER['REQUEST_METHOD'] == "POST" || $_SERVER['REQUEST_METHOD'] == "PUT" || $_SERVER['REQUEST_METHOD'] == "DELETE") { - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input')); - } else { - $url .= "?".$_SERVER['QUERY_STRING']; - } - - // forward cookies - $cookie = array(); - foreach ($_COOKIE as $key => $value) { - $cookie[] = "$key=$value"; - } - $output = curl_exec($ch); curl_close($ch); - - // forward headers back to client - $headers = explode("\n", $output); - foreach ($headers as $header) { - if (strpos($header, "HTTP/") === false) { - header($header); - } - } - - // forward cookies back to client - $cookie = explode("\n", $output); - foreach ($cookie as $cookie) { - if (strpos($cookie, "Set-Cookie:") !== false) { - header($cookie); - } - } + // todo: get headers / cookies, forward back response return $output; }