annotate Resources/Samples/OrthancFramework/MicroService/Sample.cpp @ 3757:a593c20bf79b Orthanc-1.5.4

closing branch Orthanc-1.5.4
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 16 Mar 2020 12:16:52 +0100
parents f235cc740c4b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2389
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #include <stdio.h>
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 #include <HttpServer/MongooseServer.h>
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 #include <Logging.h>
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 #include <RestApi/RestApi.h>
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 #include <SystemToolbox.h>
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 class MicroService : public Orthanc::RestApi
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 {
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 private:
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 static MicroService& GetSelf(Orthanc::RestApiCall& call)
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 {
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 return dynamic_cast<MicroService&>(call.GetContext());
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 }
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 void SayHello()
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 {
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 printf("Hello\n");
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 }
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 static void Hello(Orthanc::RestApiGetCall& call)
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 {
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 GetSelf(call).SayHello();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 Json::Value value = Json::arrayValue;
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 value.append("World");
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 call.GetOutput().AnswerJson(value);
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 }
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 public:
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 MicroService()
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 Register("/hello", Hello);
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 }
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 };
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 int main()
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 {
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 Orthanc::Logging::Initialize();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 Orthanc::Logging::EnableTraceLevel(true);
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 MicroService rest;
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 Orthanc::MongooseServer httpServer;
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 httpServer.SetPortNumber(8000);
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 httpServer.Register(rest);
2400
f235cc740c4b remote access allowed in sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2389
diff changeset
49 httpServer.SetRemoteAccessAllowed(true);
2389
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 httpServer.Start();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 LOG(WARNING) << "Micro-service started on port " << httpServer.GetPortNumber();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 Orthanc::SystemToolbox::ServerBarrier();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 }
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 LOG(WARNING) << "Micro-service stopped";
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 Orthanc::Logging::Finalize();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 return 0;
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }