Skip to content

Commit

Permalink
project moved from oatpp-examples repo
Browse files Browse the repository at this point in the history
  • Loading branch information
lganzzzo committed Jan 30, 2019
1 parent 4389bed commit e8ad93b
Show file tree
Hide file tree
Showing 14 changed files with 592 additions and 0 deletions.
1 change: 1 addition & 0 deletions .dockerignore
@@ -0,0 +1 @@
build/
8 changes: 8 additions & 0 deletions .gitignore
Expand Up @@ -30,3 +30,11 @@
*.exe
*.out
*.app

# custom build
build/

# idea
.idea/
cmake-build-debug/
*/cmake-build-debug/
44 changes: 44 additions & 0 deletions CMakeLists.txt
@@ -0,0 +1,44 @@
cmake_minimum_required(VERSION 3.1)

set(project_name example-consul) ## rename your project here

project(${project_name}-loader)

include(ExternalProject)

#############################################################################
## load all dependencies

ExternalProject_Add(oatpp
GIT_REPOSITORY "https://github.com/oatpp/oatpp.git"
GIT_TAG origin/master
CMAKE_ARGS -DOATPP_BUILD_TESTS=OFF
)

ExternalProject_Add(oatpp-consul
GIT_REPOSITORY "https://github.com/oatpp/oatpp-consul.git"
GIT_TAG origin/master
)

ExternalProject_Add(main
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/main
INSTALL_COMMAND cmake -E echo "SKIP INSTALL"
DEPENDS oatpp oatpp-consul
)

#############################################################################
## make run command

ExternalProject_Get_Property(main BINARY_DIR)

add_custom_target(run
COMMAND ${BINARY_DIR}/${project_name}-exe
DEPENDS main
WORKING_DIRECTORY ${BINARY_DIR}
)

#############################################################################
## make test command

enable_testing()
add_test(all-tests ${BINARY_DIR}/${project_name}-test)
12 changes: 12 additions & 0 deletions Dockerfile
@@ -0,0 +1,12 @@
FROM lganzzzo/alpine-cmake:latest

ADD . /service

WORKDIR /service/build

RUN cmake ..
RUN make

EXPOSE 8000 8000

ENTRYPOINT ["make", "run"]
27 changes: 27 additions & 0 deletions azure-pipelines.yml
@@ -0,0 +1,27 @@
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

jobs:
- job: ubuntu_16_04
displayName: 'Build - Ubuntu 16.04'
continueOnError: false
pool:
vmImage: 'Ubuntu 16.04'
container:
image: lganzzzo/ubuntu-cmake:latest
workspace:
clean: all
steps:
- script: |
mkdir build
- script: |
cmake ..
sudo make
displayName: 'CMake'
workingDirectory: build
- script: |
make test ARGS="-V"
displayName: 'Test'
workingDirectory: build
43 changes: 43 additions & 0 deletions main/CMakeLists.txt
@@ -0,0 +1,43 @@
cmake_minimum_required(VERSION 3.1)

set(project_name example-consul) ## rename your project here

project(${project_name})

set(CMAKE_CXX_STANDARD 11)

add_library(${project_name}-lib
src/AppComponent.hpp
src/Logger.cpp
src/Logger.hpp
src/controller/DemoController.hpp
src/controller/HealthController.hpp
src/dto/HealthDto.hpp
)

## link libs

find_package(oatpp 0.19.1 REQUIRED)
find_package(oatpp-consul 0.19.1 REQUIRED)

target_link_libraries(${project_name}-lib
PUBLIC oatpp::oatpp
PUBLIC oatpp::oatpp-test
PUBLIC oatpp::oatpp-consul
)

target_include_directories(${project_name}-lib PUBLIC src)

## add executables

add_executable(${project_name}-exe
src/App.cpp
)
target_link_libraries(${project_name}-exe ${project_name}-lib)
add_dependencies(${project_name}-exe ${project_name}-lib)

add_executable(${project_name}-test
test/tests.cpp
)
target_link_libraries(${project_name}-test ${project_name}-lib)
add_dependencies(${project_name}-test ${project_name}-lib)
99 changes: 99 additions & 0 deletions main/src/App.cpp
@@ -0,0 +1,99 @@
//
// main.cpp
// web-starter-project
//
// Created by Leonid on 2/12/18.
// Copyright © 2018 oatpp. All rights reserved.
//

//#define OATPP_USE_TARGET
//#define OATPP_TARGET_TEST

//////////////////////////////////
// App

#include "./controller/DemoController.hpp"
#include "./controller/HealthController.hpp"
#include "./AppComponent.hpp"
#include "./Logger.hpp"

//////////////////////////////////
// Test

#ifdef OATPP_TARGET_TEST
#include "test/ControllerLevelTest.hpp"
#include "test/RouterLevelTest.hpp"
#include "test/ConnectionHandlerLevelTest.hpp"
#endif

//////////////////////////////////
// oatpp

#include "oatpp/network/server/Server.hpp"

//////////////////////////////////
// std

#include <iostream>

/**
* run() method.
* 1) set Environment components.
* 2) add ApiController's endpoints to router
* 3) run server
*/
void run() {

AppComponent components; // Create scope Environment components

/* create ApiControllers and add endpoints to router */

auto router = components.httpRouter.getObject();

auto DemoController = DemoController::createShared();
DemoController->addEndpointsToRouter(router);

auto healthController = HealthController::createShared();
healthController->addEndpointsToRouter(router);

/* create server */

oatpp::network::server::Server server(components.serverConnectionProvider.getObject(),
components.serverConnectionHandler.getObject());

OATPP_LOGD("Server", "Running on port %s...", components.serverConnectionProvider.getObject()->getProperty("port").toString()->c_str());

server.run();

}

/**
* main
*/
int main(int argc, const char * argv[]) {

oatpp::base::Environment::setLogger(new Logger());
oatpp::base::Environment::init();

#if !defined(OATPP_USE_TARGET) | defined(OATPP_TARGET_APP)
run();
#endif

#ifdef OATPP_TARGET_TEST
OATPP_RUN_TEST(ControllerLevelTest);
OATPP_RUN_TEST(RouterLevelTest);
OATPP_RUN_TEST(ControllerLevelTest);
#endif

oatpp::base::Environment::setLogger(nullptr); ///< free Logger

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

oatpp::base::Environment::destroy();

return 0;
}
90 changes: 90 additions & 0 deletions main/src/AppComponent.hpp
@@ -0,0 +1,90 @@
//
// AppComponent.hpp
// oatpp-web-starter
//
// Created by Leonid on 3/2/18.
// Copyright © 2018 lganzzzo. All rights reserved.
//

#ifndef AppComponent_hpp
#define AppComponent_hpp

#include "oatpp-consul/Client.hpp"

#include "oatpp/web/client/HttpRequestExecutor.hpp"
#include "oatpp/web/server/HttpConnectionHandler.hpp"
#include "oatpp/web/server/HttpRouter.hpp"
#include "oatpp/network/client/SimpleTCPConnectionProvider.hpp"
#include "oatpp/network/server/SimpleTCPConnectionProvider.hpp"

#include "oatpp/parser/json/mapping/Serializer.hpp"
#include "oatpp/parser/json/mapping/Deserializer.hpp"

#include "oatpp/core/macro/component.hpp"

/**
* Class which creates and holds Application components and registers components in oatpp::base::Environment
* Order of components initialization is from top to bottom
*/
class AppComponent {
public:

/**
* Create ConnectionProvider component which listens on the port
*/
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
return oatpp::network::server::SimpleTCPConnectionProvider::createShared(8000);
}());

/**
* Create Router component
*/
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
return oatpp::web::server::HttpRouter::createShared();
}());

/**
* Create ConnectionHandler component which uses Router component to route requests
*/
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::server::ConnectionHandler>, serverConnectionHandler)([] {
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component
return oatpp::web::server::HttpConnectionHandler::createShared(router);
}());

/**
* Create ObjectMapper component to serialize/deserialize DTOs in Contoller's API
*/
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {

auto serializerConfig = oatpp::parser::json::mapping::Serializer::Config::createShared();
serializerConfig->includeNullFields = true;

auto deserializerConfig = oatpp::parser::json::mapping::Deserializer::Config::createShared();
deserializerConfig->allowUnknownFields = false;

auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared(serializerConfig, deserializerConfig);
return objectMapper;

}());

/**
* Create Demo-oatpp::consul::Client component
*/
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::consul::Client>, consulClient)([] {

OATPP_LOGD("AppComponent", "Assuming Consul is at port 8500");

// Create connection provider for Consul
// In case you need secure connection provider so you can connect to Consul via https see oatpp-libressl and tls-libressl example project
auto connectionProvider = oatpp::network::client::SimpleTCPConnectionProvider::createShared("localhost", 8500);

// Create httpRequestExecutor
auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(connectionProvider);

// Create and return consul client
return oatpp::consul::Client::createShared(requestExecutor);
}());

};

#endif /* AppComponent_hpp */
16 changes: 16 additions & 0 deletions main/src/Logger.cpp
@@ -0,0 +1,16 @@
//
// Logger.hpp
// oatpp-web-starter
//
// Created by Leonid on 3/2/18.
// Copyright © 2018 lganzzzo. All rights reserved.
//

#include "Logger.hpp"

#include <iostream>

void Logger::log(v_int32 priority, const std::string& tag, const std::string& message) {
oatpp::concurrency::SpinLock lock(m_atom);
std::cout << tag << ":" << message << "\n";
}
33 changes: 33 additions & 0 deletions main/src/Logger.hpp
@@ -0,0 +1,33 @@
//
// Logger.hpp
// oatpp-web-starter
//
// Created by Leonid on 3/2/18.
// Copyright © 2018 lganzzzo. All rights reserved.
//

#ifndef Logger_hpp
#define Logger_hpp

#include "oatpp/core/concurrency/SpinLock.hpp"
#include "oatpp/core/base/Environment.hpp"

/**
* Environment logger.
* All logs from OATPP_LOGV(...), OATPP_LOGD(...), OATPP_LOGE(...) go here
* You may ignore or redirect them here
*/
class Logger : public oatpp::base::Logger {
private:
oatpp::concurrency::SpinLock::Atom m_atom;
public:

Logger()
: m_atom(false)
{}

void log(v_int32 priority, const std::string& tag, const std::string& message) override;

};

#endif /* Logger_hpp */

0 comments on commit e8ad93b

Please sign in to comment.