changeset 322:7233461e2f61

new sample
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 07 Jan 2013 15:33:53 +0100
parents 47a709e22d1f
children 052dede32761
files Resources/Samples/RestApiStandalone/AutoGeneratedCode.cmake Resources/Samples/RestApiStandalone/CMakeLists.txt Resources/Samples/RestApiStandalone/README.txt Resources/Samples/RestApiStandalone/Sample.cpp
diffstat 4 files changed, 255 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/RestApiStandalone/AutoGeneratedCode.cmake	Mon Jan 07 15:33:53 2013 +0100
@@ -0,0 +1,41 @@
+set(AUTOGENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/AUTOGENERATED")
+set(AUTOGENERATED_SOURCES)
+
+file(MAKE_DIRECTORY ${AUTOGENERATED_DIR})
+include_directories(${AUTOGENERATED_DIR})
+
+macro(EmbedResources)
+  # Convert a semicolon separated list to a whitespace separated string
+  set(SCRIPT_ARGUMENTS)
+  set(DEPENDENCIES)
+  set(IS_PATH_NAME false)
+  foreach(arg ${ARGN})
+    if (${IS_PATH_NAME})
+      list(APPEND SCRIPT_ARGUMENTS "${arg}")
+      list(APPEND DEPENDENCIES "${arg}")
+      set(IS_PATH_NAME false)
+    else()
+      list(APPEND SCRIPT_ARGUMENTS "${arg}")
+      set(IS_PATH_NAME true)
+    endif()
+  endforeach()
+
+  set(TARGET_BASE "${AUTOGENERATED_DIR}/EmbeddedResources")
+  add_custom_command(
+    OUTPUT
+    "${TARGET_BASE}.h"
+    "${TARGET_BASE}.cpp"
+    COMMAND 
+    python
+    "${ORTHANC_DIR}/Resources/EmbedResources.py"
+    "${AUTOGENERATED_DIR}/EmbeddedResources"
+    ${SCRIPT_ARGUMENTS}
+    DEPENDS
+    "${ORTHANC_DIR}/Resources/EmbedResources.py"
+    ${DEPENDENCIES}
+    )
+
+  list(APPEND AUTOGENERATED_SOURCES
+    "${AUTOGENERATED_DIR}/EmbeddedResources.cpp"
+    ) 
+endmacro()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/RestApiStandalone/CMakeLists.txt	Mon Jan 07 15:33:53 2013 +0100
@@ -0,0 +1,98 @@
+cmake_minimum_required(VERSION 2.8)
+
+project(RestApiSample)
+
+file(DOWNLOAD 
+  http://mongoose.googlecode.com/files/mongoose-3.1.tgz 
+  ${CMAKE_BINARY_DIR}/mongoose-3.1.tar.gz
+  EXPECTED_MD5 "e718fc287b4eb1bd523be3fa00942bb0"
+  SHOW_PROGRESS
+  )
+
+file(DOWNLOAD 
+  http://downloads.sourceforge.net/project/jsoncpp/jsoncpp/0.5.0/jsoncpp-src-0.5.0.tar.gz
+  ${CMAKE_BINARY_DIR}/jsoncpp-src-0.5.0.tar.gz
+  EXPECTED_MD5 "24482b67c1cb17aac1ed1814288a3a8f"
+  SHOW_PROGRESS
+  )
+
+execute_process(
+  COMMAND hg clone -v -r Orthanc-0.4.0 https://code.google.com/p/orthanc/ Orthanc-0.4.0
+  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+  )
+
+execute_process(
+  COMMAND ${CMAKE_COMMAND} -E tar xvfz ${CMAKE_BINARY_DIR}/mongoose-3.1.tar.gz
+  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+  )
+
+execute_process(
+  COMMAND ${CMAKE_COMMAND} -E tar xvfz ${CMAKE_BINARY_DIR}/jsoncpp-src-0.5.0.tar.gz
+  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+  )
+
+include(AutoGeneratedCode.cmake)
+
+add_definitions(
+  -DBOOST_HAS_FILESYSTEM_V3=1
+  -DBOOST_HAS_SCHED_YIELD=1
+  -DORTHANC_SSL_ENABLED=1
+  -DORTHANC_STANDALONE=1
+  -DORTHANC_STATIC=0
+  )
+
+set(ORTHANC_DIR ${CMAKE_BINARY_DIR}/Orthanc-0.4.0)
+set(MONGOOSE_DIR ${CMAKE_BINARY_DIR}/mongoose)
+set(JSONCPP_DIR ${CMAKE_BINARY_DIR}/jsoncpp-src-0.5.0)
+
+include_directories(
+  ${ORTHANC_DIR}
+  ${MONGOOSE_DIR}
+  ${JSONCPP_DIR}/include
+  )
+
+link_libraries(
+  boost_date_time
+  boost_filesystem
+  boost_system
+  boost_thread
+  curl
+  dl
+  glog
+  png
+  pthread
+  sqlite3
+  uuid
+  z
+  )
+
+set(THIRD_PARTY_SOURCES
+  ${MONGOOSE_DIR}/mongoose.c
+  ${JSONCPP_DIR}/src/lib_json/json_reader.cpp
+  ${JSONCPP_DIR}/src/lib_json/json_value.cpp 
+  ${JSONCPP_DIR}/src/lib_json/json_writer.cpp
+  )
+
+file(GLOB ORTHANC_SOURCES 
+  ${ORTHANC_DIR}/Core/*.cpp
+  ${ORTHANC_DIR}/Core/*/*.cpp
+  ${ORTHANC_DIR}/OrthancCppClient/*.cpp
+  ${ORTHANC_DIR}/Resources/base64/base64.cpp
+  ${ORTHANC_DIR}/Resources/md5/md5.c
+  ${ORTHANC_DIR}/Resources/sha1/sha1.cpp
+  ${ORTHANC_DIR}/Resources/minizip/zip.c
+  ${ORTHANC_DIR}/Resources/minizip/ioapi.c
+  )
+
+list(REMOVE_ITEM ORTHANC_SOURCES ${ORTHANC_DIR}/OrthancCppClient/main.cpp)
+
+EmbedResources(
+  #ORTHANC_EXPLORER ${ORTHANC_DIR}/OrthancExplorer
+  )
+
+add_executable(RestApiSample
+  Sample.cpp
+  ${ORTHANC_SOURCES}
+  ${AUTOGENERATED_SOURCES}
+  ${THIRD_PARTY_SOURCES}
+  )
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/RestApiStandalone/README.txt	Mon Jan 07 15:33:53 2013 +0100
@@ -0,0 +1,11 @@
+This folder shows how it is possible to link against the "Core" and
+"OrthancCppClient" libraries from the Orthanc distribution. It is
+shown how a sample REST API can be created.
+
+This is the same sample than in folder "../RestApi", but with a
+different build script.
+
+The build script of this folder does not rely on the default CMake
+script from Orthanc. It dynamically links against the standard system
+libraries. This results in a simpler, standalone build
+script. However, it will only work on Linux-based systems.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/RestApiStandalone/Sample.cpp	Mon Jan 07 15:33:53 2013 +0100
@@ -0,0 +1,105 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include <Core/HttpServer/MongooseServer.h>
+#include <Core/RestApi/RestApi.h>
+#include <Core/Toolbox.h>
+#include <glog/logging.h>
+#include <stdio.h>
+
+
+/**
+ * This is a demo program that shows how to setup a REST server with
+ * the Orthanc Core API. Once the server is running, here are some 
+ * sample command lines to interact with it:
+ * 
+ *  # curl http://localhost:8042
+ *  # curl 'http://localhost:8042?name=Hide'
+ *  # curl http://localhost:8042 -X DELETE
+ *  # curl http://localhost:8042 -X PUT -d "PutBody"
+ *  # curl http://localhost:8042 -X POST -d "PostBody"
+ **/
+
+static void GetRoot(Orthanc::RestApi::GetCall& call)
+{
+  std::string answer = "Hello world\n";
+  answer += "Glad to meet you, Mr. " + call.GetArgument("name", "Nobody") + "\n";
+  call.GetOutput().AnswerBuffer(answer, "text/plain");
+}
+ 
+static void DeleteRoot(Orthanc::RestApi::DeleteCall& call)
+{
+  call.GetOutput().AnswerBuffer("Hey, you have just deleted the server!\n",
+                                "text/plain");
+}
+ 
+static void PostRoot(Orthanc::RestApi::PostCall& call)
+{
+  call.GetOutput().AnswerBuffer("I have received a POST with body: [" +
+                                call.GetPostBody() + "]\n", "text/plain");
+}
+ 
+static void PutRoot(Orthanc::RestApi::PutCall& call)
+{
+  call.GetOutput().AnswerBuffer("I have received a PUT with body: [" +
+                                call.GetPutBody() + "]\n", "text/plain");
+}
+ 
+int main()
+{
+  // Initialize the logging mechanism
+  google::InitGoogleLogging("Orthanc");
+  FLAGS_logtostderr = true;
+  FLAGS_minloglevel = 0;                      // Use the verbose mode
+  FLAGS_v = 0;
+  
+  // Define the callbacks of the REST API
+  std::auto_ptr<Orthanc::RestApi> rest(new Orthanc::RestApi);
+  rest->Register("/", GetRoot);
+  rest->Register("/", PostRoot);
+  rest->Register("/", PutRoot);
+  rest->Register("/", DeleteRoot);
+
+  // Setup the embedded HTTP server
+  Orthanc::MongooseServer httpServer;
+  httpServer.SetPortNumber(8042);             // Use TCP port 8042
+  httpServer.SetRemoteAccessAllowed(true);    // Do not block remote requests
+  httpServer.RegisterHandler(rest.release()); // The REST API is the handler
+
+  // Start the server and wait for the user to hit "Ctrl-C"
+  httpServer.Start();
+  LOG(WARNING) << "REST server has started";
+  Orthanc::Toolbox::ServerBarrier();
+  LOG(WARNING) << "REST server has stopped";
+
+  return 0;
+}