172 lines
3.6 KiB
Go
172 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"go.wit.com/lib/virtigoxml"
|
|
"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 route string
|
|
route = cleanURL(r.URL.Path)
|
|
|
|
if route == "/uptime" {
|
|
ok, s := uptimeCheck()
|
|
if ok {
|
|
log.Info("Handling URL:", route, "cluster is ok", s)
|
|
fmt.Fprintln(w, s)
|
|
} else {
|
|
log.Info("Handling URL:", route, "cluster is not right yet", s)
|
|
fmt.Fprintln(w, s)
|
|
}
|
|
return
|
|
}
|
|
|
|
if route == "/start" {
|
|
hostname := r.URL.Query().Get("hostname")
|
|
if hostname == "" {
|
|
log.Warn("start failed. hostname is blank", cleanURL(r.URL.Path))
|
|
fmt.Fprintln(w, "start failed. hostname is blank", cleanURL(r.URL.Path))
|
|
return
|
|
}
|
|
log.Warn("hostname is", hostname)
|
|
fmt.Fprintln(w, "hostname is", hostname)
|
|
|
|
// log.Warn("Handling URL:", tmp, "start droplet", start)
|
|
result, err := Start(hostname)
|
|
if err == nil {
|
|
fmt.Fprintln(w, result)
|
|
fmt.Fprintln(w, hostname, "started output ok")
|
|
fmt.Fprintln(w, hostname, "need to parse the output here")
|
|
fmt.Fprintln(w, hostname, "todo: switch to protobuf here")
|
|
} else {
|
|
fmt.Fprintln(w, result)
|
|
fmt.Fprintln(w, err)
|
|
fmt.Fprintln(w, hostname, "start failed")
|
|
}
|
|
return
|
|
}
|
|
|
|
if route == "/create" {
|
|
log.Info("virtigo create starts here")
|
|
fmt.Fprintln(w, "virtigo create starts here")
|
|
result, err := create(w, r)
|
|
if err != nil {
|
|
log.Info("virtigo create failed")
|
|
log.Info(result)
|
|
fmt.Fprintln(w, "virtigo create failed")
|
|
fmt.Fprintln(w, result)
|
|
return
|
|
}
|
|
log.Info("virtigo create ends here")
|
|
fmt.Fprintln(w, "virtigo create ends here")
|
|
return
|
|
}
|
|
|
|
// toggle poll logging
|
|
if route == "/poll" {
|
|
if POLL.Get() {
|
|
fmt.Fprintln(w, "POLL is true")
|
|
POLL.SetBool(false)
|
|
} else {
|
|
fmt.Fprintln(w, "POLL is false")
|
|
POLL.SetBool(true)
|
|
}
|
|
return
|
|
}
|
|
|
|
if route == "/dumpcluster" {
|
|
dumpCluster(w)
|
|
return
|
|
}
|
|
|
|
if route == "/dumpdroplets" {
|
|
dumpDroplets(w, false)
|
|
return
|
|
}
|
|
|
|
if route == "/dumpdropletsfull" {
|
|
dumpDroplets(w, true)
|
|
return
|
|
}
|
|
|
|
if route == "/dumphypervisors" {
|
|
dumpHypervisors(w)
|
|
return
|
|
}
|
|
|
|
if route == "/dumplibvirtxml" {
|
|
virtigoxml.DumpLibvirtxmlDomainNames()
|
|
return
|
|
}
|
|
|
|
if route == "/quit" {
|
|
log.Warn("writing out config file and exiting virtigo")
|
|
fmt.Fprintln(w, "writing out config file and exiting virtigo")
|
|
if err := me.cluster.ConfigSave(); err != nil {
|
|
log.Info("configsave error", err)
|
|
} else {
|
|
os.Exit(-1)
|
|
}
|
|
return
|
|
}
|
|
|
|
if route == "/favicon.ico" {
|
|
// w.Header().Set("Content-Type", "image/svg+xml")
|
|
w.Header().Set("Content-Type", "image/png")
|
|
writeFile(w, "ipv6.png")
|
|
return
|
|
}
|
|
|
|
if route == "/goReference.svg" {
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
writeFile(w, "goReference.svg")
|
|
return
|
|
}
|
|
|
|
log.Warn("BAD URL =", route)
|
|
fmt.Fprintln(w, "BAD URL tmp =", route)
|
|
}
|
|
|
|
// write a file out to the http socket
|
|
func writeFile(w http.ResponseWriter, filename string) {
|
|
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)
|
|
}
|
|
}
|