Mercurial > hg > orthanc
annotate OrthancServer/main.cpp @ 232:5368bbe813cf
refactoring of attachments
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 30 Nov 2012 14:22:27 +0100 |
parents | f2349bab5fe7 |
children | 6d9be2b470b4 |
rev | line source |
---|---|
0 | 1 /** |
62 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
0 | 3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
136 | 10 * |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
0 | 22 * |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
229 | 33 #include "OrthancRestApi.h" |
0 | 34 |
175
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
35 #include <fstream> |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
36 #include <glog/logging.h> |
112 | 37 #include <boost/algorithm/string/predicate.hpp> |
0 | 38 |
39 #include "../Core/HttpServer/EmbeddedResourceHttpHandler.h" | |
40 #include "../Core/HttpServer/FilesystemHttpHandler.h" | |
41 #include "../Core/HttpServer/MongooseServer.h" | |
42 #include "DicomProtocol/DicomServer.h" | |
62 | 43 #include "OrthancInitialization.h" |
224
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
44 #include "ServerContext.h" |
0 | 45 |
62 | 46 using namespace Orthanc; |
0 | 47 |
48 | |
224
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
49 |
0 | 50 class MyDicomStore : public IStoreRequestHandler |
51 { | |
52 private: | |
224
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
53 ServerContext& context_; |
0 | 54 |
55 public: | |
224
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
56 MyDicomStore(ServerContext& context) : |
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
57 context_(context) |
0 | 58 { |
59 } | |
60 | |
61 virtual void Handle(const std::vector<uint8_t>& dicomFile, | |
62 const DicomMap& dicomSummary, | |
63 const Json::Value& dicomJson, | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
27
diff
changeset
|
64 const std::string& remoteAet) |
0 | 65 { |
66 if (dicomFile.size() > 0) | |
67 { | |
224
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
68 context_.Store(reinterpret_cast<const char*>(&dicomFile[0]), dicomFile.size(), |
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
69 dicomSummary, dicomJson, remoteAet); |
0 | 70 } |
71 } | |
72 }; | |
73 | |
74 | |
75 class MyDicomStoreFactory : public IStoreRequestHandlerFactory | |
76 { | |
77 private: | |
224
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
78 ServerContext& context_; |
0 | 79 |
80 public: | |
224
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
81 MyDicomStoreFactory(ServerContext& context) : context_(context) |
0 | 82 { |
83 } | |
84 | |
85 virtual IStoreRequestHandler* ConstructStoreRequestHandler() | |
86 { | |
224
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
87 return new MyDicomStore(context_); |
0 | 88 } |
89 | |
90 void Done() | |
91 { | |
92 //index_.db().Execute("DELETE FROM Studies"); | |
93 } | |
94 }; | |
95 | |
96 | |
133 | 97 void PrintHelp(char* path) |
98 { | |
99 std::cout | |
100 << "Usage: " << path << " [OPTION]... [CONFIGURATION]" << std::endl | |
175
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
101 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." << std::endl |
133 | 102 << std::endl |
175
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
103 << "If no configuration file is given on the command line, a set of default " << std::endl |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
104 << "parameters is used. Please refer to the Orthanc homepage for the full " << std::endl |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
105 << "instructions about how to use Orthanc " << std::endl |
133 | 106 << "<https://code.google.com/p/orthanc/wiki/OrthancCookbook>." << std::endl |
107 << std::endl | |
108 << "Command-line options:" << std::endl | |
109 << " --help\t\tdisplay this help and exit" << std::endl | |
110 << " --logdir=[dir]\tdirectory where to store the log files" << std::endl | |
111 << "\t\t\t(if not used, the logs are dumped to stderr)" << std::endl | |
175
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
112 << " --config=[file]\tcreate a sample configuration file and exit" << std::endl |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
113 << " --trace\t\thighest verbosity in logs (for debug)" << std::endl |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
114 << " --verbose\t\tbe verbose in logs" << std::endl |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
115 << " --version\t\toutput version information and exit" << std::endl |
133 | 116 << std::endl |
117 << "Exit status:" << std::endl | |
118 << " 0 if OK," << std::endl | |
119 << " -1 if error (have a look at the logs)." << std::endl | |
120 << std::endl; | |
121 } | |
0 | 122 |
123 | |
133 | 124 void PrintVersion(char* path) |
125 { | |
126 std::cout | |
127 << path << " " << ORTHANC_VERSION << std::endl | |
128 << "Copyright (C) 2012 Medical Physics Department, CHU of Liege (Belgium) " << std::endl | |
129 << "Licensing GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>, with OpenSSL exception." << std::endl | |
130 << "This is free software: you are free to change and redistribute it." << std::endl | |
131 << "There is NO WARRANTY, to the extent permitted by law." << std::endl | |
132 << std::endl | |
133 << "Written by Sebastien Jodogne <s.jodogne@gmail.com>" << std::endl; | |
134 } | |
135 | |
0 | 136 |
137 int main(int argc, char* argv[]) | |
138 { | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
139 // Initialize Google's logging library. |
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
140 FLAGS_logtostderr = true; |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
141 FLAGS_minloglevel = 1; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
142 FLAGS_v = 0; |
133 | 143 |
112 | 144 for (int i = 1; i < argc; i++) |
145 { | |
133 | 146 if (std::string(argv[i]) == "--help") |
147 { | |
148 PrintHelp(argv[0]); | |
149 return 0; | |
150 } | |
151 | |
152 if (std::string(argv[i]) == "--version") | |
153 { | |
154 PrintVersion(argv[0]); | |
155 return 0; | |
156 } | |
157 | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
158 if (std::string(argv[i]) == "--verbose") |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
159 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
160 FLAGS_minloglevel = 0; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
161 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
162 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
163 if (std::string(argv[i]) == "--trace") |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
164 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
165 FLAGS_minloglevel = 0; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
166 FLAGS_v = 1; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
167 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
168 |
112 | 169 if (boost::starts_with(argv[i], "--logdir=")) |
170 { | |
171 FLAGS_logtostderr = false; | |
172 FLAGS_log_dir = std::string(argv[i]).substr(9); | |
173 } | |
175
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
174 |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
175 if (boost::starts_with(argv[i], "--config=")) |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
176 { |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
177 std::string configurationSample; |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
178 GetFileResource(configurationSample, EmbeddedResources::CONFIGURATION_SAMPLE); |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
179 |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
180 std::string target = std::string(argv[i]).substr(9); |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
181 std::ofstream f(target.c_str()); |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
182 f << configurationSample; |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
183 f.close(); |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
184 return 0; |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
185 } |
112 | 186 } |
187 | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
188 google::InitGoogleLogging("Orthanc"); |
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
189 |
112 | 190 |
0 | 191 try |
192 { | |
112 | 193 bool isInitialized = false; |
26 | 194 if (argc >= 2) |
195 { | |
112 | 196 for (int i = 1; i < argc; i++) |
197 { | |
198 // Use the first argument that does not start with a "-" as | |
199 // the configuration file | |
200 if (argv[i][0] != '-') | |
201 { | |
202 OrthancInitialize(argv[i]); | |
203 isInitialized = true; | |
204 } | |
205 } | |
26 | 206 } |
112 | 207 |
208 if (!isInitialized) | |
26 | 209 { |
62 | 210 OrthancInitialize(); |
26 | 211 } |
212 | |
201
bee20e978835
refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
213 boost::filesystem::path storageDirectory = GetGlobalStringParameter("StorageDirectory", "OrthancStorage"); |
224
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
214 ServerContext context(storageDirectory); |
4eb0c7ce86c9
refactoring for store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
210
diff
changeset
|
215 MyDicomStoreFactory storeScp(context); |
0 | 216 |
217 { | |
218 // DICOM server | |
219 DicomServer dicomServer; | |
55 | 220 dicomServer.SetCalledApplicationEntityTitleCheck(GetGlobalBoolParameter("DicomCheckCalledAet", false)); |
0 | 221 dicomServer.SetStoreRequestHandlerFactory(storeScp); |
128 | 222 dicomServer.SetPortNumber(GetGlobalIntegerParameter("DicomPort", 4242)); |
62 | 223 dicomServer.SetApplicationEntityTitle(GetGlobalStringParameter("DicomAet", "ORTHANC")); |
0 | 224 |
225 // HTTP server | |
226 MongooseServer httpServer; | |
158 | 227 httpServer.SetPortNumber(GetGlobalIntegerParameter("HttpPort", 8042)); |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
27
diff
changeset
|
228 httpServer.SetRemoteAccessAllowed(GetGlobalBoolParameter("RemoteAccessAllowed", false)); |
0 | 229 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
230 httpServer.SetAuthenticationEnabled(GetGlobalBoolParameter("AuthenticationEnabled", false)); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
231 SetupRegisteredUsers(httpServer); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
232 |
23 | 233 if (GetGlobalBoolParameter("SslEnabled", false)) |
234 { | |
235 std::string certificate = GetGlobalStringParameter("SslCertificate", "certificate.pem"); | |
236 httpServer.SetSslEnabled(true); | |
237 httpServer.SetSslCertificate(certificate.c_str()); | |
238 } | |
239 else | |
240 { | |
241 httpServer.SetSslEnabled(false); | |
242 } | |
243 | |
178 | 244 LOG(WARNING) << "DICOM server listening on port: " << dicomServer.GetPortNumber(); |
245 LOG(WARNING) << "HTTP server listening on port: " << httpServer.GetPortNumber(); | |
125 | 246 |
62 | 247 #if ORTHANC_STANDALONE == 1 |
248 httpServer.RegisterHandler(new EmbeddedResourceHttpHandler("/app", EmbeddedResources::ORTHANC_EXPLORER)); | |
0 | 249 #else |
62 | 250 httpServer.RegisterHandler(new FilesystemHttpHandler("/app", ORTHANC_PATH "/OrthancExplorer")); |
0 | 251 #endif |
252 | |
229 | 253 httpServer.RegisterHandler(new OrthancRestApi(context)); |
0 | 254 |
255 // GO !!! | |
256 httpServer.Start(); | |
257 dicomServer.Start(); | |
258 | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
259 LOG(WARNING) << "Orthanc has started"; |
0 | 260 Toolbox::ServerBarrier(); |
261 | |
262 // Stop | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
263 LOG(WARNING) << "Orthanc is stopping"; |
0 | 264 } |
265 | |
266 storeScp.Done(); | |
267 } | |
62 | 268 catch (OrthancException& e) |
0 | 269 { |
108 | 270 LOG(ERROR) << "EXCEPTION [" << e.What() << "]"; |
133 | 271 return -1; |
0 | 272 } |
273 | |
62 | 274 OrthancFinalize(); |
27 | 275 |
0 | 276 return 0; |
277 } |