Skip to content

Commit 31b3689

Browse files
committed
Use the new oatpp ORM.
1 parent 8e0a13f commit 31b3689

21 files changed

+484
-547
lines changed

CMakeLists.txt

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,49 @@ project(${project_name})
77
set(CMAKE_CXX_STANDARD 11)
88

99
add_library(${project_name}-lib
10-
src/App.cpp
11-
src/AppComponent.hpp
12-
src/ServiceComponent.hpp
13-
src/SwaggerComponent.hpp
14-
src/controller/UserController.cpp
1510
src/controller/UserController.hpp
16-
src/db/Database.cpp
17-
src/db/Database.hpp
11+
src/db/UserDb.hpp
1812
src/dto/ConfigDto.hpp
19-
src/dto/ErrorDto.hpp
13+
src/dto/PageDto.hpp
14+
src/dto/StatusDto.hpp
2015
src/dto/UserDto.hpp
16+
src/service/UserService.cpp
17+
src/service/UserService.hpp
18+
src/AppComponent.hpp
19+
src/DatabaseComponent.hpp
20+
src/ErrorHandler.cpp
21+
src/ErrorHandler.hpp
22+
src/ServiceComponent.hpp
23+
src/SwaggerComponent.hpp
2124
)
2225

2326
target_include_directories(${project_name}-lib PUBLIC src)
2427

2528
## link libs
2629

27-
find_package(oatpp 1.2.0 REQUIRED)
28-
find_package(oatpp-swagger 1.2.0 REQUIRED)
30+
find_package(oatpp 1.2.0 REQUIRED)
31+
find_package(oatpp-swagger 1.2.0 REQUIRED)
32+
find_package(oatpp-postgresql 1.2.0 REQUIRED)
2933

3034
target_link_libraries(${project_name}-lib
3135
PUBLIC oatpp::oatpp
3236
PUBLIC oatpp::oatpp-test
3337
PUBLIC oatpp::oatpp-swagger
38+
PUBLIC oatpp::oatpp-postgresql
3439
)
3540

3641
add_definitions(
3742
# Path to swagger-ui resources #
38-
-DOATPP_SWAGGER_RES_PATH="${oatpp-swagger_INCLUDE_DIRS}/../bin/oatpp-swagger/res"
43+
-DOATPP_SWAGGER_RES_PATH="${OATPP_BASE_DIR}/bin/oatpp-swagger/res"
3944

4045
# Path to config file #
4146
-DCONFIG_PATH="${CMAKE_CURRENT_LIST_DIR}/resources/config.json"
42-
)
43-
44-
#################################################################
45-
## link postgresql client
4647

47-
include(FindPkgConfig)
48-
49-
set(ENV{PKG_CONFIG_PATH} "/usr/local/pgsql/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}") ## change this if needed
50-
51-
pkg_check_modules(PKG_PQ REQUIRED libpq)
52-
53-
message("PKG_PQ_INCLUDE_DIRS=${PKG_PQ_INCLUDE_DIRS}")
54-
message("PKG_PQ_LIBRARY_DIRS=${PKG_PQ_LIBRARY_DIRS}")
55-
message("PKG_PQ_LIBRARIES=${PKG_PQ_LIBRARIES}")
56-
57-
target_include_directories(${project_name}-lib
58-
PUBLIC ${PKG_PQ_INCLUDE_DIRS}
59-
)
60-
61-
link_directories(
62-
${PKG_PQ_LIBRARY_DIRS}
63-
)
64-
65-
target_link_libraries(${project_name}-lib
66-
PUBLIC ${PKG_PQ_LIBRARIES}
48+
## Path to database migration scripts
49+
-DDATABASE_MIGRATIONS="${CMAKE_CURRENT_SOURCE_DIR}/sql"
6750
)
6851

6952
#################################################################
70-
7153
## add executables
7254

7355
add_executable(${project_name}-exe

resources/config.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
{
22
"dev": {
3+
"host": "0.0.0.0",
34
"port": 8000,
45
"swaggerHost": "localhost:8000",
5-
"dbHost": "localhost",
6-
"dbUser": "postgres",
7-
"dbPass": "db-pass",
8-
"dbName": "postgres"
6+
"dbConnectionString": "postgresql://postgres:db-pass@localhost:5432/postgres"
97
},
108
"local-docker": {
9+
"host": "0.0.0.0",
1110
"port": 8000,
1211
"swaggerHost": "localhost:8000",
13-
"dbHost": "db",
14-
"dbUser": "postgres",
15-
"dbPass": "db-pass",
16-
"dbName": "postgres"
12+
"dbConnectionString": "postgresql://postgres:db-pass@db:5432/postgres"
1713
}
1814
}

sql/001_init.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
3+
4+
CREATE TABLE IF NOT EXISTS AppUser (
5+
id varchar (256) PRIMARY KEY,
6+
username varchar (256) NOT NULL,
7+
email varchar (256) NOT NULL,
8+
password varchar (256) NOT NULL,
9+
role varchar (256) NULL,
10+
CONSTRAINT UK_APPUSER_USERNAME UNIQUE (username),
11+
CONSTRAINT UK_APPUSER_EMAIL UNIQUE (email)
12+
);
13+
14+
INSERT INTO AppUser
15+
(id, username, email, password, role) VALUES (uuid_generate_v4(), 'admin', '[email protected]', 'admin', 'ROLE_ADMIN');

src/App.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11

2-
#include "./controller/UserController.hpp"
3-
#include "./AppComponent.hpp"
4-
#include "./ServiceComponent.hpp"
5-
#include "./SwaggerComponent.hpp"
6-
7-
#include "oatpp/network/Server.hpp"
2+
#include "controller/UserController.hpp"
3+
#include "AppComponent.hpp"
4+
#include "DatabaseComponent.hpp"
5+
#include "ServiceComponent.hpp"
6+
#include "SwaggerComponent.hpp"
87

98
#include "oatpp-swagger/Controller.hpp"
109

11-
#include <iostream>
10+
#include "oatpp/network/Server.hpp"
1211

12+
#include <iostream>
1313

14-
/**
15-
* run() method.
16-
* 1) set Environment components.
17-
* 2) add ApiController's endpoints to router
18-
* 3) run server
19-
*/
2014
void run(const oatpp::base::CommandLineArguments& args) {
2115

2216
AppComponent appComponent(args);
2317
ServiceComponent serviceComponent;
2418
SwaggerComponent swaggerComponent;
19+
DatabaseComponent databaseComponent;
2520

2621
/* create ApiControllers and add endpoints to router */
2722

@@ -47,21 +42,12 @@ void run(const oatpp::base::CommandLineArguments& args) {
4742

4843
}
4944

50-
/**
51-
* main
52-
*/
5345
int main(int argc, const char * argv[]) {
5446

5547
oatpp::base::Environment::init();
5648

5749
run(oatpp::base::CommandLineArguments(argc, argv));
5850

59-
/* Print how much objects were created during app running, and what have left-probably leaked */
60-
/* Disable object counting for release builds using '-D OATPP_DISABLE_ENV_OBJECT_COUNTERS' flag for better performance */
61-
std::cout << "\nEnvironment:\n";
62-
std::cout << "objectsCount = " << oatpp::base::Environment::getObjectsCount() << "\n";
63-
std::cout << "objectsCreated = " << oatpp::base::Environment::getObjectsCreated() << "\n\n";
64-
6551
oatpp::base::Environment::destroy();
6652

6753
return 0;

src/DatabaseComponent.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
#ifndef EXAMPLE_POSTGRESQL_DATABASECOMPONENT_HPP
3+
#define EXAMPLE_POSTGRESQL_DATABASECOMPONENT_HPP
4+
5+
#include "db/UserDb.hpp"
6+
#include "dto/ConfigDto.hpp"
7+
8+
class DatabaseComponent {
9+
public:
10+
11+
/**
12+
* Create database client
13+
*/
14+
OATPP_CREATE_COMPONENT(std::shared_ptr<UserDb>, userDb)([] {
15+
16+
OATPP_COMPONENT(oatpp::Object<ConfigDto>, config); // Get config component
17+
18+
/* Create database-specific ConnectionProvider */
19+
auto connectionProvider = std::make_shared<oatpp::postgresql::ConnectionProvider>(config->dbConnectionString);
20+
21+
/* Create database-specific ConnectionPool */
22+
auto connectionPool = oatpp::postgresql::ConnectionPool::createShared(connectionProvider,
23+
10 /* max-connections */,
24+
std::chrono::seconds(5) /* connection TTL */);
25+
26+
/* Create database-specific Executor */
27+
auto executor = std::make_shared<oatpp::postgresql::Executor>(connectionPool);
28+
29+
/* Create MyClient database client */
30+
return std::make_shared<UserDb>(executor);
31+
32+
}());
33+
34+
35+
};
36+
37+
#endif //EXAMPLE_POSTGRESQL_DATABASECOMPONENT_HPP

src/ErrorHandler.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#include "ErrorHandler.hpp"
3+
4+
ErrorHandler::ErrorHandler(const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& objectMapper)
5+
: m_objectMapper(objectMapper)
6+
{}
7+
8+
std::shared_ptr<ErrorHandler::OutgoingResponse>
9+
ErrorHandler::handleError(const Status& status, const oatpp::String& message, const Headers& headers) {
10+
11+
auto error = StatusDto::createShared();
12+
error->status = "ERROR";
13+
error->code = status.code;
14+
error->message = message;
15+
16+
auto response = ResponseFactory::createResponse(status, error, m_objectMapper);
17+
18+
for(const auto& pair : headers.getAll()) {
19+
response->putHeader(pair.first.toString(), pair.second.toString());
20+
}
21+
22+
return response;
23+
24+
}

src/ErrorHandler.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
#ifndef EXAMPLE_POSTGRESQL_ERRORHANDLER_HPP
3+
#define EXAMPLE_POSTGRESQL_ERRORHANDLER_HPP
4+
5+
#include "dto/StatusDto.hpp"
6+
7+
#include "oatpp/web/server/handler/ErrorHandler.hpp"
8+
#include "oatpp/web/protocol/http/outgoing/ResponseFactory.hpp"
9+
10+
class ErrorHandler : public oatpp::web::server::handler::ErrorHandler {
11+
private:
12+
typedef oatpp::web::protocol::http::outgoing::Response OutgoingResponse;
13+
typedef oatpp::web::protocol::http::Status Status;
14+
typedef oatpp::web::protocol::http::outgoing::ResponseFactory ResponseFactory;
15+
private:
16+
std::shared_ptr<oatpp::data::mapping::ObjectMapper> m_objectMapper;
17+
public:
18+
19+
ErrorHandler(const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& objectMapper);
20+
21+
std::shared_ptr<OutgoingResponse>
22+
handleError(const Status& status, const oatpp::String& message, const Headers& headers) override;
23+
24+
};
25+
26+
27+
#endif //EXAMPLE_POSTGRESQL_ERRORHANDLER_HPP

src/ServiceComponent.hpp

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,33 @@
33
#define ServiceComponent_hpp
44

55
#include "dto/ConfigDto.hpp"
6-
7-
#include "db/Database.hpp"
6+
#include "ErrorHandler.hpp"
87

98
#include "oatpp/web/server/HttpConnectionHandler.hpp"
109
#include "oatpp/web/server/HttpRouter.hpp"
1110
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
1211

1312
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
14-
1513
#include "oatpp/core/macro/component.hpp"
1614

1715
class ServiceComponent {
18-
public:
19-
20-
class ErrorHandler : public oatpp::web::server::handler::ErrorHandler {
21-
public:
22-
typedef oatpp::web::protocol::http::outgoing::Response OutgoingResponse;
23-
typedef oatpp::web::protocol::http::Status Status;
24-
typedef oatpp::web::protocol::http::outgoing::ResponseFactory ResponseFactory;
25-
private:
26-
std::shared_ptr<oatpp::data::mapping::ObjectMapper> m_objectMapper;
27-
public:
28-
29-
ErrorHandler(const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& objectMapper)
30-
: m_objectMapper(objectMapper)
31-
{}
32-
33-
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper);
34-
35-
36-
std::shared_ptr<OutgoingResponse> handleError(const Status& status, const oatpp::String& message, const Headers& headers) override {
37-
38-
auto error = ErrorDto::createShared();
39-
error->code = 500;
40-
error->error = "Unhandled Error";
41-
error->message = message;
42-
43-
auto response = ResponseFactory::createResponse(Status::CODE_500, error, m_objectMapper);
44-
45-
for(const auto& pair : headers.getAll()) {
46-
response->putHeader(pair.first.toString(), pair.second.toString());
47-
}
48-
49-
return response;
50-
51-
}
52-
53-
};
54-
5516
public:
5617

5718
/**
5819
* Create ObjectMapper component to serialize/deserialize DTOs in Contoller's API
5920
*/
6021
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
61-
return oatpp::parser::json::mapping::ObjectMapper::createShared();
22+
auto mapper = oatpp::parser::json::mapping::ObjectMapper::createShared();
23+
mapper->getSerializer()->getConfig()->useBeautifier = true;
24+
return mapper;
6225
}());
6326

6427
/**
6528
* Create ConnectionProvider component which listens on the port
6629
*/
6730
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
6831
OATPP_COMPONENT(oatpp::Object<ConfigDto>, config); // Get config component
69-
return oatpp::network::tcp::server::ConnectionProvider::createShared({"localhost", config->port});
32+
return oatpp::network::tcp::server::ConnectionProvider::createShared({"localhost", config->port, oatpp::network::Address::IP_4});
7033
}());
7134

7235
/**
@@ -82,18 +45,11 @@ class ServiceComponent {
8245
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
8346
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component
8447
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper); // get ObjectMapper component
48+
8549
auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router);
8650
connectionHandler->setErrorHandler(std::make_shared<ErrorHandler>(objectMapper));
8751
return connectionHandler;
8852
}());
89-
90-
OATPP_CREATE_COMPONENT(std::shared_ptr<Database>, database)([] {
91-
OATPP_COMPONENT(oatpp::Object<ConfigDto>, config); // Get config component
92-
auto db = std::make_shared<Database>(config->dbHost, config->dbUser, config->dbPass, config->dbName);
93-
db->connect();
94-
db->init();
95-
return db;
96-
}());
9753

9854
};
9955

0 commit comments

Comments
 (0)