Skip to content

Commit 62db553

Browse files
committed
poject moved from oatpp-examples repo
1 parent 1de5835 commit 62db553

File tree

13 files changed

+533
-0
lines changed

13 files changed

+533
-0
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,12 @@
3030
*.exe
3131
*.out
3232
*.app
33+
34+
# custom build
35+
build/
36+
main/build/
37+
38+
# idea
39+
.idea/
40+
cmake-build-debug/
41+
*/cmake-build-debug/

CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
3+
set(project_name example-async-api) ## rename your project here
4+
5+
project(${project_name}-loader)
6+
7+
include(ExternalProject)
8+
9+
#############################################################################
10+
## load all dependencies
11+
12+
ExternalProject_Add(oatpp
13+
GIT_REPOSITORY "https://github.com/oatpp/oatpp.git"
14+
GIT_TAG origin/master
15+
CMAKE_ARGS -DOATPP_BUILD_TESTS=OFF
16+
)
17+
18+
ExternalProject_Add(main
19+
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/main
20+
INSTALL_COMMAND cmake -E echo "SKIP INSTALL"
21+
DEPENDS oatpp
22+
)
23+
24+
#############################################################################
25+
## make run command
26+
27+
ExternalProject_Get_Property(main BINARY_DIR)
28+
29+
add_custom_target(run
30+
COMMAND ${BINARY_DIR}/${project_name}-exe
31+
DEPENDS main
32+
WORKING_DIRECTORY ${BINARY_DIR}
33+
)
34+
35+
#############################################################################
36+
## make test command
37+
38+
enable_testing()
39+
add_test(all-tests ${BINARY_DIR}/${project_name}-test)

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM lganzzzo/alpine-cmake:latest
2+
3+
ADD . /service
4+
5+
WORKDIR /service/build
6+
7+
RUN cmake ..
8+
RUN make
9+
10+
EXPOSE 8000 8000
11+
12+
ENTRYPOINT ["make", "run"]

azure-pipelines.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Starter pipeline
2+
# Start with a minimal pipeline that you can customize to build and deploy your code.
3+
# Add steps that build, run tests, deploy, and more:
4+
# https://aka.ms/yaml
5+
6+
jobs:
7+
- job: ubuntu_16_04
8+
displayName: 'Build - Ubuntu 16.04'
9+
continueOnError: false
10+
pool:
11+
vmImage: 'Ubuntu 16.04'
12+
container:
13+
image: lganzzzo/ubuntu-cmake:latest
14+
workspace:
15+
clean: all
16+
steps:
17+
- script: |
18+
mkdir build
19+
- script: |
20+
cmake ..
21+
sudo make
22+
displayName: 'CMake'
23+
workingDirectory: build
24+
- script: |
25+
make test ARGS="-V"
26+
displayName: 'Test'
27+
workingDirectory: build

main/CMakeLists.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
3+
set(project_name example-async-api) ## rename your project here
4+
5+
project(${project_name})
6+
7+
set(CMAKE_CXX_STANDARD 11)
8+
9+
add_library(${project_name}-lib
10+
src/AppComponent.hpp
11+
src/Logger.hpp
12+
src/Logger.cpp
13+
src/controller/MyController.hpp
14+
src/dto/MyDTOs.hpp
15+
)
16+
17+
## link libs
18+
19+
find_package(oatpp 0.19.1 REQUIRED)
20+
21+
target_link_libraries(${project_name}-lib
22+
PUBLIC oatpp::oatpp
23+
PUBLIC oatpp::oatpp-test
24+
)
25+
26+
target_include_directories(${project_name}-lib PUBLIC src)
27+
28+
## add executables
29+
30+
add_executable(${project_name}-exe
31+
src/App.cpp
32+
)
33+
target_link_libraries(${project_name}-exe ${project_name}-lib)
34+
add_dependencies(${project_name}-exe ${project_name}-lib)
35+
36+
add_executable(${project_name}-test
37+
test/tests.cpp
38+
)
39+
target_link_libraries(${project_name}-test ${project_name}-lib)
40+
add_dependencies(${project_name}-test ${project_name}-lib)
41+
42+
set_target_properties(${project_name}-lib ${project_name}-exe ${project_name}-test PROPERTIES
43+
CXX_STANDARD 11
44+
CXX_EXTENSIONS OFF
45+
CXX_STANDARD_REQUIRED ON
46+
)

main/src/App.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// main.cpp
3+
// web-starter-project
4+
//
5+
// Created by Leonid on 2/12/18.
6+
// Copyright © 2018 oatpp. All rights reserved.
7+
//
8+
9+
//#define OATPP_USE_TARGET
10+
//#define OATPP_TARGET_TEST
11+
12+
//////////////////////////////////
13+
// App
14+
15+
#include "./controller/MyController.hpp"
16+
#include "./AppComponent.hpp"
17+
#include "./Logger.hpp"
18+
19+
//////////////////////////////////
20+
// Test
21+
22+
#ifdef OATPP_TARGET_TEST
23+
#endif
24+
25+
//////////////////////////////////
26+
// oatpp
27+
28+
#include "oatpp/network/server/Server.hpp"
29+
30+
//////////////////////////////////
31+
// std
32+
33+
#include <iostream>
34+
35+
/**
36+
* run() method.
37+
* 1) set Environment components.
38+
* 2) add ApiController's endpoints to router
39+
* 3) run server
40+
*/
41+
void run() {
42+
43+
AppComponent components; // Create scope Environment components
44+
45+
/* create ApiControllers and add endpoints to router */
46+
47+
auto router = components.httpRouter.getObject();
48+
49+
auto MyController = MyController::createShared();
50+
MyController->addEndpointsToRouter(router);
51+
52+
/* create server */
53+
54+
oatpp::network::server::Server server(components.serverConnectionProvider.getObject(),
55+
components.serverConnectionHandler.getObject());
56+
57+
OATPP_LOGD("Server", "Running on port %s...", components.serverConnectionProvider.getObject()->getProperty("port").toString()->c_str());
58+
59+
server.run();
60+
61+
}
62+
63+
/**
64+
* main
65+
*/
66+
int main(int argc, const char * argv[]) {
67+
68+
oatpp::base::Environment::setLogger(new Logger());
69+
oatpp::base::Environment::init();
70+
71+
#if !defined(OATPP_USE_TARGET) | defined(OATPP_TARGET_APP)
72+
run();
73+
#endif
74+
75+
#ifdef OATPP_TARGET_TEST
76+
#endif
77+
78+
oatpp::base::Environment::setLogger(nullptr); ///< free Logger
79+
80+
/* Print how much objects were created during app running, and what have left-probably leaked */
81+
/* Disable object counting for release builds using '-D OATPP_DISABLE_ENV_OBJECT_COUNTERS' flag for better performance */
82+
std::cout << "\nEnvironment:\n";
83+
std::cout << "objectsCount = " << oatpp::base::Environment::getObjectsCount() << "\n";
84+
std::cout << "objectsCreated = " << oatpp::base::Environment::getObjectsCreated() << "\n\n";
85+
86+
oatpp::base::Environment::destroy();
87+
88+
return 0;
89+
}

main/src/AppComponent.hpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// AppComponent.hpp
3+
// oatpp-web-starter
4+
//
5+
// Created by Leonid on 3/2/18.
6+
// Copyright © 2018 lganzzzo. All rights reserved.
7+
//
8+
9+
#ifndef AppComponent_hpp
10+
#define AppComponent_hpp
11+
12+
#include "oatpp/web/server/AsyncHttpConnectionHandler.hpp"
13+
#include "oatpp/web/server/HttpRouter.hpp"
14+
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"
15+
16+
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
17+
18+
#include "oatpp/core/macro/component.hpp"
19+
20+
/**
21+
* Class which creates and holds Application components and registers components in oatpp::base::Environment
22+
* Order of components initialization is from top to bottom
23+
*/
24+
class AppComponent {
25+
public:
26+
27+
/**
28+
* Create ConnectionProvider component which listens on the port
29+
*/
30+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
31+
/* non_blocking connections should be used with AsyncHttpConnectionHandler for AsyncIO */
32+
return oatpp::network::server::SimpleTCPConnectionProvider::createShared(8000, true /* true for non_blocking */);
33+
}());
34+
35+
/**
36+
* Create Router component
37+
*/
38+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
39+
return oatpp::web::server::HttpRouter::createShared();
40+
}());
41+
42+
/**
43+
* Create ConnectionHandler component which uses Router component to route requests
44+
*/
45+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, serverConnectionHandler)([] {
46+
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component
47+
/* Async ConnectionHandler for Async IO and Coroutine based endpoints */
48+
return oatpp::web::server::AsyncHttpConnectionHandler::createShared(router);
49+
}());
50+
51+
/**
52+
* Create ObjectMapper component to serialize/deserialize DTOs in Contoller's API
53+
*/
54+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
55+
auto serializerConfig = oatpp::parser::json::mapping::Serializer::Config::createShared();
56+
auto deserializerConfig = oatpp::parser::json::mapping::Deserializer::Config::createShared();
57+
deserializerConfig->allowUnknownFields = false;
58+
auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared(serializerConfig, deserializerConfig);
59+
return objectMapper;
60+
}());
61+
62+
};
63+
64+
#endif /* AppComponent_hpp */

main/src/Logger.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Logger.hpp
3+
// oatpp-web-starter
4+
//
5+
// Created by Leonid on 3/2/18.
6+
// Copyright © 2018 lganzzzo. All rights reserved.
7+
//
8+
9+
#include "Logger.hpp"
10+
11+
#include <iostream>
12+
13+
void Logger::log(v_int32 priority, const std::string& tag, const std::string& message) {
14+
oatpp::concurrency::SpinLock lock(m_atom);
15+
std::cout << tag << ":" << message << "\n";
16+
}

main/src/Logger.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Logger.hpp
3+
// oatpp-web-starter
4+
//
5+
// Created by Leonid on 3/2/18.
6+
// Copyright © 2018 lganzzzo. All rights reserved.
7+
//
8+
9+
#ifndef Logger_hpp
10+
#define Logger_hpp
11+
12+
#include "oatpp/core/concurrency/SpinLock.hpp"
13+
#include "oatpp/core/base/Environment.hpp"
14+
15+
/**
16+
* Environment logger.
17+
* All logs from OATPP_LOGV(...), OATPP_LOGD(...), OATPP_LOGE(...) go here
18+
* You may ignore or redirect them here
19+
*/
20+
class Logger : public oatpp::base::Logger {
21+
private:
22+
oatpp::concurrency::SpinLock::Atom m_atom;
23+
public:
24+
25+
Logger()
26+
: m_atom(false)
27+
{}
28+
29+
void log(v_int32 priority, const std::string& tag, const std::string& message) override;
30+
31+
};
32+
33+
#endif /* Logger_hpp */

0 commit comments

Comments
 (0)