summaryrefslogtreecommitdiff
path: root/http.go
diff options
context:
space:
mode:
Diffstat (limited to 'http.go')
-rw-r--r--http.go111
1 files changed, 60 insertions, 51 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)
}
}