2019-05-12 00:48:59 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "flag"
|
|
|
|
import "io"
|
|
|
|
import "log"
|
|
|
|
import "net/http"
|
2019-05-12 00:57:19 -05:00
|
|
|
|
2019-05-12 00:48:59 -05:00
|
|
|
import "github.com/gorilla/websocket"
|
|
|
|
|
|
|
|
import "github.com/golang/protobuf/proto"
|
|
|
|
import pb "git.wit.com/wit/witProtobuf"
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 4096,
|
|
|
|
WriteBufferSize: 4096,
|
|
|
|
EnableCompression: true,
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-12 14:19:15 -05:00
|
|
|
func sendSampleProtobuf(conn *websocket.Conn) {
|
2019-05-12 02:28:07 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 14:19:15 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 00:57:19 -05:00
|
|
|
func eventHandler(w http.ResponseWriter, r *http.Request) {
|
2019-05-12 02:28:07 -05:00
|
|
|
log.Println("echoHandler() START")
|
|
|
|
|
2019-05-12 00:48:59 -05:00
|
|
|
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 {
|
2019-05-12 00:57:19 -05:00
|
|
|
log.Println("echoHandler() TextMessage mytype=", mytype)
|
2019-05-12 00:48:59 -05:00
|
|
|
}
|
|
|
|
if mytype == websocket.BinaryMessage {
|
2019-05-12 00:57:19 -05:00
|
|
|
log.Println("echoHandler() BinaryMessage mytype=", mytype)
|
2019-05-12 00:48:59 -05:00
|
|
|
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)
|
2019-05-12 01:07:22 -05:00
|
|
|
if pdata.Type == pb.Event_MIGRATE {
|
|
|
|
log.Printf("GOT MIGRATE")
|
2019-05-12 14:19:15 -05:00
|
|
|
e := oldmain()
|
|
|
|
sendProtobuf(conn, e)
|
2019-05-12 01:07:22 -05:00
|
|
|
}
|
2019-05-12 00:48:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-05-12 00:57:19 -05:00
|
|
|
http.HandleFunc("/event", eventHandler)
|
2019-05-12 00:48:59 -05:00
|
|
|
|
|
|
|
log.Println("Starting http.ListenAndServe() on port 9000")
|
|
|
|
err := http.ListenAndServe(*addr, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("ListenAndServe: ", err)
|
|
|
|
}
|
|
|
|
}
|