101 lines
1.8 KiB
C++
101 lines
1.8 KiB
C++
#include <iostream>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "httplib.h"
|
|
#include "json.hpp"
|
|
|
|
#include "helpers.h"
|
|
|
|
using nlohmann::json;
|
|
|
|
using namespace std;
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
string config_filename = "config.json";
|
|
string devices_filename = "devices.json";
|
|
string host = "";
|
|
string cert_file = "";
|
|
string key_file = "";
|
|
uint16_t port = 0;
|
|
shared_ptr<httplib::Server> server = nullptr;
|
|
|
|
bool ok;
|
|
json cfg;
|
|
|
|
if (argc > 2)
|
|
{
|
|
config_filename = argv[1];
|
|
}
|
|
|
|
ok = read_config_json(cfg, config_filename, &cout);
|
|
if (!ok)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
json_extract(cfg, "devices_filename", devices_filename);
|
|
json_extract(cfg, "host", host);
|
|
json_extract(cfg, "port", port);
|
|
// json_extract(cfg, "cert_file", cert_file);
|
|
// json_extract(cfg, "key_file", cert_file);
|
|
|
|
if (host.empty())
|
|
{
|
|
cout << "host not provided" << endl;
|
|
return -1;
|
|
}
|
|
|
|
if (0 == port)
|
|
{
|
|
cout << "port number not provided" << endl;
|
|
return -1;
|
|
}
|
|
|
|
if (!cert_file.empty() && !key_file.empty())
|
|
{
|
|
// TODO: Implement SSL Server Properly
|
|
}
|
|
else
|
|
{
|
|
server = make_shared<httplib::Server>();
|
|
}
|
|
|
|
auto setup_handler = [](const httplib::Request& req, httplib::Response& res)
|
|
{
|
|
cout << req.headers.size() << endl;
|
|
for (auto header : req.headers)
|
|
{
|
|
cout << header.first << ": " << header.second << endl;
|
|
}
|
|
res.status = 400;
|
|
};
|
|
|
|
auto log_handler = [](const httplib::Request& req, httplib::Response& res)
|
|
{
|
|
try
|
|
{
|
|
json j = json::parse(req.body);
|
|
cout << j.dump(4) << endl;
|
|
}
|
|
catch (const exception& e)
|
|
{
|
|
cout << req.body << endl;
|
|
}
|
|
res.status = 200;
|
|
};
|
|
|
|
server->Get("/api/setup/", setup_handler);
|
|
server->Get("/api/setup", setup_handler);
|
|
|
|
server->Post("/api/log/", log_handler);
|
|
server->Post("/api/log", log_handler);
|
|
|
|
server->listen(host, port);
|
|
|
|
return 0;
|
|
}
|