comparison Resources/Samples/RestApiLinuxDynamic/Sample.cpp @ 329:f579d50fdf8f

rename
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 08 Jan 2013 14:56:10 +0100
parents Resources/Samples/RestApiStandalone/Sample.cpp@25514c48e30e
children 5962898fb7c8
comparison
equal deleted inserted replaced
328:25514c48e30e 329:f579d50fdf8f
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 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
60 // Define the callbacks of the REST API using C++11 lambda functions
61 std::auto_ptr<Orthanc::RestApi> rest(new Orthanc::RestApi);
62
63 rest->Register("/", [] (Orthanc::RestApi::GetCall& call) {
64 std::string answer = "Hello world\n";
65 answer += "Glad to meet you, Mr. " + call.GetArgument("name", "Nobody") + "\n";
66 call.GetOutput().AnswerBuffer(answer, "text/plain");
67 });
68
69 rest->Register("/", [] (Orthanc::RestApi::DeleteCall& call) {
70 call.GetOutput().AnswerBuffer("Hey, you have just deleted the server!\n",
71 "text/plain");
72 });
73
74 rest->Register("/", [] (Orthanc::RestApi::PostCall& call) {
75 call.GetOutput().AnswerBuffer("I have received a POST with body: [" +
76 call.GetPostBody() + "]\n", "text/plain");
77 });
78
79 rest->Register("/", [] (Orthanc::RestApi::PutCall& call) {
80 call.GetOutput().AnswerBuffer("I have received a PUT with body: [" +
81 call.GetPutBody() + "]\n", "text/plain");
82 });
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 }