diff --git a/src/solar-server/main.cpp b/src/solar-server/main.cpp index 29a60ba..0a061a6 100644 --- a/src/solar-server/main.cpp +++ b/src/solar-server/main.cpp @@ -1,34 +1,56 @@ +#include +#include #include #include #include -#include +#include + +std::optional ExtractArgs(int argc, char ** argv) +{ + cxxopts::Options options( + "solar-server", + "solar-server is a small Pistache based HTTP content server with a REST API to access the solar log database"); + + options.add_options()( + "p,listening-port", + "TCP port to listen on for REST API requests.", + cxxopts::value())( + "connection-string", + "Path to the sqlite3 database file", + cxxopts::value()); + + if(argc == 1) + { + std::cout << options.help() << std::endl; + return {}; + } + + try + { + auto const parsed = options.parse(argc, argv); + return parsed; + } + catch(cxxopts::OptionException const & e) + { + spdlog::error(e.what()); + return {}; + } +} int main(int argc, char ** argv) { - TCLAP::CmdLine cmd( - "solar-server is a small Pistache based HTTP content server with a REST API to access the solar log database", - ' ', - "1.0.0"); + auto const maybeArgs = ExtractArgs(argc, argv); + if(!maybeArgs.has_value()) + { + return 1; + } - TCLAP::ValueArg - listeningPortArg("p", "listening-port", "TCP listening port number", true, 0u, "TCP listening port number"); - cmd.add(listeningPortArg); - - TCLAP::ValueArg databaseFileArg( - "d", - "database-file", - "Absolute path pointing to the solar SQLite *.db file", - true, - "", - "Absolute path pointing to the solar SQLite *.db file"); - cmd.add(databaseFileArg); - - cmd.parse(argc, argv); + auto const & args = maybeArgs.value(); Configuration & config = Configuration::Get(); - config.SetupDatabase(databaseFileArg.getValue()); + config.SetupDatabase(args["connection-string"].as()); - Pistache::Address address(Pistache::Ipv4::any(), listeningPortArg.getValue()); + Pistache::Address address(Pistache::Ipv4::any(), args["listening-port"].as()); Pistache::Http::Endpoint server(address); auto options = Pistache::Http::Endpoint::options().threads(2); @@ -38,5 +60,6 @@ int main(int argc, char ** argv) Api::SetupRouting(router); server.setHandler(router.handler()); + spdlog::info("solar-server listening on localhost:{0}", args["listening-port"].as()); server.serve(); } \ No newline at end of file diff --git a/systemd/solar-server.service b/systemd/solar-server.service index f2011aa..2a9f9a6 100644 --- a/systemd/solar-server.service +++ b/systemd/solar-server.service @@ -5,7 +5,7 @@ After=network.target [Service] Type=simple -ExecStart=/usr/local/bin/solar-server -d /mnt/data0/log/solarpaneloutput.db -p 3001 +ExecStart=/usr/local/bin/solar-server -p 3001 --connection-string /mnt/data0/log/solarpaneloutput.db Restart=always RestartSec=30