Skip to content

Commit 53605de

Browse files
clang-format
1 parent b567af6 commit 53605de

8 files changed

Lines changed: 200 additions & 169 deletions

File tree

.clang-format

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
BasedOnStyle: WebKit
3+
AlignConsecutiveMacros: true
4+
AlignConsecutiveAssignments: true
5+
AlignConsecutiveDeclarations: true
6+
AllowShortBlocksOnASingleLine: false
7+
AllowShortCaseLabelsOnASingleLine: false
8+
AllowShortIfStatementsOnASingleLine: Never
9+
AllowShortLoopsOnASingleLine: false
10+
BreakBeforeBraces: Custom
11+
BraceWrapping:
12+
AfterClass: true
13+
AfterFunction: true
14+
AfterStruct: true
15+
AfterNamespace: false
16+
BreakConstructorInitializers: BeforeComma
17+
CompactNamespaces: true
18+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
19+
PointerAlignment: Middle
20+
SortIncludes: false
21+
SpaceAfterTemplateKeyword: false
22+
SpaceBeforeParens: Never
23+
SpaceBeforeRangeBasedForLoopColon: false
24+
SpaceInEmptyParentheses: false
25+
SpacesInAngles: false
26+
SpacesInParentheses: true
27+
SpacesInSquareBrackets: true
28+
Standard: Cpp11
29+
TabWidth: '4'
30+
UseTab: Always
31+
32+
...

examples/leader/main.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ DISABLE_WARNING_POP
2020

2121
class 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

3333
void 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

5252
int 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 );

src/Consul.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,36 @@
1111

1212
struct consulcpp::Consul::Private
1313
{
14-
std::string mAgentAddress;
15-
std::string mAgentAPIVersion = "v1";
16-
std::string mAddress;
17-
Services mServices;
18-
Sessions mSessions;
19-
Leader mLeader;
20-
KV mKV;
14+
std::string mAgentAddress;
15+
std::string mAgentAPIVersion = "v1";
16+
std::string mAddress;
17+
Services mServices;
18+
Sessions mSessions;
19+
Leader mLeader;
20+
KV mKV;
2121

2222
Private( Consul & consul, const std::string & agentAddress )
2323
: mServices( consul )
2424
, mSessions( consul )
2525
, mLeader( consul )
2626
, mKV( consul )
2727
{
28-
if( agentAddress.empty() ){
28+
if( agentAddress.empty() ) {
2929
mAgentAddress = "http://127.0.0.1:8500";
30-
}else{
30+
} else {
3131
mAgentAddress = agentAddress;
3232
}
3333
}
3434
};
3535

36-
consulcpp::Consul::Consul() : d( std::make_unique<Private>( *this, std::string() ) )
36+
consulcpp::Consul::Consul()
37+
: d( std::make_unique<Private>( *this, std::string() ) )
3738
{
38-
3939
}
4040

41-
consulcpp::Consul::Consul( const std::string & agentAddress ) : d( std::make_unique<Private>( *this, agentAddress ) )
41+
consulcpp::Consul::Consul( const std::string & agentAddress )
42+
: d( std::make_unique<Private>( *this, agentAddress ) )
4243
{
43-
4444
}
4545

4646
consulcpp::Consul::~Consul()
@@ -50,12 +50,12 @@ consulcpp::Consul::~Consul()
5050
bool consulcpp::Consul::connect()
5151
{
5252
auto response = consulcpp::internal::HttpClient::get( fmt::format( "{}/{}/agent/self", d->mAgentAddress, d->mAgentAPIVersion ) );
53-
if( response ){
53+
if( response ) {
5454
auto jsonValue = nlohmann::json::parse( response.value() );
55-
try{
55+
try {
5656
auto memberJson = jsonValue.at( "Member" );
57-
d->mAddress = memberJson.at( "Addr" ).get<std::string>();
58-
}catch( const std::exception & e ){
57+
d->mAddress = memberJson.at( "Addr" ).get<std::string>();
58+
} catch( const std::exception & e ) {
5959
spdlog::error( "{}. Json was: {}", e.what(), jsonValue.dump() );
6060
}
6161
}

src/KV.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99

1010
struct consulcpp::KV::Private
1111
{
12-
consulcpp::Consul & mConsul;
12+
consulcpp::Consul & mConsul;
1313

1414
Private( Consul & consul )
1515
: mConsul( consul )
1616
{
1717
}
1818
};
1919

20-
consulcpp::KV::KV( Consul & consul ) : d( std::make_unique<Private>( consul ) )
20+
consulcpp::KV::KV( Consul & consul )
21+
: d( std::make_unique<Private>( consul ) )
2122
{
22-
2323
}
2424

2525
consulcpp::KV::~KV()
@@ -28,24 +28,24 @@ consulcpp::KV::~KV()
2828

2929
stdx::optional<std::string> consulcpp::KV::get( const std::string & key ) const
3030
{
31-
stdx::optional<std::string> res;
31+
stdx::optional<std::string> res;
3232

3333
auto response = consulcpp::internal::HttpClient::get( fmt::format( "{}/{}/kv/{}", d->mConsul.agentAddress(), d->mConsul.agentAPIVersion(), key ) );
34-
if( response && !response.value().empty() ){
35-
spdlog::info("GET VALUE {}", response.value());
34+
if( response && !response.value().empty() ) {
35+
spdlog::info( "GET VALUE {}", response.value() );
3636
auto jsonValue = nlohmann::json::parse( response.value() );
3737

38-
if( jsonValue.is_array() ){
39-
std::string value;
40-
try{
41-
value = jsonValue[0].at( "Value" ).get<std::string>();
42-
}catch( const std::exception & e ){
38+
if( jsonValue.is_array() ) {
39+
std::string value;
40+
try {
41+
value = jsonValue[ 0 ].at( "Value" ).get<std::string>();
42+
} catch( const std::exception & e ) {
4343
spdlog::error( "{}. Json was: {}", e.what(), response.value() );
4444
}
45-
if( !value.empty() ){
46-
gsl::owner<char*> out = new char[value.size()];
47-
auto info = boost::beast::detail::base64::decode( out, value.c_str(), value.size() );
48-
res = std::string( out, info.first );
45+
if( !value.empty() ) {
46+
gsl::owner<char *> out = new char[ value.size() ];
47+
auto info = boost::beast::detail::base64::decode( out, value.c_str(), value.size() );
48+
res = std::string( out, info.first );
4949
}
5050
}
5151
}
@@ -54,29 +54,29 @@ stdx::optional<std::string> consulcpp::KV::get( const std::string & key ) const
5454

5555
bool consulcpp::KV::set( const std::string & key, const std::string & value ) const
5656
{
57-
bool res = false;
57+
bool res = false;
5858

5959
auto response = consulcpp::internal::HttpClient::put( fmt::format( "{}/{}/kv/{}", d->mConsul.agentAddress(), d->mConsul.agentAPIVersion(), key ), value );
60-
if( response ){
61-
if( response.value().find( "true" ) != std::string::npos ){
60+
if( response ) {
61+
if( response.value().find( "true" ) != std::string::npos ) {
6262
res = true;
6363
}
64-
}else{
65-
spdlog::error( "KV set: Consul returns the error {}", response.error());
64+
} else {
65+
spdlog::error( "KV set: Consul returns the error {}", response.error() );
6666
}
6767
return res;
6868
}
6969

7070
bool consulcpp::KV::destroy( const std::string & key ) const
7171
{
72-
bool res = false;
72+
bool res = false;
7373

7474
auto response = consulcpp::internal::HttpClient::delete_( fmt::format( "{}/{}/kv/{}", d->mConsul.agentAddress(), d->mConsul.agentAPIVersion(), key ) );
75-
if( response ){
76-
if( response.value().find( "true" ) != std::string::npos ){
75+
if( response ) {
76+
if( response.value().find( "true" ) != std::string::npos ) {
7777
res = true;
7878
}
79-
}else{
79+
} else {
8080
spdlog::error( "KV delete: Consul returns the error {}", response.error() );
8181
}
8282
return res;

src/Leader.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99

1010
struct consulcpp::Leader::Private
1111
{
12-
consulcpp::Consul & mConsul;
12+
consulcpp::Consul & mConsul;
1313

1414
Private( Consul & consul )
1515
: mConsul( consul )
1616
{
1717
}
1818
};
1919

20-
consulcpp::Leader::Leader( Consul & consul ) : d( std::make_unique<Private>( consul ) )
20+
consulcpp::Leader::Leader( Consul & consul )
21+
: d( std::make_unique<Private>( consul ) )
2122
{
22-
2323
}
2424

2525
consulcpp::Leader::~Leader()
@@ -28,32 +28,32 @@ consulcpp::Leader::~Leader()
2828

2929
consulcpp::Leader::Status consulcpp::Leader::acquire( const Service & service, const Session & session ) const
3030
{
31-
Leader::Status res = Status::Error;
32-
std::string query = fmt::format( "{}/{}/kv/service/{}/leader?acquire={}", d->mConsul.agentAddress(), d->mConsul.agentAPIVersion(), service.mName, session.mId );
31+
Leader::Status res = Status::Error;
32+
std::string query = fmt::format( "{}/{}/kv/service/{}/leader?acquire={}", d->mConsul.agentAddress(), d->mConsul.agentAPIVersion(), service.mName, session.mId );
3333

3434
auto response = consulcpp::internal::HttpClient::put( query );
35-
if( response ){
36-
if( response.value().find( "true" ) != std::string::npos ){
35+
if( response ) {
36+
if( response.value().find( "true" ) != std::string::npos ) {
3737
res = Status::Yes;
38-
}else if( response.value().find( "false" ) != std::string::npos ){
38+
} else if( response.value().find( "false" ) != std::string::npos ) {
3939
res = Status::No;
4040
}
41-
}else{
41+
} else {
4242
spdlog::error( "Leader acquire: Consul returns the error {}", response.error() );
4343
}
4444
return res;
4545
}
4646

4747
void consulcpp::Leader::release( const Service & service, const Session & session ) const
4848
{
49-
std::string query = fmt::format( "{}/{}/kv/service/{}/leader?release={}", d->mConsul.agentAddress(), d->mConsul.agentAPIVersion(), service.mName, session.mId );
49+
std::string query = fmt::format( "{}/{}/kv/service/{}/leader?release={}", d->mConsul.agentAddress(), d->mConsul.agentAPIVersion(), service.mName, session.mId );
5050

5151
auto response = consulcpp::internal::HttpClient::put( query );
52-
if( response ){
53-
if( response.value().find( "true" ) == std::string::npos ){
52+
if( response ) {
53+
if( response.value().find( "true" ) == std::string::npos ) {
5454
spdlog::warn( "Leader release: release fails. Value was not acquire by this service?" );
5555
}
56-
}else{
56+
} else {
5757
spdlog::error( "Leader release: Consul returns the error {}", response.error() );
5858
}
5959
}

0 commit comments

Comments
 (0)