Skip to content

Commit

Permalink
handle websocket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
slamdev committed May 23, 2020
1 parent e30d1ec commit 05a935a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module flux-notifier
go 1.14

require (
github.com/gorilla/websocket v1.4.2
github.com/prometheus/client_golang v1.6.0
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
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/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
Expand Down
1 change: 1 addition & 0 deletions pkg/notifier/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func serve(ctx context.Context, port int) (err error) {
mux.Handle("/health", http.HandlerFunc(handleHealthRequest))
mux.Handle("/metrics", promhttp.Handler())
mux.Handle("/v6/events", http.HandlerFunc(handleEventsRequest))
mux.Handle("/", http.HandlerFunc(handleWebsocketRequest))

srv := &http.Server{
Addr: ":" + strconv.FormatInt(int64(port), 10),
Expand Down
37 changes: 37 additions & 0 deletions pkg/notifier/websocket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package notifier

import (
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"net/http"
)

var upgrader = websocket.Upgrader{}

func handleWebsocketRequest(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
logrus.WithError(err).Error("failed to upgrade connection")
return
}
defer func() {
if err := conn.Close(); err != nil {
logrus.WithError(err).Error("failed to close connection")
}
}()

for {
mt, message, err := conn.ReadMessage()
if err != nil {
logrus.WithError(err).Error("failed to read message")
break
}

logrus.WithField("ws-message", string(message)).Info("received ws message")

if err := conn.WriteMessage(mt, message); err != nil {
logrus.WithError(err).Error("failed to write message")
break
}
}
}

0 comments on commit 05a935a

Please sign in to comment.