@@ -20,7 +20,7 @@ DISABLE_WARNING_POP
2020
2121class HealthServiceImpl final : public grpc::health::v1::Health::Service
2222{
23- grpc::Status Check ( grpc::ServerContext* context, const grpc::health::v1::HealthCheckRequest * request, grpc::health::v1::HealthCheckResponse * reply ) override
23+ grpc::Status Check ( grpc::ServerContext * context, const grpc::health::v1::HealthCheckRequest * request, grpc::health::v1::HealthCheckResponse * reply ) override
2424 {
2525 reply->set_status ( grpc::health::v1::HealthCheckResponse_ServingStatus_SERVING );
2626 spdlog::info ( " HealthServiceImpl Check called" );
@@ -32,78 +32,78 @@ static std::unique_ptr<grpc::Server> gServer;
3232
3333void RunServer ( const consulcpp::Service & service )
3434{
35- std::string serverAddress = fmt::format ( " {}:{}" , service.mAddress , service.mPort );
36- HealthServiceImpl healthService;
35+ std::string serverAddress = fmt::format ( " {}:{}" , service.mAddress , service.mPort );
36+ HealthServiceImpl healthService;
3737 grpc::ServerBuilder builder;
3838
3939 builder.AddListeningPort ( serverAddress, grpc::InsecureServerCredentials () );
4040 builder.RegisterService ( &healthService );
4141
4242 gServer = builder.BuildAndStart ();
43- if ( gServer ){
43+ if ( gServer ) {
4444 spdlog::info ( " Server listening on {}" , serverAddress );
4545 gServer ->Wait ();
4646 spdlog::info ( " Server stopped" );
47- }else {
47+ } else {
4848 spdlog::critical ( " Cannot start gRPC server on {}" , serverAddress );
4949 }
5050}
5151
5252int main ( int argc, char * argv[] )
5353{
54- consulcpp::Consul consul;
55-
56- if ( consul.connect () ){
54+ consulcpp::Consul consul;
55+
56+ if ( consul.connect () ) {
5757 spdlog::info ( " Agent address {}" , consul.address () );
5858
59- consulcpp::Service service;
60- consulcpp::ServiceCheck check;
59+ consulcpp::Service service;
60+ consulcpp::ServiceCheck check;
6161
62- service.mId = fmt::format ( " {}" , std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now ().time_since_epoch ()).count () );
63- service.mName = " leader-demo" ;
62+ service.mId = fmt::format ( " {}" , std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now ().time_since_epoch () ).count () );
63+ service.mName = " leader-demo" ;
6464 service.mAddress = consul.address ();
65- if ( argc > 1 ){
66- service.mPort = std::stoi ( argv[1 ] );
67- }else {
65+ if ( argc > 1 ) {
66+ service.mPort = std::stoi ( argv[ 1 ] );
67+ } else {
6868 service.mPort = 50051 ;
6969 }
70- check.mInterval = " 5s" ;
70+ check.mInterval = " 5s" ;
7171 check.mDeregisterCriticalServiceAfter = " 1m" ;
72- check.mGRPC = fmt::format ( " {}:{}/Health" , service.mAddress , service.mPort );
73- service.mChecks = { check };
72+ check.mGRPC = fmt::format ( " {}:{}/Health" , service.mAddress , service.mPort );
73+ service.mChecks = { check };
7474
7575 // Purge death services
76- std::vector<consulcpp::Service> otherServices = consul.services ().findInCatalog ( service.mName , service.mTags );
77- for ( auto service: otherServices ){
76+ std::vector<consulcpp::Service> otherServices = consul.services ().findInCatalog ( service.mName , service.mTags );
77+ for ( auto service: otherServices ) {
7878 consul.services ().destroy ( service );
7979 }
8080
8181 consul.services ().create ( service );
82- consulcpp::Session session = consul.sessions ().create ();
82+ consulcpp::Session session = consul.sessions ().create ();
8383 spdlog::info ( " Session {}" , session.mId );
8484
8585 consul.kv ().set ( " my-key" , " my-value" );
8686
8787 consulcpp::Leader::Status leader = consul.leader ().acquire ( service, session );
88- if ( leader == consulcpp::Leader::Status::Yes ){
88+ if ( leader == consulcpp::Leader::Status::Yes ) {
8989 spdlog::info ( " I'm the leader" );
90- }else {
90+ } else {
9191 spdlog::info ( " I'm a follower" );
9292 }
9393 std::thread grpcThread ( RunServer, service );
9494 loop ();
95- if ( gServer ){
95+ if ( gServer ) {
9696 gServer ->Shutdown ();
9797 }
9898 grpcThread.join ();
99-
99+
100100 auto val = consul.kv ().get ( " my-key" );
101- if ( val ){
101+ if ( val ) {
102102 spdlog::info ( " Key value was {}" , val.value () );
103- if ( consul.kv ().destroy ( " my-key" ) ){
103+ if ( consul.kv ().destroy ( " my-key" ) ) {
104104 spdlog::info ( " Key deleted" );
105105 }
106- }else {
106+ } else {
107107 spdlog::info ( " Key not found" );
108108 }
109109 consul.leader ().release ( service, session );
0 commit comments