virtigo/http.go

181 lines
3.9 KiB
Go

package main
import (
"fmt"
"net/http"
"os"
"strings"
pb "go.wit.com/lib/protobuf/virtbuf"
"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)
// show only what droplets should be running but are missing
if route == "/missing" {
var count int
var missing int
for _, d := range me.cluster.Droplets {
if d.StartState != pb.DropletState_ON {
continue
}
count += 1
if d.CurrentState == pb.DropletState_ON {
continue
}
missing += 1
fmt.Fprintln(w, d.Hostname, "current state:", d.CurrentState)
}
if missing == 0 {
fmt.Fprintln(w, "all", count, "droplets set to run are running")
} else {
fmt.Fprintln(w, missing, "droplets missing")
fmt.Fprintln(w, count, "droplets should be running")
}
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
}
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
}
// 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
}
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)
}
}