annotate Resources/Samples/RestApiLinuxDynamic/Sample.cpp @ 407:2d269089078f

reintegration of lua scripting into mainline
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 02 May 2013 16:49:28 +0200
parents 5962898fb7c8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
276
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
401
5962898fb7c8 copyright update
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 329
diff changeset
3 * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege,
276
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include <Core/HttpServer/MongooseServer.h>
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include <Core/RestApi/RestApi.h>
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include <Core/Toolbox.h>
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include <glog/logging.h>
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include <stdio.h>
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 /**
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 * This is a demo program that shows how to setup a REST server with
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 * the Orthanc Core API. Once the server is running, here are some
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 * sample command lines to interact with it:
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 *
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 * # curl http://localhost:8042
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 * # curl 'http://localhost:8042?name=Hide'
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 * # curl http://localhost:8042 -X DELETE
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 * # curl http://localhost:8042 -X PUT -d "PutBody"
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 * # curl http://localhost:8042 -X POST -d "PostBody"
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 **/
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 int main()
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 // Initialize the logging mechanism
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 google::InitGoogleLogging("Orthanc");
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 FLAGS_logtostderr = true;
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 FLAGS_minloglevel = 0; // Use the verbose mode
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 FLAGS_v = 0;
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
328
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
60 // Define the callbacks of the REST API using C++11 lambda functions
276
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 std::auto_ptr<Orthanc::RestApi> rest(new Orthanc::RestApi);
328
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
62
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
63 rest->Register("/", [] (Orthanc::RestApi::GetCall& call) {
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
64 std::string answer = "Hello world\n";
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
65 answer += "Glad to meet you, Mr. " + call.GetArgument("name", "Nobody") + "\n";
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
66 call.GetOutput().AnswerBuffer(answer, "text/plain");
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
67 });
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
68
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
69 rest->Register("/", [] (Orthanc::RestApi::DeleteCall& call) {
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
70 call.GetOutput().AnswerBuffer("Hey, you have just deleted the server!\n",
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
71 "text/plain");
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
72 });
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
73
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
74 rest->Register("/", [] (Orthanc::RestApi::PostCall& call) {
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
75 call.GetOutput().AnswerBuffer("I have received a POST with body: [" +
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
76 call.GetPostBody() + "]\n", "text/plain");
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
77 });
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
78
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
79 rest->Register("/", [] (Orthanc::RestApi::PutCall& call) {
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
80 call.GetOutput().AnswerBuffer("I have received a PUT with body: [" +
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
81 call.GetPutBody() + "]\n", "text/plain");
25514c48e30e demonstration of C++11 lambda functions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 322
diff changeset
82 });
276
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 // Setup the embedded HTTP server
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 Orthanc::MongooseServer httpServer;
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 httpServer.SetPortNumber(8042); // Use TCP port 8042
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 httpServer.SetRemoteAccessAllowed(true); // Do not block remote requests
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 httpServer.RegisterHandler(rest.release()); // The REST API is the handler
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 // Start the server and wait for the user to hit "Ctrl-C"
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 httpServer.Start();
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 LOG(WARNING) << "REST server has started";
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 Orthanc::Toolbox::ServerBarrier();
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 LOG(WARNING) << "REST server has stopped";
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 return 0;
8af8754a7a8e rest api demo
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 }