16#include <crow/compression.h>
17#include <crow/logging.h>
27#include <sys/select.h>
34#include <crow/http_response.h>
35#include <grpcpp/server.h>
38using namespace std::filesystem;
43 std::atomic<bool> s_consoleLoopStarted{
false };
44 std::atomic<bool> s_consoleLoopStop{
false };
45 std::thread s_consoleThread;
47 void start_interactive_quit_if_tty()
49 if (s_consoleLoopStarted.exchange(
true))
return;
55 s_consoleLoopStop.store(
false);
56 s_consoleThread = std::thread([]
58 std::fprintf(stderr,
"[interactive] Type 'q' then Enter to quit gracefully...\n");
60 while (!s_consoleLoopStop.load() && std::getline(std::cin, line))
62 for (
auto& ch : line) ch =
static_cast<char>(std::tolower(
static_cast<unsigned char>(ch)));
63 if (line ==
"q" || line ==
"quit" || line ==
"exit" || line ==
"shutdown")
73 void stop_interactive_quit() { s_consoleLoopStop.store(
true); }
74 void join_interactive_quit() {
if (s_consoleThread.joinable()) s_consoleThread.join(); }
80 std::atomic<OdbServerAppBase *> OdbServerAppBase::s_activeInstance{
nullptr};
83 :
OdbAppBase(argc, argv), m_shutdownFlag(false)
87 s_activeInstance.store(
this, std::memory_order_release);
106 s_activeInstance.store(
nullptr, std::memory_order_release);
111 auto *instance = s_activeInstance.load(std::memory_order_acquire);
112 if (instance !=
nullptr)
114 instance->signal_handler(signum);
122 std::cout <<
"gRPC server not implemented in base class" << std::endl;
125 void OdbServerAppBase::start_grpc_server()
127 if (
args().grpcPort() > 0 && m_pDesignCache)
134 if (!grpcBindAddress.empty() && grpcBindAddress.front() !=
'[' && grpcBindAddress.find(
':') != std::string::npos)
136 grpcBindAddress =
"[" + grpcBindAddress +
"]";
138 std::string grpc_server_address = grpcBindAddress +
":" + std::to_string(
args().grpcPort());
140 std::cout <<
"gRPC server thread started on port " <<
args().
grpcPort() << std::endl;
151 void OdbServerAppBase::stop_servers()
158 std::cout <<
"Shutting down servers..." << std::endl;
175 std::cout <<
"All servers shut down." << std::endl;
184 return ExitCode::Success;
189 return ExitCode::FailedInit;
192 m_crowApp.loglevel(crow::LogLevel::Info);
195 m_crowApp.use_compression(crow::compression::algorithm::GZIP);
204 CROW_ROUTE(m_crowApp,
"/healthz")([] {
205 return crow::response{ 200,
"ok" };
207 CROW_ROUTE(m_crowApp,
"/ready")([
this] {
208 if (
m_shutdownFlag.load())
return crow::response{ 503,
"not ready" };
209 return crow::response{ 200,
"ready" };
216 m_crowApp.bindaddr(
args().bindAddress());
219 m_crowApp.port(
static_cast<unsigned short>(
args().port()));
222 m_crowApp.multithreaded();
225 return ExitCode::PreServerRunFailed;
231 start_interactive_quit_if_tty();
235 <<
" (http-trace=" << (
args().
httpTrace() ?
"true" :
"false") <<
")" << std::endl;
239 stop_interactive_quit();
240 join_interactive_quit();
255 return ExitCode::PostServerRunFailed;
258 return ExitCode::Success;
268 return *m_pRequestAuthentication;
273 m_pRequestAuthentication = std::move(pRequestAuthentication);
278 return m_pDesignCache;
283 m_pDesignCache = pDesignCache;
286 void OdbServerAppBase::register_routes()
290 pController->register_routes();
294 void OdbServerAppBase::signal_handler(
int signum)
296 std::cout <<
"\nSignal " << signum <<
" received. Initiating graceful shutdown..." << std::endl;
DesignCache m_designCache
const OdbDesignArgs & args() const override
virtual Utils::ExitCode Run() override
std::string bindAddress() const
std::shared_ptr< DesignCache > design_cache() override
RequestAuthenticationBase & request_auth() override
virtual bool preServerRun()
std::shared_ptr< grpc::Server > m_grpcServer
std::unique_ptr< std::thread > m_grpcThread
virtual bool postServerRun()
CrowApp & crow_app() override
RouteController::Vector m_vecControllers
virtual void RunGrpcServer(const std::string &server_address, std::shared_ptr< DesignCache > cache)
std::atomic< bool > m_shutdownFlag
void set_grpc_server(std::shared_ptr< grpc::Server > server)
static void HandleSignal(int signum)
~OdbServerAppBase() override
Utils::ExitCode Run() override
OdbServerAppBase(int argc, char *argv[])
virtual void add_controllers()=0
std::shared_ptr< grpc::Server > grpc_server() const
static void SetEnabled(bool enabled) noexcept
crow::Crow< crow::CORSHandler > CrowApp