main.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <csignal>
  2. #include <iostream>
  3. #include "webserver_service.hpp"
  4. #include "logging/logger.hpp"
  5. #include "config/config_loader.hpp"
  6. #include "common/config_arg.hpp"
  7. using namespace smartbotic;
  8. std::atomic<bool> g_shutdown{false};
  9. void signalHandler(int signal) {
  10. LOG_INFO("Received signal {}, shutting down...", signal);
  11. g_shutdown = true;
  12. }
  13. int main(int argc, char* argv[]) {
  14. // Initialize logging
  15. logging::LogConfig log_config;
  16. log_config.name = "webserver";
  17. log_config.level = logging::LogLevel::Info;
  18. logging::Logger::init(log_config);
  19. LOG_INFO("SmartBotic WebServer starting...");
  20. // Load configuration
  21. const auto config_arg = common::parseConfigArg(argc, argv, "./config/webserver.json");
  22. const std::filesystem::path config_path = config_arg.path;
  23. webserver::WebServerServiceConfig config;
  24. if (std::filesystem::exists(config_path)) {
  25. config = webserver::WebServerService::loadConfig(config_path);
  26. LOG_INFO("Loaded configuration from {}", config_path.string());
  27. } else if (config_arg.explicitly_given) {
  28. // Someone named this file, so starting on defaults would run the
  29. // service against a different database, project namespace and port
  30. // than they asked for - and the only sign would be a warning nobody
  31. // reads. Refuse to start instead.
  32. LOG_ERROR("Config file not found at {}. It was given on the command line, "
  33. "so refusing to start on built-in defaults", config_path.string());
  34. return 1;
  35. } else {
  36. LOG_WARN("Config file not found at {}, using defaults", config_path.string());
  37. }
  38. // Setup signal handlers
  39. std::signal(SIGINT, signalHandler);
  40. std::signal(SIGTERM, signalHandler);
  41. // Create and start service
  42. webserver::WebServerService service(config);
  43. try {
  44. service.start();
  45. // Wait for shutdown signal
  46. while (!g_shutdown) {
  47. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  48. }
  49. service.stop();
  50. } catch (const std::exception& e) {
  51. LOG_ERROR("Fatal error: {}", e.what());
  52. return 1;
  53. }
  54. LOG_INFO("SmartBotic WebServer stopped");
  55. logging::Logger::shutdown();
  56. return 0;
  57. }