remove bad database flag, bad curl options, and add sigint listener
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
f2bf6e326b
commit
1115512817
@ -3,11 +3,14 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
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!"))
|
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() {
|
func main() {
|
||||||
socketPath, users := getArgs()
|
socketPath, users := getArgs()
|
||||||
os.Remove(socketPath)
|
os.Remove(socketPath)
|
||||||
@ -24,6 +32,17 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
os.Chmod(socketPath, 0700)
|
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()
|
defer listener.Close()
|
||||||
|
|
||||||
for _, user := range strings.Split(users, ",") {
|
for _, user := range strings.Split(users, ",") {
|
||||||
@ -32,6 +51,7 @@ func main() {
|
|||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/", indexHandler)
|
mux.HandleFunc("/", indexHandler)
|
||||||
|
mux.HandleFunc("/health", healthCheckHandler)
|
||||||
|
|
||||||
http.Serve(listener, mux)
|
http.Serve(listener, mux)
|
||||||
}
|
}
|
||||||
|
@ -3,5 +3,5 @@
|
|||||||
nice -n 19 /home/simponic/fruitvote/fruitvote \
|
nice -n 19 /home/simponic/fruitvote/fruitvote \
|
||||||
--users simponic,nginx \
|
--users simponic,nginx \
|
||||||
--socket-path /home/simponic/fruitvote/http.sock \
|
--socket-path /home/simponic/fruitvote/http.sock \
|
||||||
--database /home/simponic/fruitvote/db.sqlite \
|
&
|
||||||
&
|
# --database /home/simponic/fruitvote/db.sqlite \
|
||||||
|
@ -29,53 +29,13 @@ class GoPage {
|
|||||||
public function go() {
|
public function go() {
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
$url = "http://localhost".$this->page;
|
$url = "http://localhost".$this->page;
|
||||||
|
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, $this->socket);
|
curl_setopt($ch, CURLOPT_UNIX_SOCKET_PATH, $this->socket);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
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);
|
$output = curl_exec($ch);
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
// todo: get headers / cookies, forward back response
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user