OdbDesignLib
OdbDesign ODB++ Parsing Library
OdbServerAppBase.cpp
1 #include "OdbServerAppBase.h"
2 #include "OdbServerAppBase.h"
3 #include "Logger.h"
4 
5 using namespace Utils;
6 using namespace std::filesystem;
7 
8 namespace Odb::Lib::App
9 {
10  OdbServerAppBase::OdbServerAppBase(int argc, char* argv[])
11  : OdbAppBase(argc, argv)
12  {
13  }
14 
15  bool OdbServerAppBase::preServerRun()
16  {
17  // override in extended class to configure server or run custom code
18  return true;
19  }
20 
21  bool OdbServerAppBase::postServerRun()
22  {
23  // override in extended class to cleanup server or run custom code
24  return true;
25  }
26 
27  OdbServerAppBase::~OdbServerAppBase()
28  {
29  m_vecControllers.clear();
30  }
31 
32  ExitCode OdbServerAppBase::Run()
33  {
34  // print usage and exit w/ success
35  if (args().help())
36  {
37  args().printUsage();
38  return ExitCode::Success;
39  }
40 
41  // call base class
42  if (ExitCode::Success != OdbAppBase::Run()) return ExitCode::FailedInit;
43 
44  // set Crow's log level
45  m_crowApp.loglevel(crow::LogLevel::Info);
46 
47  // enable HTTP compression
48  m_crowApp.use_compression(crow::compression::algorithm::GZIP);
49 
50  try
51  {
52  if (args().useHttps())
53  {
54  path sslDirPath(args().sslDir());
55  if (!exists(sslDirPath) || !is_directory(sslDirPath))
56  {
57  logerror("SSL was specified but the directory does not exist, exiting...");
58  return ExitCode::FailedInitSslDirDoesNotExist;
59  }
60 
61  // enable SSL/HTTPS
62  m_crowApp.ssl_file((sslDirPath / SSL_CERT_FILE).string(),
63  (sslDirPath / SSL_KEY_FILE).string());
64  }
65  }
66  catch (boost::wrapexcept<boost::system::system_error>& e)
67  {
68  // log the error
69  logexception(e);
70  logerror("SSL was specified but it failed to initialize, exiting...");
71  return ExitCode::FailedInitSsl;
72  }
73 
74  // let subclasses add controller types
75  add_controllers();
76 
77  // register all added controllers' routes
78  register_routes();
79 
80  // set port to passed-in port or default if none supplied
81  m_crowApp.port(static_cast<unsigned short>(args().port()));
82 
83  // set server to use multiple threads
84  m_crowApp.multithreaded();
85 
86  if (!preServerRun()) return ExitCode::PreServerRunFailed;
87 
88  // run the Crow server
89  m_crowApp.run();
90 
91  if (!postServerRun()) return ExitCode::PostServerRunFailed;
92 
93  // success!
94  return ExitCode::Success;
95  }
96 
97  CrowApp& OdbServerAppBase::crow_app()
98  {
99  return m_crowApp;
100  }
101 
102  RequestAuthenticationBase& OdbServerAppBase::request_auth()
103  {
104  return *m_pRequestAuthentication;
105  }
106 
107  void OdbServerAppBase::request_auth(std::unique_ptr<RequestAuthenticationBase> pRequestAuthentication)
108  {
109  m_pRequestAuthentication = std::move(pRequestAuthentication);
110  }
111 
112  void OdbServerAppBase::register_routes()
113  {
114  for (const auto& pController : m_vecControllers)
115  {
116  pController->register_routes();
117  }
118  }
119 }