convert to httppb
This commit is contained in:
parent
a29a774aa4
commit
af7984a887
81
http.go
81
http.go
|
@ -5,43 +5,70 @@ 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)
|
||||
}
|
||||
|
||||
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
|
||||
if err != nil {
|
||||
log.Info("ReadAll() error =", 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) {
|
||||
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 == "/" {
|
||||
return
|
||||
}
|
||||
|
||||
if route == "/machine" {
|
||||
handleMachine(r, w, hostname, msg)
|
||||
handleMachine(w, reqPB)
|
||||
return
|
||||
}
|
||||
|
||||
if route == "/uptime" {
|
||||
doUptime(w)
|
||||
return
|
||||
}
|
||||
|
||||
log.Warn("BAD URL =", route)
|
||||
}
|
||||
|
||||
func doUptime(w io.Writer) {
|
||||
if me.zood == nil {
|
||||
fmt.Fprintf(w, "BAD zood == nil\n")
|
||||
return
|
||||
|
@ -64,22 +91,4 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
|
|||
} else {
|
||||
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 (
|
||||
"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))
|
||||
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)
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
3
main.go
3
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
|
||||
|
|
Loading…
Reference in New Issue