/* module: evdaemon description: to monitor and configure all other components. runs only one instance per host. author: Bruce.Lu update: 2019/08/30 */ #pragma GCC diagnostic ignored "-Wunused-variable" #pragma GCC diagnostic ignored "-Wunused-lambda-capture" #include "inc/tinythread.hpp" #include "inc/httplib.h" #include "inc/zmqhelper.hpp" #include "inc/database.h" #include "inc/json.hpp" #include using namespace std; using namespace httplib; using namespace nlohmann; class HttpSrv{ private: Server svr; json config; json info; int port = 8088; void setMonitorThread() { } protected: public: void run(){ setMonitorThread(); // get config svr.Get("/info", [this](const Request& req, Response& res){ LVDB::getSn(this->info); res.set_content(this->info.dump(), "text/json"); }); svr.Post("/info", [this](const Request& req, Response& res){ json ret; ret["code"] = 0; ret["msg"] = "ok"; string sn = req.get_param_value("sn"); if(sn.empty()){ ret["code"] = 1; ret["msg"] = "no sn in param"; }else{ json info; info["sn"] = sn; // TODO: info["lastboot"] = chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count(); LVDB::setSn(info); } res.set_content(this->info.dump(), "text/json"); }); svr.Get("/config", [this](const Request& req, Response& res){ LVDB::getLocalConfig(this->config); res.set_content(this->config.dump(), "text/json"); }); svr.Post("/config", [this](const Request& req, Response& res){ json ret; ret["code"] = 0; ret["msg"] = "ok"; ret["time"] = chrono::duration_cast(chrono::system_clock::now().time_since_epoch()).count(); try{ json newConfig; newConfig["data"] = json::parse(req.body)["data"]; LVDB::setLocalConfig(newConfig); spdlog::info("evmgr new config: {}", newConfig.dump(4)); // TODO: restart other components // }catch(exception &e) { ret.clear(); ret["code"] = -1; ret["msg"] = e.what(); ret["data"] = req.body; } res.set_content(ret.dump(), "text/json"); }); svr.Post("/reset", [](const Request& req, Response& res){ }); svr.listen("0.0.0.0", 8088); } HttpSrv(){ char* strPort = getenv("PORT"); if(strPort != NULL) { port = stoi(strPort); } }; ~HttpSrv(){}; }; int main(){ json info; LVDB::getSn(info); spdlog::info("evdaemon: \n{}",info.dump(4)); HttpSrv srv; srv.run(); }