Mercurial > hg > orthanc
annotate OrthancServer/main.cpp @ 210:96b7918a6a18
start of the refactoring of the Orthanc REST API
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 28 Nov 2012 18:03:44 +0100 |
parents | bee20e978835 |
children | 4eb0c7ce86c9 |
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 | |
62 | 33 #include "OrthancRestApi.h" |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
34 #include "OrthancRestApi2.h" |
0 | 35 |
175
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
36 #include <fstream> |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
37 #include <glog/logging.h> |
112 | 38 #include <boost/algorithm/string/predicate.hpp> |
0 | 39 |
40 #include "../Core/HttpServer/EmbeddedResourceHttpHandler.h" | |
41 #include "../Core/HttpServer/FilesystemHttpHandler.h" | |
42 #include "../Core/HttpServer/MongooseServer.h" | |
43 #include "DicomProtocol/DicomServer.h" | |
62 | 44 #include "OrthancInitialization.h" |
0 | 45 |
46 | |
62 | 47 using namespace Orthanc; |
0 | 48 |
49 | |
50 class MyDicomStore : public IStoreRequestHandler | |
51 { | |
52 private: | |
53 ServerIndex& index_; | |
54 FileStorage storage_; | |
55 | |
56 public: | |
57 MyDicomStore(ServerIndex& index, | |
58 const std::string& path) : | |
59 index_(index), | |
60 storage_(path) | |
61 { | |
62 } | |
63 | |
64 virtual void Handle(const std::vector<uint8_t>& dicomFile, | |
65 const DicomMap& dicomSummary, | |
66 const Json::Value& dicomJson, | |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
27
diff
changeset
|
67 const std::string& remoteAet) |
0 | 68 { |
69 if (dicomFile.size() > 0) | |
70 { | |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
178
diff
changeset
|
71 index_.Store(storage_, |
0 | 72 reinterpret_cast<const char*>(&dicomFile[0]), dicomFile.size(), |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
27
diff
changeset
|
73 dicomSummary, dicomJson, remoteAet); |
0 | 74 } |
75 } | |
76 }; | |
77 | |
78 | |
79 class MyDicomStoreFactory : public IStoreRequestHandlerFactory | |
80 { | |
81 private: | |
82 ServerIndex& index_; | |
83 std::string path_; | |
84 | |
85 public: | |
86 MyDicomStoreFactory(ServerIndex& index, | |
87 const std::string& path) : | |
88 index_(index), | |
89 path_(path) | |
90 { | |
91 } | |
92 | |
93 virtual IStoreRequestHandler* ConstructStoreRequestHandler() | |
94 { | |
95 return new MyDicomStore(index_, path_); | |
96 } | |
97 | |
98 void Done() | |
99 { | |
100 //index_.db().Execute("DELETE FROM Studies"); | |
101 } | |
102 }; | |
103 | |
104 | |
133 | 105 void PrintHelp(char* path) |
106 { | |
107 std::cout | |
108 << "Usage: " << path << " [OPTION]... [CONFIGURATION]" << std::endl | |
175
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
109 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." << std::endl |
133 | 110 << std::endl |
175
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
111 << "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
|
112 << "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
|
113 << "instructions about how to use Orthanc " << std::endl |
133 | 114 << "<https://code.google.com/p/orthanc/wiki/OrthancCookbook>." << std::endl |
115 << std::endl | |
116 << "Command-line options:" << std::endl | |
117 << " --help\t\tdisplay this help and exit" << std::endl | |
118 << " --logdir=[dir]\tdirectory where to store the log files" << std::endl | |
119 << "\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
|
120 << " --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
|
121 << " --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
|
122 << " --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
|
123 << " --version\t\toutput version information and exit" << std::endl |
133 | 124 << std::endl |
125 << "Exit status:" << std::endl | |
126 << " 0 if OK," << std::endl | |
127 << " -1 if error (have a look at the logs)." << std::endl | |
128 << std::endl; | |
129 } | |
0 | 130 |
131 | |
133 | 132 void PrintVersion(char* path) |
133 { | |
134 std::cout | |
135 << path << " " << ORTHANC_VERSION << std::endl | |
136 << "Copyright (C) 2012 Medical Physics Department, CHU of Liege (Belgium) " << std::endl | |
137 << "Licensing GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>, with OpenSSL exception." << std::endl | |
138 << "This is free software: you are free to change and redistribute it." << std::endl | |
139 << "There is NO WARRANTY, to the extent permitted by law." << std::endl | |
140 << std::endl | |
141 << "Written by Sebastien Jodogne <s.jodogne@gmail.com>" << std::endl; | |
142 } | |
143 | |
0 | 144 |
145 int main(int argc, char* argv[]) | |
146 { | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
147 // Initialize Google's logging library. |
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
148 FLAGS_logtostderr = true; |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
149 FLAGS_minloglevel = 1; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
150 FLAGS_v = 0; |
133 | 151 |
112 | 152 for (int i = 1; i < argc; i++) |
153 { | |
133 | 154 if (std::string(argv[i]) == "--help") |
155 { | |
156 PrintHelp(argv[0]); | |
157 return 0; | |
158 } | |
159 | |
160 if (std::string(argv[i]) == "--version") | |
161 { | |
162 PrintVersion(argv[0]); | |
163 return 0; | |
164 } | |
165 | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
166 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
|
167 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
168 FLAGS_minloglevel = 0; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
169 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
170 |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
171 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
|
172 { |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
173 FLAGS_minloglevel = 0; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
174 FLAGS_v = 1; |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
175 } |
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
176 |
112 | 177 if (boost::starts_with(argv[i], "--logdir=")) |
178 { | |
179 FLAGS_logtostderr = false; | |
180 FLAGS_log_dir = std::string(argv[i]).substr(9); | |
181 } | |
175
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
182 |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
183 if (boost::starts_with(argv[i], "--config=")) |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
184 { |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
185 std::string configurationSample; |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
186 GetFileResource(configurationSample, EmbeddedResources::CONFIGURATION_SAMPLE); |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
187 |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
188 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
|
189 std::ofstream f(target.c_str()); |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
190 f << configurationSample; |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
191 f.close(); |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
192 return 0; |
662af781a227
sample config file from command line
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
193 } |
112 | 194 } |
195 | |
102
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
196 google::InitGoogleLogging("Orthanc"); |
7593b57dc1bf
switch to google log
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
62
diff
changeset
|
197 |
112 | 198 |
0 | 199 try |
200 { | |
112 | 201 bool isInitialized = false; |
26 | 202 if (argc >= 2) |
203 { | |
112 | 204 for (int i = 1; i < argc; i++) |
205 { | |
206 // Use the first argument that does not start with a "-" as | |
207 // the configuration file | |
208 if (argv[i][0] != '-') | |
209 { | |
210 OrthancInitialize(argv[i]); | |
211 isInitialized = true; | |
212 } | |
213 } | |
26 | 214 } |
112 | 215 |
216 if (!isInitialized) | |
26 | 217 { |
62 | 218 OrthancInitialize(); |
26 | 219 } |
220 | |
201
bee20e978835
refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
221 boost::filesystem::path storageDirectory = GetGlobalStringParameter("StorageDirectory", "OrthancStorage"); |
bee20e978835
refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
222 FileStorage storage(storageDirectory.string()); |
bee20e978835
refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
223 ServerIndex index(storage, storageDirectory.string()); |
bee20e978835
refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
224 MyDicomStoreFactory storeScp(index, storageDirectory.string()); |
0 | 225 |
226 { | |
227 // DICOM server | |
228 DicomServer dicomServer; | |
55 | 229 dicomServer.SetCalledApplicationEntityTitleCheck(GetGlobalBoolParameter("DicomCheckCalledAet", false)); |
0 | 230 dicomServer.SetStoreRequestHandlerFactory(storeScp); |
128 | 231 dicomServer.SetPortNumber(GetGlobalIntegerParameter("DicomPort", 4242)); |
62 | 232 dicomServer.SetApplicationEntityTitle(GetGlobalStringParameter("DicomAet", "ORTHANC")); |
0 | 233 |
234 // HTTP server | |
235 MongooseServer httpServer; | |
158 | 236 httpServer.SetPortNumber(GetGlobalIntegerParameter("HttpPort", 8042)); |
34
96e57b863dd9
option to disallow remote access
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
27
diff
changeset
|
237 httpServer.SetRemoteAccessAllowed(GetGlobalBoolParameter("RemoteAccessAllowed", false)); |
0 | 238 |
25
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
239 httpServer.SetAuthenticationEnabled(GetGlobalBoolParameter("AuthenticationEnabled", false)); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
240 SetupRegisteredUsers(httpServer); |
dd1489098265
basic http authentication
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
241 |
23 | 242 if (GetGlobalBoolParameter("SslEnabled", false)) |
243 { | |
244 std::string certificate = GetGlobalStringParameter("SslCertificate", "certificate.pem"); | |
245 httpServer.SetSslEnabled(true); | |
246 httpServer.SetSslCertificate(certificate.c_str()); | |
247 } | |
248 else | |
249 { | |
250 httpServer.SetSslEnabled(false); | |
251 } | |
252 | |
178 | 253 LOG(WARNING) << "DICOM server listening on port: " << dicomServer.GetPortNumber(); |
254 LOG(WARNING) << "HTTP server listening on port: " << httpServer.GetPortNumber(); | |
125 | 255 |
62 | 256 #if ORTHANC_STANDALONE == 1 |
257 httpServer.RegisterHandler(new EmbeddedResourceHttpHandler("/app", EmbeddedResources::ORTHANC_EXPLORER)); | |
0 | 258 #else |
62 | 259 httpServer.RegisterHandler(new FilesystemHttpHandler("/app", ORTHANC_PATH "/OrthancExplorer")); |
0 | 260 #endif |
261 | |
210
96b7918a6a18
start of the refactoring of the Orthanc REST API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
262 httpServer.RegisterHandler(new OrthancRestApi2(index, storageDirectory.string())); |
201
bee20e978835
refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
263 httpServer.RegisterHandler(new OrthancRestApi(index, storageDirectory.string())); |
0 | 264 |
265 // GO !!! | |
266 httpServer.Start(); | |
267 dicomServer.Start(); | |
268 | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
269 LOG(WARNING) << "Orthanc has started"; |
0 | 270 Toolbox::ServerBarrier(); |
271 | |
272 // Stop | |
137
0e97abc7b950
fix of a bug in older versions of sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
273 LOG(WARNING) << "Orthanc is stopping"; |
0 | 274 } |
275 | |
276 storeScp.Done(); | |
277 } | |
62 | 278 catch (OrthancException& e) |
0 | 279 { |
108 | 280 LOG(ERROR) << "EXCEPTION [" << e.What() << "]"; |
133 | 281 return -1; |
0 | 282 } |
283 | |
62 | 284 OrthancFinalize(); |
27 | 285 |
0 | 286 return 0; |
287 } |