start a unified json function

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-24 22:36:56 +00:00
parent 19147ea950
commit 13af6a6273
1 changed files with 191 additions and 108 deletions

View File

@ -60,6 +60,60 @@ func checkLogin(ge *pb.Event) bool {
return false
}
func jsonAddEvent(ge *pb.Event) bool {
var url string
var httpType string
httpType = "GET"
if ge.Type == pb.Event_LOGIN {
url = API + "me"
} else if ge.Type == pb.Event_ADD {
// stackapi:4000/vms/jcarr.com?count=1&cpu=2&ram=512&disk=25
url = API + "vms/jcarr.com?count=1&cpu=2&ram=512&disk=25"
httpType = "POST"
} else if ge.Type == pb.Event_MIGRATE {
url = API + "clusters"
} else {
// if unknown, just check login
url = API + "me"
}
log.Println("jsonAddEvent() ", httpType, url)
req, err := http.NewRequest(httpType, url, nil)
if err != nil {
log.Println("can't make new req")
return false
}
req.Header.Set("X-Wit-Auth", ge.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Can't exec the req to list networks: %s", err)
return false
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("can't read resp")
return false
}
log.Printf("RESPONSE =", body)
if body == nil {
log.Println("RESPONSE is nil")
return false
} else {
len := len(body)
log.Println("body = ", string(body))
log.Println("body len = ", len)
email := gjson.Get(string(body), "email")
log.Println("email = ", email.String())
log.Println("ge.Username = ", ge.Username)
if (email.String() == ge.Username) {
log.Println("\tTHIS TOTALLY WORKED MAN")
return true
}
}
return false
}
func fetchNetworks(token string) []network {
var networks []network
req, err := http.NewRequest("GET", API + "clusters", nil)
@ -131,6 +185,122 @@ func fetchVMs(token string, network string) []vm {
return vms
}
// Also potentially look at:
// https://www.reddit.com/r/golang/comments/boznnz/an_alternative_to_gorilla_sessions_jeff_manage/
var upgrader = websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
EnableCompression: true,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
/*
func sendSampleProtobuf(conn *websocket.Conn) {
msg := pb.CreateSampleEvent()
msg.Name = "test echo over gorilla websocket"
data, _ := proto.Marshal(msg)
err := conn.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
log.Println("sendProtobuf() write:", err)
}
}
*/
func sendProtobuf(conn *websocket.Conn, msg *pb.Event) {
data, _ := proto.Marshal(msg)
err := conn.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
log.Println("sendProtobuf() write:", err)
}
}
func eventHandler(w http.ResponseWriter, r *http.Request) {
log.Println("echoHandler() START")
log.Println("echoHandler() ATTEMPT r.Header.Get", r.Header.Get("User-Agent"))
log.Println("echoHandler() ATTEMPT r.Header", r.Header)
for key, value := range r.Header {
log.Println("echoHandler() HEADER key, value =", key, value)
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade:", err)
return
}
defer conn.Close()
for {
mytype, message, err := conn.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
if mytype == websocket.TextMessage {
log.Println("echoHandler() TextMessage mytype=", mytype)
}
if mytype == websocket.BinaryMessage {
log.Println("echoHandler() BinaryMessage mytype=", mytype)
pdata := new(pb.Event)
err = proto.Unmarshal(message, pdata)
if (err != nil) {
log.Printf("readConn() something fucked up happened in Unmarshal")
}
log.Printf("recv binary: %s", pdata)
if pdata.Type == pb.Event_GET {
log.Printf("GOT GET")
e := processGetEvent(pdata)
sendProtobuf(conn, e)
}
if pdata.Type == pb.Event_MIGRATE {
log.Printf("GOT MIGRATE")
e := processGetEvent(pdata)
sendProtobuf(conn, e)
}
if pdata.Type == pb.Event_LOGIN {
log.Printf("GOT LOGIN")
e := processLoginEvent(pdata)
sendProtobuf(conn, e)
}
if pdata.Type == pb.Event_ADD {
log.Printf("GOT ADD")
e := processAddEvent(pdata)
sendProtobuf(conn, e)
}
}
}
}
func serveHome(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "Not found.", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
io.WriteString(w, "<html><body>Echo Server</body></html>")
}
var addr = flag.String("addr", ":9000", "http service address")
func main() {
flag.Parse()
http.HandleFunc("/", serveHome)
http.HandleFunc("/event", eventHandler)
log.Println("Starting http.ListenAndServe() on port 9000")
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func processGetEvent(ge *pb.Event) *pb.Event {
log.Println("processGetEvent() START")
log.Println("processGetEvent() ge.Token =", ge.Token)
@ -197,113 +367,26 @@ func processLoginEvent(ge *pb.Event) *pb.Event {
return e
}
// Also potentially look at:
// https://www.reddit.com/r/golang/comments/boznnz/an_alternative_to_gorilla_sessions_jeff_manage/
func processAddEvent(ge *pb.Event) *pb.Event {
log.Println("processAddEvent() START")
log.Println("processAddLoginEvent() ge.Token =", ge.Token)
var upgrader = websocket.Upgrader{
ReadBufferSize: 4096,
WriteBufferSize: 4096,
EnableCompression: true,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
/*
func sendSampleProtobuf(conn *websocket.Conn) {
msg := pb.CreateSampleEvent()
msg.Name = "test echo over gorilla websocket"
data, _ := proto.Marshal(msg)
err := conn.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
log.Println("sendProtobuf() write:", err)
}
}
*/
func sendProtobuf(conn *websocket.Conn, msg *pb.Event) {
data, _ := proto.Marshal(msg)
err := conn.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
log.Println("sendProtobuf() write:", err)
}
}
func eventHandler(w http.ResponseWriter, r *http.Request) {
log.Println("echoHandler() START")
log.Println("echoHandler() ATTEMPT r.Header.Get", r.Header.Get("User-Agent"))
log.Println("echoHandler() ATTEMPT r.Header", r.Header)
for key, value := range r.Header {
log.Println("echoHandler() HEADER key, value =", key, value)
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade:", err)
return
}
defer conn.Close()
for {
mytype, message, err := conn.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
if mytype == websocket.TextMessage {
log.Println("echoHandler() TextMessage mytype=", mytype)
}
if mytype == websocket.BinaryMessage {
log.Println("echoHandler() BinaryMessage mytype=", mytype)
pdata := new(pb.Event)
err = proto.Unmarshal(message, pdata)
if (err != nil) {
log.Printf("readConn() something fucked up happened in Unmarshal")
}
log.Printf("recv binary: %s", pdata)
if pdata.Type == pb.Event_MIGRATE {
log.Printf("GOT MIGRATE")
e := processGetEvent(pdata)
sendProtobuf(conn, e)
}
if pdata.Type == pb.Event_LOGIN {
log.Printf("GOT LOGIN")
e := processLoginEvent(pdata)
sendProtobuf(conn, e)
}
if pdata.Type == pb.Event_GET {
log.Printf("GOT GET")
e := processGetEvent(pdata)
sendProtobuf(conn, e)
}
}
}
}
func serveHome(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "Not found.", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
io.WriteString(w, "<html><body>Echo Server</body></html>")
}
var addr = flag.String("addr", ":9000", "http service address")
func main() {
flag.Parse()
http.HandleFunc("/", serveHome)
http.HandleFunc("/event", eventHandler)
log.Println("Starting http.ListenAndServe() on port 9000")
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
log.Println("processAddEvent() NEED TO LOGIN HERE OR CHECK TOKEN WORKS")
if checkLogin(ge) {
log.Println("processAddEvent() LOGIN WORKED")
log.Println("processAddEvent() LOGIN WORKED")
log.Println("processAddEvent() LOGIN WORKED")
} else {
log.Println("processAddEvent() LOGIN FAILED")
log.Println("processAddEvent() LOGIN FAILED")
log.Println("processAddEvent() LOGIN FAILED")
e := pb.MakeFailResponse()
e.Comment = "LOGIN FAILED"
return e
}
jsonAddEvent(ge)
log.Println("processAddEvent() END")
return nil
}