remove bad database flag, bad curl options, and add sigint listener
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Elizabeth Hunt 2024-03-10 01:25:22 -07:00
parent f2bf6e326b
commit 1115512817
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
3 changed files with 23 additions and 43 deletions

View File

@ -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)
}

View File

@ -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 \

View File

@ -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;
}