Skip to content

Commit

Permalink
feat: add websocket support
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Dec 6, 2019
1 parent 688beee commit 26047e6
Show file tree
Hide file tree
Showing 8 changed files with 1,439 additions and 251 deletions.
12 changes: 12 additions & 0 deletions api/calcapi.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ service Service {
rpc KeyValueStringGet(KeyValueStringGet.Input) returns (KeyValueStringGet.Output) { option (google.api.http) = { get: "/api/key-value-string-get/{key}"}; };
rpc KeyValueFloatSet(KeyValueFloatSet.Input) returns (KeyValueFloatSet.Output) { option (google.api.http) = { post: "/api/key-value-float-set/{key}"; body: "*" }; };
rpc KeyValueFloatGet(KeyValueFloatGet.Input) returns (KeyValueFloatGet.Output) { option (google.api.http) = { get: "/api/key-value-float-get/{key}"}; };
rpc EchoStream(stream EchoStream.Input) returns (stream EchoStream.Output) { option (google.api.http) = {post: "/api/echo-stream", body: "*"}; };
rpc TestReadStream(TestReadStream.Input) returns (stream TestReadStream.Output) { option (google.api.http) = {get: "/api/test-read-stream"}; };

// SoundcloudAlbums
// Airtable...
Expand All @@ -50,6 +52,16 @@ service Service {
// Genius...
}

message EchoStream {
message Input { string msg = 1; }
message Output { string msg = 1; }
}

message TestReadStream {
message Input {}
message Output { string msg = 1; }
}

message CounterInc {
message Input { string key = 1; double gap = 2; }
message Output { double value = 2; }
Expand Down
35 changes: 8 additions & 27 deletions cmd/calcbiz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/rs/cors"
minify "github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/html"
"github.com/tmc/grpc-websocket-proxy/wsproxy"
chilogger "github.com/treastech/logger"
"github.com/urfave/cli"
"go.uber.org/zap"
Expand Down Expand Up @@ -70,33 +71,12 @@ func main() {
Action: server,
Flags: []cli.Flag{
// server options
cli.StringFlag{
Name: "http-bind",
Usage: "TCP port address for HTTP server",
Value: ":9000",
},
cli.StringFlag{
Name: "grpc-bind",
Usage: "TCP port address for gRPC server",
Value: ":9001",
},
cli.BoolFlag{
Name: "debug",
Usage: "Enable debug mode",
},
cli.StringFlag{Name: "http-bind", Usage: "TCP port address for HTTP server", Value: ":9000"},
cli.StringFlag{Name: "grpc-bind", Usage: "TCP port address for gRPC server", Value: ":9001"},
cli.BoolFlag{Name: "debug", Usage: "Enable debug mode"},
// service options
cli.StringFlag{
Name: "soundcloud-client-id",
Value: "<configure-me>",
Usage: "SoundCloud CLIENT_ID",
EnvVar: "SOUNDCLOUD_CLIENT_ID",
},
cli.IntFlag{
Name: "soundcloud-user-id",
Value: 96137699,
Usage: "SoundCloud USER_ID",
EnvVar: "SOUNDCLOUD_USER_ID",
},
cli.StringFlag{Name: "soundcloud-client-id", Value: "<configure-me>", Usage: "SoundCloud CLIENT_ID", EnvVar: "SOUNDCLOUD_CLIENT_ID"},
cli.IntFlag{Name: "soundcloud-user-id", Value: 96137699, Usage: "SoundCloud USER_ID", EnvVar: "SOUNDCLOUD_USER_ID"},
},
},
}
Expand Down Expand Up @@ -228,7 +208,8 @@ func startHTTPServer(ctx context.Context, opts *serverOptions) error {
r.Mount("/", routerHandler)

zap.L().Info("starting HTTP server", zap.String("bind", opts.HTTPBind))
return http.ListenAndServe(opts.HTTPBind, r)
m := wsproxy.WebsocketProxy(r) // FIXME: with logger
return http.ListenAndServe(opts.HTTPBind, m)
}

func startGRPCServer(ctx context.Context, opts *serverOptions) error {
Expand Down
2 changes: 1 addition & 1 deletion gen.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
01de06cfe2c7566d9894afbf15ac8dd904623f90 ./api/calcapi.proto
431b63cd908588e398bbb94d1e642e585441099c ./api/dashboard.proto
5c613daf5318661d709f8766163ef986d885b72d Makefile
a0415fc5002d339337b03fe97801376ff3be3bbd ./api/crew.proto
bf9f980c132b726eee22ccb88d9cfcdb275cfadb ./api/calcapi.proto
ffa8325482af5c58dd816e9404f0226db0a1d7de ./api/soundcloud.proto
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/google/uuid v1.1.1 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
github.com/gorilla/mux v1.7.3
github.com/gorilla/websocket v1.4.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0
github.com/grpc-ecosystem/grpc-gateway v1.12.1
github.com/huandu/xstrings v1.2.1 // indirect
Expand All @@ -34,6 +35,7 @@ require (
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c // indirect
github.com/tdewolff/minify/v2 v2.6.1
github.com/tdewolff/parse/v2 v2.3.15 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5
github.com/tpyolang/tpyo-cli v1.0.0
github.com/treastech/logger v0.0.0-20180705232552-e381e9ecf2e3
github.com/urfave/cli v1.22.2
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 h1:THDBEeQ9xZ8JEaCLyLQqXMMdRqNr0QAUJTIkQAUtFjg=
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0/go.mod h1:f5nM7jw/oeRSadq3xCzHAvxcr8HZnzsqU6ILg/0NiiE=
github.com/grpc-ecosystem/grpc-gateway v1.8.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
Expand Down Expand Up @@ -126,6 +128,7 @@ github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQL
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down Expand Up @@ -192,6 +195,7 @@ github.com/shazow/memoizer v0.0.0-20130904030615-74fc48eaeadc/go.mod h1:MSsSkysG
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
Expand All @@ -217,6 +221,8 @@ github.com/tdewolff/parse/v2 v2.3.15 h1:LxKGiSw/7TP2DthHA6TTpnnd9qUynx3rMvGtZiep
github.com/tdewolff/parse/v2 v2.3.15/go.mod h1:+V2lSZ93xpH2Csfs/vtNY1Fjr8kcFMsZKjyLoSkZbM0=
github.com/tdewolff/test v1.0.4 h1:ih38SXuQJ32Hng5EtSW32xqEsVeMnPp6nNNRPhBBDE8=
github.com/tdewolff/test v1.0.4/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 h1:LnC5Kc/wtumK+WB441p7ynQJzVuNRJiqddSIE3IlSEQ=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tpyolang/tpyo-cli v1.0.0 h1:vf0ddfW9Si2pn3rzPe51hFpWmvkkVZNWnmXLIsAe2gg=
github.com/tpyolang/tpyo-cli v1.0.0/go.mod h1:gYyKr7Mog1FDfbA58jt3iYlcUZY5ztHkHjioIDLuu1w=
github.com/treastech/logger v0.0.0-20180705232552-e381e9ecf2e3 h1:0SnC9653NEySn3YUL1UV9o45KfQzszcOpIJ2f2BlrVg=
Expand Down
32 changes: 32 additions & 0 deletions pkg/calcapi/api_stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package calcapi

import (
fmt "fmt"
"time"
)

func (svc *svc) EchoStream(srv Service_EchoStreamServer) error {
for {
req, err := srv.Recv()
if err != nil {
return err
}

err = srv.Send(&EchoStream_Output{Msg: req.Msg})
if err != nil {
return err
}
}
}

func (svc *svc) TestReadStream(in *TestReadStream_Input, stream Service_TestReadStreamServer) error {
start := time.Now()
for i := 0; i < 5; i++ {
time.Sleep(time.Second)
err := stream.Send(&TestReadStream_Output{Msg: fmt.Sprintf("Hello! %v", time.Since(start))})
if err != nil {
return err
}
}
return nil
}
Loading

0 comments on commit 26047e6

Please sign in to comment.