annotate OrthancFramework/Resources/Samples/MicroService/Sample.cpp @ 4090:160ec8417874 framework

integration mainline->framework
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Jun 2020 19:33:22 +0200
parents 47e9e788224c
children 50cb0fb99e34
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
4049
47e9e788224c removing occurrences of ORTHANC_ROOT
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
3 #include "../../../Sources/HttpServer/HttpServer.h"
47e9e788224c removing occurrences of ORTHANC_ROOT
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
4 #include "../../../Sources/Logging.h"
47e9e788224c removing occurrences of ORTHANC_ROOT
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
5 #include "../../../Sources/RestApi/RestApi.h"
47e9e788224c removing occurrences of ORTHANC_ROOT
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
6 #include "../../../Sources/SystemToolbox.h"
2389
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 {
4049
47e9e788224c removing occurrences of ORTHANC_ROOT
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
46 Orthanc::HttpServer httpServer;
2389
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 }