Mercurial > hg > orthanc
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 |
rev | line source |
---|---|
276 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
401 | 3 * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege, |
276 | 4 * Belgium |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include <Core/HttpServer/MongooseServer.h> | |
34 #include <Core/RestApi/RestApi.h> | |
35 #include <Core/Toolbox.h> | |
36 #include <glog/logging.h> | |
37 #include <stdio.h> | |
38 | |
39 | |
40 /** | |
41 * This is a demo program that shows how to setup a REST server with | |
42 * the Orthanc Core API. Once the server is running, here are some | |
43 * sample command lines to interact with it: | |
44 * | |
45 * # curl http://localhost:8042 | |
46 * # curl 'http://localhost:8042?name=Hide' | |
47 * # curl http://localhost:8042 -X DELETE | |
48 * # curl http://localhost:8042 -X PUT -d "PutBody" | |
49 * # curl http://localhost:8042 -X POST -d "PostBody" | |
50 **/ | |
51 | |
52 int main() | |
53 { | |
54 // Initialize the logging mechanism | |
55 google::InitGoogleLogging("Orthanc"); | |
56 FLAGS_logtostderr = true; | |
57 FLAGS_minloglevel = 0; // Use the verbose mode | |
58 FLAGS_v = 0; | |
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 | 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 | 83 |
84 // Setup the embedded HTTP server | |
85 Orthanc::MongooseServer httpServer; | |
86 httpServer.SetPortNumber(8042); // Use TCP port 8042 | |
87 httpServer.SetRemoteAccessAllowed(true); // Do not block remote requests | |
88 httpServer.RegisterHandler(rest.release()); // The REST API is the handler | |
89 | |
90 // Start the server and wait for the user to hit "Ctrl-C" | |
91 httpServer.Start(); | |
92 LOG(WARNING) << "REST server has started"; | |
93 Orthanc::Toolbox::ServerBarrier(); | |
94 LOG(WARNING) << "REST server has stopped"; | |
95 | |
96 return 0; | |
97 } |