diff --git a/addon/web.go b/addon/web.go new file mode 100644 index 0000000..ddf50ff --- /dev/null +++ b/addon/web.go @@ -0,0 +1,145 @@ +package addon + +import ( + "net/http" + "text/template" + + "github.com/gorilla/websocket" + "github.com/sirupsen/logrus" +) + +func (web *WebAddon) echo(w http.ResponseWriter, r *http.Request) { + c, err := web.upgrader.Upgrade(w, r, nil) + if err != nil { + web.log.Print("upgrade:", err) + return + } + defer c.Close() + for { + mt, message, err := c.ReadMessage() + if err != nil { + web.log.Println("read:", err) + break + } + web.log.Printf("recv: %s", message) + err = c.WriteMessage(mt, message) + if err != nil { + web.log.Println("write:", err) + break + } + } +} + +func home(w http.ResponseWriter, r *http.Request) { + homeTemplate.Execute(w, "ws://"+r.Host+"/echo") +} + +type WebAddon struct { + Base + addr string + upgrader *websocket.Upgrader + serverMux *http.ServeMux + server *http.Server + log *logrus.Entry +} + +func NewWebAddon() *WebAddon { + web := new(WebAddon) + web.addr = ":9081" + web.upgrader = &websocket.Upgrader{} + + web.serverMux = new(http.ServeMux) + web.serverMux.HandleFunc("/echo", web.echo) + web.serverMux.HandleFunc("/", home) + + web.server = &http.Server{Addr: web.addr, Handler: web.serverMux} + + web.log = log.WithField("in", "WebAddon") + + go func() { + web.log.Infof("server start listen at %v\n", web.addr) + err := web.server.ListenAndServe() + web.log.Error(err) + }() + + return web +} + +var homeTemplate = template.Must(template.New("").Parse(` + + + + + + + + +
+

Click "Open" to create a connection to the server, +"Send" to send a message to the server and "Close" to close the connection. +You can change the message and send multiple times. +

+

+ + +

+ +

+
+
+
+ + +`)) diff --git a/go.mod b/go.mod index cef5c16..12a0f0c 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.15 require ( github.com/andybalholm/brotli v1.0.1 github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e + github.com/gorilla/websocket v1.4.2 github.com/jordwest/mock-conn v0.0.0-20180617021051-4896c6bd1641 github.com/sirupsen/logrus v1.7.0 ) diff --git a/go.sum b/go.sum index 2d06e82..339e81f 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/jordwest/mock-conn v0.0.0-20180617021051-4896c6bd1641 h1:ChkB2s4mFDekyUUmbNE7qNhennP0rfqF2YZUOGxbhFk= github.com/jordwest/mock-conn v0.0.0-20180617021051-4896c6bd1641/go.mod h1:AJFEOPtj5Z5z3MAy+0uvjQAH02iRnQr6fnvuHYp/Jek= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=