summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-09-09 18:02:06 -0500
committerJeff Carr <[email protected]>2025-09-09 18:02:06 -0500
commitaf7984a887cc583fe46edcc3e00fd71e6da0e627 (patch)
tree289cd43431a4a0a2115c251a4dc487d5923d80c0
parenta29a774aa43ca332cac6ca3da7242eb31a9bfcf0 (diff)
-rw-r--r--http.go111
-rw-r--r--httpDump.go83
-rw-r--r--machine.go38
-rw-r--r--main.go3
4 files changed, 82 insertions, 153 deletions
diff --git a/http.go b/http.go
index 7711300..14cb28a 100644
--- a/http.go
+++ b/http.go
@@ -5,81 +5,90 @@ package main
import (
"fmt"
- "io/ioutil"
+ "io"
"net/http"
- "strings"
"time"
+ "go.wit.com/lib/protobuf/httppb"
"go.wit.com/log"
)
-// remove '?' part and trailing '/'
-func cleanURL(url string) string {
- url = "/" + strings.Trim(url, "/")
- return url
-}
-
+/*
+ if strings.HasPrefix(route, "/repos/") {
+ pb := gitpb.NewRepos()
+ if err := pb.Unmarshal(reqPB.ClientData); err == nil {
+ reqPB.Log("Repos Unmarshal() len=%d", pb.Len())
+ } else {
+ reqPB.Logf("Repos Unmarshal() err=%v", err)
+ }
+ result := gitpb.NewRepos()
+ switch route {
+ case "/repos/check":
+ result = addRequest(pb, reqPB)
+ reqPB.Logf("repos check result.Len()=%d pb.Len()=%d\n", result.Len(), pb.Len())
+ case "/repos/pull":
+ result = pullRequest(pb, reqPB)
+ case "/repos/add":
+ result = addRequest(pb, reqPB)
+ default:
+ reqPB.Logf("repos check result.Len()=%d pb.Len()=%d\n", result.Len(), pb.Len())
+ log.Info("repos", route, "unknown")
+ }
+ if err := result.SendReply(w, reqPB); err != nil {
+ reqPB.Logf("Oh well, Send to client failed. err=%v", err)
+ }
+ // todo: logReq(reqPB)
+ logReqPB(reqPB)
+ return
+ }
+*/
func okHandler(w http.ResponseWriter, r *http.Request) {
- // log.Info("Got URL Path: ", r.URL.Path)
- route := cleanURL(r.URL.Path)
-
- hostname := r.URL.Query().Get("hostname")
- // flag := r.URL.Query().Get("flag")
-
- msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
+ reqPB, err := httppb.ReqToPB(r)
+ reqPB.Logf("START: Got %d bytes from the client", len(reqPB.ClientData))
if err != nil {
- log.Info("ReadAll() error =", err)
- return
+ reqPB.Logf("httppb err %v", err)
}
+ route := reqPB.Route
+
if route == "/" {
return
}
if route == "/machine" {
- handleMachine(r, w, hostname, msg)
+ handleMachine(w, reqPB)
return
}
if route == "/uptime" {
- if me.zood == nil {
- fmt.Fprintf(w, "BAD zood == nil\n")
- return
- }
- var count int
- var bad int
- for m := range me.machines.IterAll() {
- count += 1
- if m.FindVersion("zood") != me.zood.version {
- if m.SinceLastUpdate() > 10*time.Minute {
- // skip machines that have not been updated in the last 10 minutes
- log.Info("ignoring old machine", m.Hostname)
- continue
- }
- bad += 1
- }
- }
- if bad == 0 {
- fmt.Fprintf(w, "GOOD machine count=(%d) all machines are version %s\n", count, me.zood.version)
- } else {
- fmt.Fprintf(w, "BAD machine count=(%d) upgrade=(%d) to %s\n", count, bad, me.zood.version)
- }
+ doUptime(w)
return
}
log.Warn("BAD URL =", route)
}
-// starts and sits waiting for HTTP requests
-func startHTTP() {
- http.HandleFunc("/", okHandler)
-
- p := fmt.Sprintf(":%d", argv.Port)
- log.Println("zookeeper main startHTTP() Running on port", p)
-
- err := http.ListenAndServe(p, nil)
- if err != nil {
- log.Println("Error starting server:", err)
- badExit(err)
+func doUptime(w io.Writer) {
+ if me.zood == nil {
+ fmt.Fprintf(w, "BAD zood == nil\n")
+ return
+ }
+ var count int
+ var bad int
+ for m := range me.machines.IterAll() {
+ count += 1
+ if m.FindVersion("zood") != me.zood.version {
+ if m.SinceLastUpdate() > 10*time.Minute {
+ // skip machines that have not been updated in the last 10 minutes
+ log.Info("ignoring old machine", m.Hostname)
+ continue
+ }
+ bad += 1
+ }
+ }
+ if bad == 0 {
+ fmt.Fprintf(w, "GOOD machine count=(%d) all machines are version %s\n", count, me.zood.version)
+ } else {
+ fmt.Fprintf(w, "BAD machine count=(%d) upgrade=(%d) to %s\n", count, bad, me.zood.version)
}
}
diff --git a/httpDump.go b/httpDump.go
deleted file mode 100644
index 3df4bb0..0000000
--- a/httpDump.go
+++ /dev/null
@@ -1,83 +0,0 @@
-package main
-
-import (
- "fmt"
- "net/http"
-)
-
-func dumpRemoteAddr(r *http.Request) string {
- return r.RemoteAddr
-}
-
-func dumpUserAgent(r *http.Request) string {
- var all string
- for param, values := range r.URL.Query() {
- for _, value := range values {
- all += fmt.Sprint(" Query:", param, value)
- }
- }
- // hostname := r.URL.Query().Get("hostname")
- return r.UserAgent() + all
-}
-
-func dumpClient(r *http.Request) {
- /*
- var host, url, proto, addr, agent string
-
- host = r.Host
- url = r.URL.String()
- proto = r.Proto
- addr = r.RemoteAddr
- agent = r.UserAgent()
-
- log.Warn(host, proto, addr, url, agent)
-
- fmt.Fprintln(accessf, time.Now(), host, proto, addr, url, agent)
- // return
-
- fmt.Fprintln(clientf)
- fmt.Fprintln(clientf, time.Now())
- // Basic request information
- fmt.Fprintln(clientf, "Method:", r.Method)
- fmt.Fprintln(clientf, "URL:", r.URL)
- fmt.Fprintln(clientf, "Protocol:", r.Proto)
- fmt.Fprintln(clientf, "Host:", r.Host)
- fmt.Fprintln(clientf, "Remote Address:", r.RemoteAddr)
-
- // Headers
- fmt.Fprintln(clientf, "Headers:")
- for name, values := range r.Header {
- for _, value := range values {
- fmt.Fprintln(clientf, "Headers:", name, value)
- }
- }
-
- // Query parameters
- fmt.Fprintln(clientf, "Query Parameters:")
- for param, values := range r.URL.Query() {
- for _, value := range values {
- fmt.Fprintln(clientf, "Query:", param, value)
- }
- }
-
- // User-Agent
- fmt.Fprintln(clientf, "User-Agent:", r.UserAgent())
-
- // Content Length
- fmt.Fprintln(clientf, "Content Length:", r.ContentLength)
-
- // Cookies
- fmt.Fprintln(clientf, "Cookies:")
- for _, cookie := range r.Cookies() {
- fmt.Fprintln(clientf, cookie.Name, cookie.Value)
- }
-
- // Request Body (if applicable)
- if r.Body != nil {
- body, err := ioutil.ReadAll(r.Body)
- if err == nil {
- fmt.Fprintln(clientf, "Body:", string(body))
- }
- }
- */
-}
diff --git a/machine.go b/machine.go
index fa2975f..7504f21 100644
--- a/machine.go
+++ b/machine.go
@@ -6,9 +6,9 @@ package main
import (
"fmt"
"net/http"
- "strings"
"time"
+ "go.wit.com/lib/protobuf/httppb"
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log"
"google.golang.org/protobuf/types/known/timestamppb"
@@ -20,25 +20,27 @@ func rawGetHostname(data []byte) *zoopb.Machine {
return newm
}
-func handleMachine(r *http.Request, w http.ResponseWriter, hostname string, data []byte) {
- hostname = strings.TrimSpace(hostname)
- newm := rawGetHostname(data)
+func handleMachine(w http.ResponseWriter, reqPB *httppb.HttpRequest) {
+ // hostname := strings.TrimSpace(reqPB.Hostname)
+ newm := rawGetHostname(reqPB.ClientData)
if newm == nil {
- log.Info("unmarshal failed on data len =", len(data))
- }
- if hostname != newm.Hostname {
- // log.Info("hostname mismatch", hostname, "vs", newm.Hostname)
- hostname = newm.Hostname
+ log.Info("unmarshal failed on data len =", len(reqPB.ClientData))
}
+ /*
+ if reqPB.Hostname != newm.Hostname {
+ // log.Info("hostname mismatch", hostname, "vs", newm.Hostname)
+ hostname = newm.Hostname
+ }
+ */
- if hostname == "" {
- ua := dumpUserAgent(r)
- ra := dumpRemoteAddr(r)
- log.Info("hostname is blank even after unmarshal. data len =", len(data), ra, ua, newm.Cpus, newm.Hostname)
+ if reqPB.Hostname == "" {
+ ua := reqPB.DumpUserAgent()
+ ra := reqPB.DumpRemoteAddr()
+ log.Info("hostname is blank even after unmarshal. data len =", len(reqPB.ClientData), ra, ua, newm.Cpus, newm.Hostname)
return
}
// log.Info("lookoing for", hostname)
- m := me.machines.FindByHostname(hostname)
+ m := me.machines.FindByHostname(reqPB.Hostname)
if m == nil {
am := new(zoopb.Machine)
am.Hostname = newm.Hostname
@@ -47,10 +49,10 @@ func handleMachine(r *http.Request, w http.ResponseWriter, hostname string, data
log.Info("new machine", am.Hostname, am.Memory)
return
}
- ua := dumpUserAgent(r)
- ra := dumpRemoteAddr(r)
+ ua := reqPB.DumpUserAgent()
+ ra := reqPB.DumpRemoteAddr()
if m.UserAgent != ua {
- log.Info("hostname ua changed len =", len(data), ra, hostname, ua)
+ log.Info("hostname ua changed len =", len(reqPB.ClientData), ra, reqPB.Hostname, ua)
m.UserAgent = ua
}
if m.Upgrade {
@@ -65,7 +67,7 @@ func handleMachine(r *http.Request, w http.ResponseWriter, hostname string, data
} else {
fmt.Fprintln(w, "good")
}
- // log.Info("update machine protobuf", hostname)
+ // log.Info("update machine protobuf", reqPB.hostname)
updateMachine(newm)
}
diff --git a/main.go b/main.go
index 41525c3..8362933 100644
--- a/main.go
+++ b/main.go
@@ -9,6 +9,7 @@ import (
"go.wit.com/dev/alexflint/arg"
"go.wit.com/lib/gui/prep"
+ "go.wit.com/lib/protobuf/httppb"
"go.wit.com/lib/protobuf/zoopb"
"go.wit.com/log"
)
@@ -43,7 +44,7 @@ func main() {
go NewWatchdog()
if !argv.NoPort {
- go startHTTP()
+ go httppb.StartHTTP(okHandler, argv.Port)
}
me.myGui.Start() // loads the GUI toolkit