changeset 276:8af8754a7a8e

rest api demo
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sun, 09 Dec 2012 14:45:10 +0100
parents c8123aa17e01
children 58f969933720
files Resources/Samples/RestApi/CMakeLists.txt Resources/Samples/RestApi/Sample.cpp
diffstat 2 files changed, 152 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/RestApi/CMakeLists.txt	Sun Dec 09 14:45:10 2012 +0100
@@ -0,0 +1,47 @@
+cmake_minimum_required(VERSION 2.8)
+
+project(RestApiSample)
+
+include(ExternalProject)
+
+ExternalProject_Add(
+  ORTHANC_CORE
+  PREFIX ${CMAKE_BINARY_DIR}/Orthanc/
+  DOWNLOAD_COMMAND hg clone https://code.google.com/p/orthanc/ -r Orthanc-0.3.1
+  UPDATE_COMMAND ""
+  SOURCE_DIR ${CMAKE_BINARY_DIR}/Orthanc/src/orthanc/
+
+  # Optional step, to reuse the third-party downloads
+  PATCH_COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_SOURCE_DIR}/../../../ThirdPartyDownloads ThirdPartyDownloads
+
+  CMAKE_COMMAND ${CMAKE_COMMAND}
+  CMAKE_ARGS -DSTATIC_BUILD=ON -DSTANDALONE_BUILD=ON -DUSE_DYNAMIC_GOOGLE_LOG=OFF -DUSE_DYNAMIC_SQLITE=OFF -DONLY_CORE_LIBRARY=ON -DENABLE_SSL=OFF
+  BUILD_COMMAND $(MAKE)
+  INSTALL_COMMAND ""
+  BUILD_IN_SOURCE 0
+)
+
+ExternalProject_Get_Property(ORTHANC_CORE source_dir)
+include_directories(${source_dir})
+
+ExternalProject_Get_Property(ORTHANC_CORE binary_dir)
+link_directories(${binary_dir})
+include_directories(${binary_dir}/jsoncpp-src-0.5.0/include)
+include_directories(${binary_dir}/glog-0.3.2/src)
+
+add_executable(RestApiSample
+  Sample.cpp
+  )
+
+add_dependencies(RestApiSample ORTHANC_CORE)
+
+target_link_libraries(RestApiSample 
+  # From Orthanc
+  CoreLibrary
+  GoogleLog
+  #OpenSSL
+
+  # System-wide libraries
+  pthread 
+  )
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/Samples/RestApi/Sample.cpp	Sun Dec 09 14:45:10 2012 +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;
+}