Mercurial > hg > orthanc
comparison Resources/Samples/RestApiStandalone/Sample.cpp @ 322:7233461e2f61
new sample
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 07 Jan 2013 15:33:53 +0100 |
parents | Resources/Samples/RestApi/Sample.cpp@8af8754a7a8e |
children | 25514c48e30e |
comparison
equal
deleted
inserted
replaced
321:47a709e22d1f | 322:7233461e2f61 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, | |
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 static void GetRoot(Orthanc::RestApi::GetCall& call) | |
53 { | |
54 std::string answer = "Hello world\n"; | |
55 answer += "Glad to meet you, Mr. " + call.GetArgument("name", "Nobody") + "\n"; | |
56 call.GetOutput().AnswerBuffer(answer, "text/plain"); | |
57 } | |
58 | |
59 static void DeleteRoot(Orthanc::RestApi::DeleteCall& call) | |
60 { | |
61 call.GetOutput().AnswerBuffer("Hey, you have just deleted the server!\n", | |
62 "text/plain"); | |
63 } | |
64 | |
65 static void PostRoot(Orthanc::RestApi::PostCall& call) | |
66 { | |
67 call.GetOutput().AnswerBuffer("I have received a POST with body: [" + | |
68 call.GetPostBody() + "]\n", "text/plain"); | |
69 } | |
70 | |
71 static void PutRoot(Orthanc::RestApi::PutCall& call) | |
72 { | |
73 call.GetOutput().AnswerBuffer("I have received a PUT with body: [" + | |
74 call.GetPutBody() + "]\n", "text/plain"); | |
75 } | |
76 | |
77 int main() | |
78 { | |
79 // Initialize the logging mechanism | |
80 google::InitGoogleLogging("Orthanc"); | |
81 FLAGS_logtostderr = true; | |
82 FLAGS_minloglevel = 0; // Use the verbose mode | |
83 FLAGS_v = 0; | |
84 | |
85 // Define the callbacks of the REST API | |
86 std::auto_ptr<Orthanc::RestApi> rest(new Orthanc::RestApi); | |
87 rest->Register("/", GetRoot); | |
88 rest->Register("/", PostRoot); | |
89 rest->Register("/", PutRoot); | |
90 rest->Register("/", DeleteRoot); | |
91 | |
92 // Setup the embedded HTTP server | |
93 Orthanc::MongooseServer httpServer; | |
94 httpServer.SetPortNumber(8042); // Use TCP port 8042 | |
95 httpServer.SetRemoteAccessAllowed(true); // Do not block remote requests | |
96 httpServer.RegisterHandler(rest.release()); // The REST API is the handler | |
97 | |
98 // Start the server and wait for the user to hit "Ctrl-C" | |
99 httpServer.Start(); | |
100 LOG(WARNING) << "REST server has started"; | |
101 Orthanc::Toolbox::ServerBarrier(); | |
102 LOG(WARNING) << "REST server has stopped"; | |
103 | |
104 return 0; | |
105 } |