Skip to content

Instantly share code, notes, and snippets.

@xthezealot
Last active May 21, 2019 11:12
Show Gist options
  • Save xthezealot/ee53cb532743c36db4bac28b8eab4b9f to your computer and use it in GitHub Desktop.
Save xthezealot/ee53cb532743c36db4bac28b8eab4b9f to your computer and use it in GitHub Desktop.
Run Go binary server on PHP/Apache shared hosting
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/up.php
# TODO: Handle random port.
# TODO: Use ProxyPass or ProxyPassMatch instead of mod_proxy's RewriteRule for better performance.
RewriteRule ^(.*) http://127.0.0.1:8080/$1 [P]
package main
import (
"fmt"
"net"
"net/http"
)
func main() {
http.HandleFunc("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(r.RemoteAddr + " - " + r.URL.Path))
}))
listener, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
port := listener.Addr().(*net.TCPAddr).Port
// TODO: Save port and PID for .htaccess and up.php.
// If port information not accessible from .htaccess, generate a new .htaccess here.
// TODO: Remove port information at exit.
fmt.Printf("Running on :%d", port)
panic(http.Serve(listener, nil))
}
<?php
// TODO: Check for Go server PID.
// If PID information not available, jump to rerunning server.
// TODO: Handle random port.
$url = 'http://localhost:8080';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code >= 200 && $code < 400) exit();
// TODO: Kill current process and remove port information.
// TODO: Rerun server in background and redirect output to /dev/null.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment