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