comparison Resources/Samples/RestApiStandalone/Sample.cpp @ 328:25514c48e30e

demonstration of C++11 lambda functions
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 08 Jan 2013 14:55:55 +0100
parents 7233461e2f61
children
comparison
equal deleted inserted replaced
327:4564e908bba9 328:25514c48e30e
47 * # curl http://localhost:8042 -X DELETE 47 * # curl http://localhost:8042 -X DELETE
48 * # curl http://localhost:8042 -X PUT -d "PutBody" 48 * # curl http://localhost:8042 -X PUT -d "PutBody"
49 * # curl http://localhost:8042 -X POST -d "PostBody" 49 * # curl http://localhost:8042 -X POST -d "PostBody"
50 **/ 50 **/
51 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() 52 int main()
78 { 53 {
79 // Initialize the logging mechanism 54 // Initialize the logging mechanism
80 google::InitGoogleLogging("Orthanc"); 55 google::InitGoogleLogging("Orthanc");
81 FLAGS_logtostderr = true; 56 FLAGS_logtostderr = true;
82 FLAGS_minloglevel = 0; // Use the verbose mode 57 FLAGS_minloglevel = 0; // Use the verbose mode
83 FLAGS_v = 0; 58 FLAGS_v = 0;
84 59
85 // Define the callbacks of the REST API 60 // Define the callbacks of the REST API using C++11 lambda functions
86 std::auto_ptr<Orthanc::RestApi> rest(new Orthanc::RestApi); 61 std::auto_ptr<Orthanc::RestApi> rest(new Orthanc::RestApi);
87 rest->Register("/", GetRoot); 62
88 rest->Register("/", PostRoot); 63 rest->Register("/", [] (Orthanc::RestApi::GetCall& call) {
89 rest->Register("/", PutRoot); 64 std::string answer = "Hello world\n";
90 rest->Register("/", DeleteRoot); 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 });
91 83
92 // Setup the embedded HTTP server 84 // Setup the embedded HTTP server
93 Orthanc::MongooseServer httpServer; 85 Orthanc::MongooseServer httpServer;
94 httpServer.SetPortNumber(8042); // Use TCP port 8042 86 httpServer.SetPortNumber(8042); // Use TCP port 8042
95 httpServer.SetRemoteAccessAllowed(true); // Do not block remote requests 87 httpServer.SetRemoteAccessAllowed(true); // Do not block remote requests