Use cxxopts for solar-server
This commit is contained in:
@@ -1,34 +1,56 @@
|
||||
#include <cxxopts.hpp>
|
||||
#include <optional>
|
||||
#include <pistache/endpoint.h>
|
||||
#include <solar/server/api.hpp>
|
||||
#include <solar/server/configuration.hpp>
|
||||
#include <tclap/CmdLine.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
std::optional<cxxopts::ParseResult> 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<unsigned>())(
|
||||
"connection-string",
|
||||
"Path to the sqlite3 database file",
|
||||
cxxopts::value<std::string>());
|
||||
|
||||
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<unsigned>
|
||||
listeningPortArg("p", "listening-port", "TCP listening port number", true, 0u, "TCP listening port number");
|
||||
cmd.add(listeningPortArg);
|
||||
|
||||
TCLAP::ValueArg<std::string> 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<std::string>());
|
||||
|
||||
Pistache::Address address(Pistache::Ipv4::any(), listeningPortArg.getValue());
|
||||
Pistache::Address address(Pistache::Ipv4::any(), args["listening-port"].as<unsigned>());
|
||||
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<unsigned>());
|
||||
server.serve();
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user