Mercurial > hg > orthanc
comparison Resources/Samples/OrthancFramework/MicroService/Sample.cpp @ 2389:88402bd5dbf5
sample of a micro-service
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 30 Aug 2017 11:48:35 +0200 |
parents | |
children | f235cc740c4b |
comparison
equal
deleted
inserted
replaced
2388:50cde8246542 | 2389:88402bd5dbf5 |
---|---|
1 #include <stdio.h> | |
2 | |
3 #include <HttpServer/MongooseServer.h> | |
4 #include <Logging.h> | |
5 #include <RestApi/RestApi.h> | |
6 #include <SystemToolbox.h> | |
7 | |
8 class MicroService : public Orthanc::RestApi | |
9 { | |
10 private: | |
11 static MicroService& GetSelf(Orthanc::RestApiCall& call) | |
12 { | |
13 return dynamic_cast<MicroService&>(call.GetContext()); | |
14 } | |
15 | |
16 void SayHello() | |
17 { | |
18 printf("Hello\n"); | |
19 } | |
20 | |
21 static void Hello(Orthanc::RestApiGetCall& call) | |
22 { | |
23 GetSelf(call).SayHello(); | |
24 | |
25 Json::Value value = Json::arrayValue; | |
26 value.append("World"); | |
27 | |
28 call.GetOutput().AnswerJson(value); | |
29 } | |
30 | |
31 public: | |
32 MicroService() | |
33 { | |
34 Register("/hello", Hello); | |
35 } | |
36 }; | |
37 | |
38 int main() | |
39 { | |
40 Orthanc::Logging::Initialize(); | |
41 Orthanc::Logging::EnableTraceLevel(true); | |
42 | |
43 MicroService rest; | |
44 | |
45 { | |
46 Orthanc::MongooseServer httpServer; | |
47 httpServer.SetPortNumber(8000); | |
48 httpServer.Register(rest); | |
49 httpServer.Start(); | |
50 | |
51 LOG(WARNING) << "Micro-service started on port " << httpServer.GetPortNumber(); | |
52 Orthanc::SystemToolbox::ServerBarrier(); | |
53 } | |
54 | |
55 LOG(WARNING) << "Micro-service stopped"; | |
56 | |
57 Orthanc::Logging::Finalize(); | |
58 | |
59 return 0; | |
60 } |