summaryrefslogtreecommitdiff
path: root/http.go
blob: ebcaa7099b90c4250e9e389be31012b1ab7b868c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main

import (
	"fmt"
	"net/http"
	"strings"
	"time"

	"go.wit.com/lib/gui/shell"
	"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)

	// list all the droplets
	if tmp == "/droplets" {
		log.Info("Handling URL:", tmp)
		for _, d := range me.droplets {
			dur := time.Since(d.lastpoll) // Calculate the elapsed time
			fmt.Fprintln(w, d.Hostname, d.hname, shell.FormatDuration(dur))
		}
		return
	}

	// is the cluster running what it should?
	if tmp == "/good" {
		var good = true
		for _, d := range me.droplets {
			if d.State != "ON" {
				continue
			}
			dur := time.Since(d.lastpoll) // Calculate the elapsed time
			if d.CurrentState != "ON" {
				fmt.Fprintln(w, "BAD  STATE   ", d.Hostname, "State =", d.State, "CurrentState =", d.CurrentState, shell.FormatDuration(dur))
				good = false
			} else {
				dur := time.Since(d.lastpoll) // Calculate the elapsed time
				fmt.Fprintln(w, "GOOD STATE ON", d.Hostname, d.hname, shell.FormatDuration(dur))
			}
		}
		if good {
			fmt.Fprintln(w, "Handling URL:", tmp, "GOOD=true")
		} else {
			fmt.Fprintln(w, "Handling URL:", tmp, "GOOD=false")
		}
		return
	}

	if tmp == "/favicon.ico" {
		writeFile(w, "ipv6.png")
		return
	}
	if tmp == "/uptime" {
		if clusterHealthy() {
			log.Info("Handling URL:", tmp, "1 GOOD=true")
			fmt.Fprintln(w, "GOOD=true")
		} else {
			log.Info("Handling URL:", tmp, "1 GOOD=false")
			fmt.Fprintln(w, "GOOD=false")
		}
		for _, h := range me.hypers {
			dur := time.Since(h.lastpoll) // Calculate the elapsed time
			if dur > 2 * time.Minute {
				url := "http://" + h.Hostname + ":2520/kill"
				log.Info("KILL DAEMON ON", h.Hostname, shell.FormatDuration(dur), "curl", url)
			}
			// l :=  shell.FormatDuration(dur)
			// log.Warn("HOST =", h.Hostname, "Last poll =", l)
			//if d.State != "ON" {
			//	continue
			//}
			// dur := time.Since(d.lastpoll) // Calculate the elapsed time
		}
		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)
	}
}