convert to httppb
This commit is contained in:
parent
a29a774aa4
commit
af7984a887
83
http.go
83
http.go
|
@ -5,43 +5,70 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.wit.com/lib/protobuf/httppb"
|
||||||
"go.wit.com/log"
|
"go.wit.com/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// remove '?' part and trailing '/'
|
/*
|
||||||
func cleanURL(url string) string {
|
if strings.HasPrefix(route, "/repos/") {
|
||||||
url = "/" + strings.Trim(url, "/")
|
pb := gitpb.NewRepos()
|
||||||
return url
|
if err := pb.Unmarshal(reqPB.ClientData); err == nil {
|
||||||
}
|
reqPB.Log("Repos Unmarshal() len=%d", pb.Len())
|
||||||
|
} else {
|
||||||
func okHandler(w http.ResponseWriter, r *http.Request) {
|
reqPB.Logf("Repos Unmarshal() err=%v", err)
|
||||||
// log.Info("Got URL Path: ", r.URL.Path)
|
}
|
||||||
route := cleanURL(r.URL.Path)
|
result := gitpb.NewRepos()
|
||||||
|
switch route {
|
||||||
hostname := r.URL.Query().Get("hostname")
|
case "/repos/check":
|
||||||
// flag := r.URL.Query().Get("flag")
|
result = addRequest(pb, reqPB)
|
||||||
|
reqPB.Logf("repos check result.Len()=%d pb.Len()=%d\n", result.Len(), pb.Len())
|
||||||
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
case "/repos/pull":
|
||||||
if err != nil {
|
result = pullRequest(pb, reqPB)
|
||||||
log.Info("ReadAll() error =", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
func okHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
reqPB, err := httppb.ReqToPB(r)
|
||||||
|
reqPB.Logf("START: Got %d bytes from the client", len(reqPB.ClientData))
|
||||||
|
if err != nil {
|
||||||
|
reqPB.Logf("httppb err %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
route := reqPB.Route
|
||||||
|
|
||||||
if route == "/" {
|
if route == "/" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if route == "/machine" {
|
if route == "/machine" {
|
||||||
handleMachine(r, w, hostname, msg)
|
handleMachine(w, reqPB)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if route == "/uptime" {
|
if route == "/uptime" {
|
||||||
|
doUptime(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Warn("BAD URL =", route)
|
||||||
|
}
|
||||||
|
|
||||||
|
func doUptime(w io.Writer) {
|
||||||
if me.zood == nil {
|
if me.zood == nil {
|
||||||
fmt.Fprintf(w, "BAD zood == nil\n")
|
fmt.Fprintf(w, "BAD zood == nil\n")
|
||||||
return
|
return
|
||||||
|
@ -64,22 +91,4 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(w, "BAD machine count=(%d) upgrade=(%d) to %s\n", count, bad, me.zood.version)
|
fmt.Fprintf(w, "BAD machine count=(%d) upgrade=(%d) to %s\n", count, bad, me.zood.version)
|
||||||
}
|
}
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
83
httpDump.go
83
httpDump.go
|
@ -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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
32
machine.go
32
machine.go
|
@ -6,9 +6,9 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.wit.com/lib/protobuf/httppb"
|
||||||
"go.wit.com/lib/protobuf/zoopb"
|
"go.wit.com/lib/protobuf/zoopb"
|
||||||
"go.wit.com/log"
|
"go.wit.com/log"
|
||||||
"google.golang.org/protobuf/types/known/timestamppb"
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||||||
|
@ -20,25 +20,27 @@ func rawGetHostname(data []byte) *zoopb.Machine {
|
||||||
return newm
|
return newm
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleMachine(r *http.Request, w http.ResponseWriter, hostname string, data []byte) {
|
func handleMachine(w http.ResponseWriter, reqPB *httppb.HttpRequest) {
|
||||||
hostname = strings.TrimSpace(hostname)
|
// hostname := strings.TrimSpace(reqPB.Hostname)
|
||||||
newm := rawGetHostname(data)
|
newm := rawGetHostname(reqPB.ClientData)
|
||||||
if newm == nil {
|
if newm == nil {
|
||||||
log.Info("unmarshal failed on data len =", len(data))
|
log.Info("unmarshal failed on data len =", len(reqPB.ClientData))
|
||||||
}
|
}
|
||||||
if hostname != newm.Hostname {
|
/*
|
||||||
|
if reqPB.Hostname != newm.Hostname {
|
||||||
// log.Info("hostname mismatch", hostname, "vs", newm.Hostname)
|
// log.Info("hostname mismatch", hostname, "vs", newm.Hostname)
|
||||||
hostname = newm.Hostname
|
hostname = newm.Hostname
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if hostname == "" {
|
if reqPB.Hostname == "" {
|
||||||
ua := dumpUserAgent(r)
|
ua := reqPB.DumpUserAgent()
|
||||||
ra := dumpRemoteAddr(r)
|
ra := reqPB.DumpRemoteAddr()
|
||||||
log.Info("hostname is blank even after unmarshal. data len =", len(data), ra, ua, newm.Cpus, newm.Hostname)
|
log.Info("hostname is blank even after unmarshal. data len =", len(reqPB.ClientData), ra, ua, newm.Cpus, newm.Hostname)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// log.Info("lookoing for", hostname)
|
// log.Info("lookoing for", hostname)
|
||||||
m := me.machines.FindByHostname(hostname)
|
m := me.machines.FindByHostname(reqPB.Hostname)
|
||||||
if m == nil {
|
if m == nil {
|
||||||
am := new(zoopb.Machine)
|
am := new(zoopb.Machine)
|
||||||
am.Hostname = newm.Hostname
|
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)
|
log.Info("new machine", am.Hostname, am.Memory)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ua := dumpUserAgent(r)
|
ua := reqPB.DumpUserAgent()
|
||||||
ra := dumpRemoteAddr(r)
|
ra := reqPB.DumpRemoteAddr()
|
||||||
if m.UserAgent != ua {
|
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
|
m.UserAgent = ua
|
||||||
}
|
}
|
||||||
if m.Upgrade {
|
if m.Upgrade {
|
||||||
|
@ -65,7 +67,7 @@ func handleMachine(r *http.Request, w http.ResponseWriter, hostname string, data
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintln(w, "good")
|
fmt.Fprintln(w, "good")
|
||||||
}
|
}
|
||||||
// log.Info("update machine protobuf", hostname)
|
// log.Info("update machine protobuf", reqPB.hostname)
|
||||||
updateMachine(newm)
|
updateMachine(newm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
3
main.go
3
main.go
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"go.wit.com/dev/alexflint/arg"
|
"go.wit.com/dev/alexflint/arg"
|
||||||
"go.wit.com/lib/gui/prep"
|
"go.wit.com/lib/gui/prep"
|
||||||
|
"go.wit.com/lib/protobuf/httppb"
|
||||||
"go.wit.com/lib/protobuf/zoopb"
|
"go.wit.com/lib/protobuf/zoopb"
|
||||||
"go.wit.com/log"
|
"go.wit.com/log"
|
||||||
)
|
)
|
||||||
|
@ -43,7 +44,7 @@ func main() {
|
||||||
|
|
||||||
go NewWatchdog()
|
go NewWatchdog()
|
||||||
if !argv.NoPort {
|
if !argv.NoPort {
|
||||||
go startHTTP()
|
go httppb.StartHTTP(okHandler, argv.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
me.myGui.Start() // loads the GUI toolkit
|
me.myGui.Start() // loads the GUI toolkit
|
||||||
|
|
Loading…
Reference in New Issue