-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
118 lines (102 loc) · 3.64 KB
/
main.cpp
File metadata and controls
118 lines (102 loc) · 3.64 KB
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
#include <drogon/drogon.h>
#include <fmt/format.h>
#include <boost/program_options.hpp>
#include <spdlog/spdlog.h>
#include <fstream>
#include "src/Controller.h"
using namespace drogon;
// Fix parsing std::filesystem::path with spaces (see https://github.com/boostorg/program_options/issues/69)
namespace boost
{
template <>
inline std::filesystem::path lexical_cast<std::filesystem::path, std::basic_string<char>>(const std::basic_string<char> &arg)
{
return std::filesystem::path(arg);
}
}
namespace po = boost::program_options;
int main( int argc, char** argv )
{
std::string host;
std::filesystem::path certPath, keyPath, uploads;
int port;
bool clearUploads;
Controller::Config config;
po::options_description desc( "Simple Inference Server" );
desc.add_options()
( "help,h", "Display help message" )
( "invokePath", po::value( &config.invokePath )->required(), "Path to the script that will be invoked" )
( "indexPath", po::value( &config.indexPath )->default_value( {} ), "Path to the index.html" )
( "host", po::value( &host )->default_value( "127.0.0.1" ), "Host to bind to" )
( "port", po::value( &port )->default_value( 7654 ), "Port to bind to" )
( "cert", po::value( &certPath )->default_value( {} ), "Path to SSL certificate" )
( "key", po::value( &keyPath )->default_value( {} ), "Path to SSL key" )
( "uploads", po::value( &uploads )->default_value( "uploads" ), "Folder to store uploaded files" )
( "clear_uploads", po::bool_switch( &clearUploads )->default_value( false ), "Clear uploads folder" )
;
po::variables_map vm;
try
{
po::store( po::parse_command_line( argc, argv, desc ), vm );
if ( vm.count( "help" ) )
{
std::cout << desc << std::endl;
return 0;
}
po::notify( vm );
}
catch ( po::error& err )
{
std::cerr << "Failed to parse arguments: " << err.what() << std::endl;
return -1;
}
std::error_code ec;
if ( std::filesystem::exists( uploads, ec ) && clearUploads )
{
std::filesystem::remove_all( uploads, ec );
if ( ec )
{
spdlog::error( "Could not remove uploads directory: {}", ec.message() );
return -1;
}
}
// check that uploads directory is writable (or that we can create it)
if ( std::filesystem::exists( uploads, ec ) )
{
const auto checkDir = uploads / "check";
if ( !std::filesystem::create_directory( checkDir, ec ) )
{
spdlog::error( "Uploads directory is not writable: {}", ec.message() );
return -1;
}
std::filesystem::remove( checkDir, ec );
}
else
{
if ( !std::filesystem::create_directory( uploads, ec ) )
{
spdlog::error( "Could not create uploads directory: {}", ec.message() );
return -1;
}
}
const bool useSSL = exists( certPath ) && exists( keyPath );
if ( useSSL && !app().supportSSL() )
{
spdlog::error( "Current configuration does not support SSL" );
return -1;
}
if ( useSSL )
spdlog::info( "Use SSL" );
else
spdlog::info( "Do not use SSL" );
spdlog::default_logger()->flush();
spdlog::default_logger()->set_level( spdlog::level::debug );
trantor::Logger::enableSpdLog( spdlog::default_logger() );
app()
.setClientMaxBodySize( 1024*1024*1024 ) // 1gb
.setUploadPath( uploads )
.addListener( host, port, useSSL, certPath, keyPath )
.registerController( std::make_shared<Controller>( config ) )
.run();
return 0;
}