-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.cpp
317 lines (261 loc) · 10.6 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Created by wcjohns on 20110322.
/* InterSpec: an application to analyze spectral gamma radiation data.
Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC
(NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
Government retains certain rights in this software.
For questions contact William Johnson via email at wcjohns@sandia.gov, or
alternative emails of interspec@sandia.gov.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
This file is for creating a InterSpec web-server; either localhost (primarily for development
purposes), or as a web-server (has only been superficially tested inside docker containers).
To build stand-alone "native" apps, see the sub-directories in the "target" directory.
*/
#include "InterSpec_config.h"
#include <string>
#include <iostream>
#include <boost/program_options.hpp>
#include "SpecUtils/StringAlgo.h"
#include "SpecUtils/Filesystem.h"
#include "InterSpec/AppUtils.h"
#include "InterSpec/InterSpec.h"
#include "InterSpec/InterSpecServer.h"
#if( USE_BATCH_TOOLS )
#include "InterSpec/BatchCommandLine.h"
#endif
int main( int argc, char **argv )
{
#ifdef _WIN32
AppUtils::getUtf8Args( argc, argv );
#endif
std::cout << std::showbase << std::hex << "Running with Wt version "
<< WT_VERSION << std::dec << ", from executable compiled on "
<< __DATE__ << std::endl;
#if( PERFORM_DEVELOPER_CHECKS )
std::cout << "Developer tests are being performed" << std::endl;
#endif
std::cout << std::endl;
int server_port_num;
std::string docroot, wt_config, user_data_dir;
#if( USE_BATCH_TOOLS )
bool batch_peak_fit = false, batch_act_fit = false;
#endif
#if( BUILD_FOR_WEB_DEPLOYMENT )
std::string http_address = "127.0.0.1";
static_assert( !BUILD_AS_LOCAL_SERVER, "Web and local server should not both be enabled");
#endif
namespace po = boost::program_options;
unsigned term_width = AppUtils::terminal_width();
unsigned min_description_length = ((term_width < 80u) ? term_width/2u : 40u);
po::options_description cl_desc("Allowed options", term_width, min_description_length);
cl_desc.add_options()
("help,h", "produce this help message")
("http-port", po::value<int>(&server_port_num)->default_value(8080),
"The HTTP port to bind the web-server too. Ports below 1024 are not recommended, and require elevated privileges.")
#if( BUILD_FOR_WEB_DEPLOYMENT )
( "http-address", po::value<std::string>(&http_address),
"The network HTTP address to bind the web-server too; '127.0.0.1' is localhost, while '0.0.0.0' will serve the web-app to the external network."
)
#endif
("userdatadir", po::value<std::string>(&user_data_dir),
"The directory to store user data to, or to look in for custom user data (serial_to_model.csv, etc)."
)
("config", po::value<std::string>(&wt_config),
"The Wt config XML file to use."
)
("docroot", po::value<std::string>(&docroot),
"The directory that contains the 'InterSpec_resources' and 'data' directories.\n"
"All files in the docroot directory, and its subdirectories are available via HTTP."
#if( !BUILD_FOR_WEB_DEPLOYMENT )
"\nDefaults to current working directory."
#endif
"\nThis value sets Wts 'docroot' and 'approot' variables."
)
("static-data-dir", "The static data directory (e.g., 'data' dir that holds cross-sections, "
"nuclear-data, etc) to use. If not specified, uses 'data' in the `docroot` directory."
)
#if( USE_BATCH_TOOLS )
("batch-peak-fit", po::value<bool>(&batch_peak_fit)->implicit_value(true)->default_value(false),
"Batch-fit peaks.\n"
"\tUse '--batch-peak-fit --help' to see available options."
)
("batch-act-fit", po::value<bool>(&batch_act_fit)->implicit_value(true)->default_value(false),
"Batch shielding/source fit.\n"
"\tUse '--batch-act-fit --help' to see available options."
)
#endif
;
po::variables_map cl_vm;
try
{
po::parsed_options parsed_opts
= po::command_line_parser(argc,argv)
#if( USE_BATCH_TOOLS )
.allow_unregistered()
#endif
.options(cl_desc)
.run();
po::store( parsed_opts, cl_vm );
po::notify( cl_vm );
}catch( std::exception &e )
{
std::cerr << "Command line argument error: " << e.what() << std::endl << std::endl;
std::cout << cl_desc << std::endl;
return 1;
}//try catch
#if( USE_BATCH_TOOLS )
const bool is_batch = (batch_peak_fit || batch_act_fit);
#else
const bool is_batch = false;
#endif
if( cl_vm.count("help") && !is_batch )
{
std::cout << "Available command-line options for starting the InterSpec web-server are:\n";
std::cout << cl_desc << std::endl;
return 0;
}//if( cl_vm.count("help") )
#if( BUILD_FOR_WEB_DEPLOYMENT )
if( cl_vm.count("config") )
{
std::cerr << "You must specify the Wt config file to use (the 'config' option)" << std::endl;
return -20;
}
if( cl_vm.count("http-address") )
{
std::cerr << "You must specify the network adapter address to bind to"
<< " (the 'http-address' option)." << std::endl;
return -21;
}
if( cl_vm.count("docroot") )
{
std::cerr << "You must specify the HTTP document root directory to use (the 'docroot' option)" << std::endl;
return -22;
}
#endif
if( (server_port_num <= 0) || (server_port_num > 65535) )
{
std::cerr << "Invalid server port number: " << server_port_num
<< ", must be between 1 and 65535" << std::endl;
return -23;
}
if( server_port_num <= 1024 )
{
#if( !BUILD_FOR_WEB_DEPLOYMENT )
std::cerr << "Server port number below 1024 not allowed." << std::endl;
return -23;
#else
std::cerr << "Warning: using a privileged port is not recommended." << std::endl;
#endif
}//if( server_port_num <= 1024 )
#if( !BUILD_FOR_WEB_DEPLOYMENT && BUILD_AS_LOCAL_SERVER )
if( docroot.empty() )
{
// I cant get MSVC to set CWD to anywhere besides InterSpec/out/build/x64-Debug/,
// so we'll look for our resources up to three levels up.
// However, we def dont want to do this for anything other than localhost development
std::string targetfile = "InterSpec_resources/InterSpec.css";
if( !AppUtils::locate_file( targetfile, false, 3, false ) )
{
std::cerr << "Unable to find base directory that contains 'InterSpec_resources' directory."
<< std::endl;
return -24;
}
docroot = SpecUtils::parent_path( SpecUtils::parent_path( targetfile ) );
}//if( docroot.empty() )
#endif //_WIN32 && !BUILD_FOR_WEB_DEPLOYMENT
if( user_data_dir.empty() )
{
#if( BUILD_AS_LOCAL_SERVER )
// If there is a "user_data" directory in the CWD, we'll set this as the writeable data
// directory to simulate desktop app behavior of saving DRFs and similar
std::string dev_user_data = "user_data";
if( !AppUtils::locate_file( dev_user_data, true, 3, false ) )
{
std::cerr << "No '" << dev_user_data << "' - you must specify writeable data directory,"
<< " or there must be a 'user_data' directory in the current working directory."
<< std::endl;
return -25;
}
// We will make user data path absolute, so there wont be any ambiguity later on
// (I dont think we ever change CWD, but JIC)
if( !SpecUtils::is_absolute_path(dev_user_data) )
{
const std::string cwd = SpecUtils::get_working_path();
const std::string trial_dir = SpecUtils::append_path( cwd, dev_user_data );
if( SpecUtils::is_directory(trial_dir) )
{
dev_user_data = trial_dir;
SpecUtils::make_canonical_path( dev_user_data );
}
}//if( !SpecUtils::is_absolute_path(userDir) )
user_data_dir = dev_user_data;
#else
std::cerr << "You must specify the directory to store user data to (the 'userdatadir' option)."
<< std::endl;
return -25;
#endif
}//if( user_data_dir.empty() )
#if( !BUILD_FOR_WEB_DEPLOYMENT )
if( docroot.empty() )
docroot = ".";
if( wt_config.empty() )
wt_config = SpecUtils::append_path( docroot, "data/config/wt_config_localweb.xml" );
#endif
if( cl_vm.count("static-data-dir") )
{
std::string datadir = cl_vm["static-data-dir"].as<std::string>();
if( !SpecUtils::is_directory(datadir) )
datadir = SpecUtils::append_path( docroot, datadir );
if( !SpecUtils::is_directory(datadir) )
{
std::cerr << "Specified 'static-data-dir' ('" << cl_vm["static-data-dir"].as<std::string>()
<< "') is not a directory." << std::endl;
return -26;
}//if( !SpecUtils::is_directory(datadir) )
// We wont make datadir path absolute, to avoid possible long name hassles on Windows, and I
// dont think the code changes current working directory anywhere.
//datadir = SpecUtils::make_canonical_path(datadir);
InterSpec::setStaticDataDirectory( datadir );
}
#if( !BUILD_FOR_WEB_DEPLOYMENT )
else
{
const std::string datadir = SpecUtils::append_path( docroot, "data" );
if( !SpecUtils::is_directory(datadir) )
{
std::cerr << "No 'data' directory in docroot-'" << docroot << "';"
<< " please specify the '--static-data-dir' argument." << std::endl;
return -26;
}
InterSpec::setStaticDataDirectory( datadir );
}//if( cl_vm.count("static-data-dir") ) / else
#endif
#if( USE_BATCH_TOOLS )
if( is_batch )
return BatchCommandLine::run_batch_command( argc, argv );
#endif
// Start the InterSpec server
const int rval = InterSpecServer::start_server( argv[0], user_data_dir.c_str(),
docroot.c_str(),
wt_config.c_str(),
static_cast<short int>(server_port_num) );
if( rval < 0 )
{
std::cerr << "Failed to start server, val=" << rval << std::endl;
return rval;
}
std::cout << "\nYou can now point your browser to: " << InterSpecServer::urlBeingServedOn()
<< std::endl;
return InterSpecServer::wait_for_shutdown();
}//int main( int argc, const char * argv[] )