package main import ( "fmt" "net/http" "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 tmp string tmp = cleanURL(r.URL.Path) // is the cluster running what it should? if tmp == "/droplets" { for _, d := range me.cluster.Droplets { var msg string if d.StartState == pb.DropletState_ON { msg = "(should be on)" } switch d.CurrentState { case pb.DropletState_ON: fmt.Fprintln(w, d.Hostname, "ON") case pb.DropletState_OFF: fmt.Fprintln(w, d.Hostname, msg) default: fmt.Fprintln(w, d.Hostname, "? state:", d.CurrentState, msg) } } return } // show only what droplets should be running but are missing if tmp == "/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 tmp == "/favicon.ico" { // w.Header().Set("Content-Type", "image/svg+xml") w.Header().Set("Content-Type", "image/png") writeFile(w, "ipv6.png") return } if tmp == "/goReference.svg" { w.Header().Set("Content-Type", "image/svg+xml") writeFile(w, "goReference.svg") return } if tmp == "/dumplibvirtxml" { virtigoxml.DumpLibvirtxmlDomainNames() return } if tmp == "/uptime" { b, s := clusterHealthy() if b { log.Info("Handling URL:", tmp, "cluster is ok", s) fmt.Fprintln(w, s) } else { log.Info("Handling URL:", tmp, "cluster is not right yet", s) fmt.Fprintln(w, s) } return } if tmp == "/start" { start := r.URL.Query().Get("start") // log.Warn("Handling URL:", tmp, "start droplet", start) b, result := Start(start) log.Warn("Start returned =", b, "result =", result) fmt.Fprintln(w, "Start() returned", b) fmt.Fprintln(w, "result:", result) return } log.Warn("BAD URL =", tmp) fmt.Fprintln(w, "BAD URL tmp =", tmp) } // 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) } }