Last active
February 6, 2024 06:36
-
-
Save dudo/affd0635a59466f9f73e0f4f06862df4 to your computer and use it in GitHub Desktop.
Go Multi-Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"os" | |
"os/signal" | |
"syscall" | |
"github.com/orbservability/io/pkg/server" | |
pb "github.com/orbservability/schemas/v1" | |
"github.com/prometheus/client_golang/prometheus/promhttp" | |
"github.com/rs/zerolog/log" | |
"orbservability.com/ingestion-api/pkg/eventgateway" | |
) | |
func main() { | |
// Error channel for server errors | |
errChan := make(chan error, 2) | |
// Map HTTP routes to handlers, and serve HTTP | |
httpServer := server.ServeHTTP(errChan, map[string]http.Handler{"/metrics": promhttp.Handler()}) | |
// Initialize gRPC services, and serve gRPC | |
eventGateway := &eventgateway.ServiceHandler{} | |
grpcServer := server.ServeGRPC(errChan, []server.ServiceRegistrar{eventGateway}) | |
// Set up signal handling for graceful shutdown, and wait for a termination signal | |
sigChan := make(chan os.Signal, 1) | |
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) | |
// Block until a signal is received | |
select { | |
case <-sigChan: | |
log.Warn().Msg("Application shutting down") | |
server.ShutdownGRPC(grpcServer) | |
server.ShutdownHTTP(httpServer) | |
case err := <-errChan: | |
log.Fatal().Err(err).Msg("Server error") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package eventgateway | |
import ( | |
"reflect" | |
pb "github.com/orbservability/schemas/v1" | |
"google.golang.org/grpc" | |
"github.com/rs/zerolog/log" | |
) | |
type ServiceHandler struct { | |
pb.UnimplementedEventGatewayServiceServer | |
} | |
func (s *ServiceHandler) RegisterWithServer(grpcServer *grpc.Server) { | |
pb.RegisterEventGatewayServiceServer(grpcServer, s) | |
} | |
func (s *ServiceHandler) StreamEvents(stream pb.EventGatewayService_StreamEventsServer) error { | |
for { | |
msg, err := stream.Recv() | |
if err != nil { | |
return err | |
} | |
log.Debug(). | |
Stringer("msg", msg). | |
Stringer("type", reflect.TypeOf(msg)). | |
Msg("Message received") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment