Skip to content

Commit

Permalink
chore: socket.io API (#45)
Browse files Browse the repository at this point in the history
chore: socket.io API
  • Loading branch information
moul authored Dec 8, 2019
2 parents 9989997 + 5300ba7 commit c808cb3
Show file tree
Hide file tree
Showing 6 changed files with 7,010 additions and 4,144 deletions.
56 changes: 56 additions & 0 deletions api/calcapi.proto
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,60 @@ message Recettator {
string kind = 6;
string name_and_quantity = 7;
}
}

//// socket.io

message SIO {

message Join {
message Input {
string room = 1;
Peer peer = 2;
}
message Output {
repeated Peer peers = 1;
}
message Event {
string room = 1;
Peer peer = 2;
}
}

message Broadcast {
message Input {
string room = 1;
string msg = 2;
}
message Output {}
message Event {
string room = 1;
string msg = 2;
Peer peer = 3;
}
}

message Disconnect {
message Event {
string room = 1;
Peer peer = 2;
}
}

message Error {
bool is_error = 1;
string err_msg = 2;
}

message Peer {
string name = 1;
string color = 2;
}

message Context {
Peer peer = 1;
bool no_metadata = 2;
// int32 joined_at = 3;
}

}
55 changes: 38 additions & 17 deletions cmd/calcbiz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
app.Before = func(c *cli.Context) error {
config := zap.NewDevelopmentConfig()
config.Level.SetLevel(zap.DebugLevel)
config.DisableStacktrace = false
config.DisableStacktrace = true
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
l, err := config.Build()
if err != nil {
Expand Down Expand Up @@ -188,39 +188,60 @@ func startHTTPServer(ctx context.Context, opts *serverOptions) error {
routerHandler = m.Middleware(router)
}

///
r := chi.NewRouter()
// socketio server
sio, err := opts.svc.SocketIOServer()
if err != nil {
return errors.Wrap(err, "initialize socket.io server")
}

// cors
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
//AllowedOrigins: []string{"*"},
AllowOriginFunc: func(origin string) bool { return true },
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300,
})
r.Use(cors.Handler)
r.Use(chilogger.Logger(zap.L().Named("http")))
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(10 * time.Second))
r.Use(middleware.RealIP)
r.Use(middleware.RequestID)

r.Mount("/api/", gwmux)
r.Mount("/", routerHandler)
/// chi router
r := chi.NewRouter()
r.Route("/socket.io", func(r chi.Router) {
r.Use(cors.Handler)
r.Use(middleware.Recoverer)
//r.Use(chilogger.Logger(zap.L().Named("http")))
//r.Use(middleware.Timeout(10 * time.Second))
//r.Use(middleware.RealIP)
//r.Use(middleware.RequestID)
r.Mount("/", sio)
})
r.Route("/api", func(r chi.Router) {
r.Use(cors.Handler)
r.Use(chilogger.Logger(zap.L().Named("http")))
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(10 * time.Second))
r.Use(middleware.RealIP)
r.Use(middleware.RequestID)
r.Mount("/", gwmux)
})
r.Route("/", func(r chi.Router) {
r.Use(chilogger.Logger(zap.L().Named("http")))
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(10 * time.Second))
r.Use(middleware.RealIP)
r.Use(middleware.RequestID)
r.Mount("/", routerHandler)
})

// socket.io
sio, err := opts.svc.SocketIOServer()
if err != nil {
return errors.Wrap(err, "initialize socket.io server")
}
go func() {
err := sio.Serve()
if err != nil {
zap.L().Fatal("start socket.io server", zap.Error(err))
}
}()
defer sio.Close()
r.Mount("/socket.io/", sio)

zap.L().Info("starting HTTP server", zap.String("bind", opts.HTTPBind))
m := wsproxy.WebsocketProxy(r) // FIXME: with logger
Expand Down
2 changes: 1 addition & 1 deletion gen.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
431b63cd908588e398bbb94d1e642e585441099c ./api/dashboard.proto
5c613daf5318661d709f8766163ef986d885b72d Makefile
8578d7af4827f4aaa309e084a3bfe80a7146748b ./api/calcapi.proto
a0415fc5002d339337b03fe97801376ff3be3bbd ./api/crew.proto
bf9f980c132b726eee22ccb88d9cfcdb275cfadb ./api/calcapi.proto
ffa8325482af5c58dd816e9404f0226db0a1d7de ./api/soundcloud.proto
Loading

0 comments on commit c808cb3

Please sign in to comment.