annotate OrthancFramework/Resources/Samples/MicroService/Sample.cpp @ 4386:3ea753570c6d

cppcheck
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 18 Dec 2020 12:15:45 +0100
parents bf7b9edf6b81
children d9473bd5ed43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4106
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
1 /**
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
4 * Department, University Hospital of Liege, Belgium
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
6 *
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
7 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4106
diff changeset
8 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4106
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4106
diff changeset
10 * the License, or (at your option) any later version.
4106
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
11 *
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
12 * This program is distributed in the hope that it will be useful, but
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4106
diff changeset
15 * Lesser General Public License for more details.
4106
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
16 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4106
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4106
diff changeset
18 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4106
diff changeset
19 * <http://www.gnu.org/licenses/>.
4106
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
20 **/
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
21
50cb0fb99e34 added license headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4049
diff changeset
22
2389
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include <stdio.h>
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
4049
47e9e788224c removing occurrences of ORTHANC_ROOT
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
25 #include "../../../Sources/HttpServer/HttpServer.h"
47e9e788224c removing occurrences of ORTHANC_ROOT
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
26 #include "../../../Sources/Logging.h"
47e9e788224c removing occurrences of ORTHANC_ROOT
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
27 #include "../../../Sources/RestApi/RestApi.h"
47e9e788224c removing occurrences of ORTHANC_ROOT
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
28 #include "../../../Sources/SystemToolbox.h"
2389
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 class MicroService : public Orthanc::RestApi
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 {
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 private:
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 static MicroService& GetSelf(Orthanc::RestApiCall& call)
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 return dynamic_cast<MicroService&>(call.GetContext());
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 void SayHello()
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 printf("Hello\n");
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 }
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 static void Hello(Orthanc::RestApiGetCall& call)
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 GetSelf(call).SayHello();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 Json::Value value = Json::arrayValue;
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 value.append("World");
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 call.GetOutput().AnswerJson(value);
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
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 public:
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 MicroService()
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 Register("/hello", Hello);
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 };
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 int main()
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 {
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 Orthanc::Logging::Initialize();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 Orthanc::Logging::EnableTraceLevel(true);
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 MicroService rest;
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 {
4049
47e9e788224c removing occurrences of ORTHANC_ROOT
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
68 Orthanc::HttpServer httpServer;
2389
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 httpServer.SetPortNumber(8000);
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 httpServer.Register(rest);
2400
f235cc740c4b remote access allowed in sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2389
diff changeset
71 httpServer.SetRemoteAccessAllowed(true);
2389
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 httpServer.Start();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 LOG(WARNING) << "Micro-service started on port " << httpServer.GetPortNumber();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 Orthanc::SystemToolbox::ServerBarrier();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 }
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 LOG(WARNING) << "Micro-service stopped";
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 Orthanc::Logging::Finalize();
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 return 0;
88402bd5dbf5 sample of a micro-service
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 }