2024-10-11 23:01:55 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"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) {
|
|
|
|
var tmp string
|
|
|
|
tmp = cleanURL(r.URL.Path)
|
|
|
|
|
2024-10-11 23:18:45 -05:00
|
|
|
log.Info("Got URL:", tmp)
|
2024-10-11 23:01:55 -05:00
|
|
|
if tmp == "/" {
|
|
|
|
fmt.Fprintln(w, "OK")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if tmp == "/me" {
|
|
|
|
j, err := dumpJsonClient(r)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(w, "BAD ZOOT")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, j)
|
|
|
|
return
|
|
|
|
}
|
2024-10-11 23:18:45 -05:00
|
|
|
|
|
|
|
if tmp == "/vms" {
|
|
|
|
s := poolHypervisor(hv)
|
|
|
|
fmt.Fprint(w, s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-11 23:01:55 -05:00
|
|
|
if tmp == "/favicon.ico" {
|
|
|
|
writeFile(w, "ipv6.png")
|
|
|
|
return
|
|
|
|
}
|
2024-10-11 23:18:45 -05:00
|
|
|
if tmp == "/uptime" {
|
|
|
|
writeFile(w, "uptime.html")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// used for uptime monitor checking (like Kuma)
|
2024-10-11 23:01:55 -05:00
|
|
|
if tmp == "/uptime" {
|
|
|
|
writeFile(w, "uptime.html")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Warn("BAD URL =", tmp)
|
|
|
|
fmt.Fprintln(w, "BAD ZOOT")
|
|
|
|
// badurl(w, r.URL.String())
|
|
|
|
// fmt.Fprintln(w, "BAD", tmp)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|