summaryrefslogtreecommitdiff
path: root/http.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-11-15 18:49:52 -0600
committerJeff Carr <[email protected]>2024-11-15 18:49:52 -0600
commitfbd638aec099fc88c5bd616dff891bb942d2de4b (patch)
tree0f3a8abb3e61ec3261f5db803fb561e2c221e04c /http.go
day 1.v0.0.1
Diffstat (limited to 'http.go')
-rw-r--r--http.go95
1 files changed, 95 insertions, 0 deletions
diff --git a/http.go b/http.go
new file mode 100644
index 0000000..6547f74
--- /dev/null
+++ b/http.go
@@ -0,0 +1,95 @@
+package main
+
+import (
+ "fmt"
+ "io/ioutil"
+ "net/http"
+ "os"
+ "strings"
+
+ "go.wit.com/log"
+)
+
+// remove '?' part and trailing '/'
+func cleanURL(url string) string {
+ url = "/" + strings.Trim(url, "/")
+ return url
+}
+
+func okHandler(w http.ResponseWriter, r *http.Request) {
+ log.Info("Got URL Path: ", r.URL.Path)
+ route := cleanURL(r.URL.Path)
+
+ domname := r.URL.Query().Get("domain")
+
+ msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
+ if err != nil {
+ fmt.Fprintln(w, "ReadAll() error =", err)
+ return
+ }
+
+ log.Info("Got URL msg:", string(msg))
+ if route == "/" {
+ fmt.Fprintln(w, "OK")
+ return
+ }
+
+ // exit the virtigo daemon & have systemd restart it
+ // this can happen & when it does, access to
+ // to libvirtd will hang (aka: virsh list will hang)
+ // One way to trigger this is to not properly close
+ // domain sockets opened from go-qemu/hypervisor
+ // it's a good idea in any case so leave it here
+ if route == "/kill" {
+ log.Warn("KILLED")
+ fmt.Fprintln(w, "KILLED")
+ os.Exit(-1)
+ return
+ }
+
+ // curl http://localhost:2520/import?domain=foo.bar.com
+ if route == "/import" {
+ fmt.Fprint(w, "import domain:", domname)
+
+ return
+ }
+
+ if route == "/favicon.ico" {
+ writeFile(w, "ipv6.png")
+ return
+ }
+
+ log.Warn("BAD URL =", route)
+}
+
+func writeFile(w http.ResponseWriter, filename string) {
+ // fmt.Fprintln(w, "GOT TEST?")
+ fullname := "resources/" + filename
+ pfile, err := resources.ReadFile(fullname)
+ if err != nil {
+ log.Println("ERROR:", err)
+ // w.Write(pfile)
+ return
+ }
+
+ var repohtml string
+ repohtml = string(pfile)
+ if filename == "goReference.svg" {
+ w.Header().Set("Content-Type", "image/svg+xml")
+ }
+ fmt.Fprintln(w, repohtml)
+ log.Println("writeFile() found internal file:", filename)
+}
+
+// starts and sits waiting for HTTP requests
+func startHTTP() {
+ http.HandleFunc("/", okHandler)
+
+ p := fmt.Sprintf(":%d", argv.Port)
+ log.Println("Running on port", p)
+
+ err := http.ListenAndServe(p, nil)
+ if err != nil {
+ log.Println("Error starting server:", err)
+ }
+}