Mercurial > hg > orthanc
changeset 2389:88402bd5dbf5
sample of a micro-service
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 30 Aug 2017 11:48:35 +0200 |
parents | 50cde8246542 |
children | cf0eb76c5e81 |
files | CMakeLists.txt Resources/Samples/OrthancFramework/MicroService/CMakeLists.txt Resources/Samples/OrthancFramework/MicroService/README.txt Resources/Samples/OrthancFramework/MicroService/Sample.cpp |
diffstat | 4 files changed, 79 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Wed Aug 30 11:26:08 2017 +0200 +++ b/CMakeLists.txt Wed Aug 30 11:48:35 2017 +0200 @@ -250,7 +250,7 @@ add_definitions(-DORTHANC_USE_PRECOMPILED_HEADERS=1) ADD_VISUAL_STUDIO_PRECOMPILED_HEADERS( - "PrecompiledHeaders.h" "Core/PrecompiledHeaders.cpp" ORTHANC_CORE_SOURCES) + "PrecompiledHeaders.h" "Core/PrecompiledHeaders.cpp" ORTHANC_CORE_SOURCES_INTERNAL) ADD_VISUAL_STUDIO_PRECOMPILED_HEADERS( "PrecompiledHeadersServer.h" "OrthancServer/PrecompiledHeadersServer.cpp" ORTHANC_SERVER_SOURCES)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Resources/Samples/OrthancFramework/MicroService/CMakeLists.txt Wed Aug 30 11:48:35 2017 +0200 @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 2.8) + +project(Sample) + +include(${CMAKE_SOURCE_DIR}/../../../CMake/OrthancFrameworkParameters.cmake) + +set(ENABLE_WEB_SERVER ON) + +include(${CMAKE_SOURCE_DIR}/../../../CMake/OrthancFrameworkConfiguration.cmake) + +add_executable(Sample + ${ORTHANC_CORE_SOURCES} + Sample.cpp + ) + +include_directories(${ORTHANC_ROOT}/Core)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Resources/Samples/OrthancFramework/MicroService/README.txt Wed Aug 30 11:48:35 2017 +0200 @@ -0,0 +1,2 @@ +This file shows how to create a simple Web service in C++ (similar to +Python's Flask) using the Orthanc standalone framework.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Resources/Samples/OrthancFramework/MicroService/Sample.cpp Wed Aug 30 11:48:35 2017 +0200 @@ -0,0 +1,60 @@ +#include <stdio.h> + +#include <HttpServer/MongooseServer.h> +#include <Logging.h> +#include <RestApi/RestApi.h> +#include <SystemToolbox.h> + +class MicroService : public Orthanc::RestApi +{ +private: + static MicroService& GetSelf(Orthanc::RestApiCall& call) + { + return dynamic_cast<MicroService&>(call.GetContext()); + } + + void SayHello() + { + printf("Hello\n"); + } + + static void Hello(Orthanc::RestApiGetCall& call) + { + GetSelf(call).SayHello(); + + Json::Value value = Json::arrayValue; + value.append("World"); + + call.GetOutput().AnswerJson(value); + } + +public: + MicroService() + { + Register("/hello", Hello); + } +}; + +int main() +{ + Orthanc::Logging::Initialize(); + Orthanc::Logging::EnableTraceLevel(true); + + MicroService rest; + + { + Orthanc::MongooseServer httpServer; + httpServer.SetPortNumber(8000); + httpServer.Register(rest); + httpServer.Start(); + + LOG(WARNING) << "Micro-service started on port " << httpServer.GetPortNumber(); + Orthanc::SystemToolbox::ServerBarrier(); + } + + LOG(WARNING) << "Micro-service stopped"; + + Orthanc::Logging::Finalize(); + + return 0; +}