提交 6c00a563 authored 作者: bruce.lu's avatar bruce.lu

init

上级 2201b433
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
"C_Cpp.intelliSenseEngineFallback": "Enabled", "C_Cpp.intelliSenseEngineFallback": "Enabled",
"python.pythonPath": "/opt/apps/conda/anaconda3/bin/python", "python.pythonPath": "/opt/apps/conda/anaconda3/bin/python",
"files.associations": { "files.associations": {
"chrono": "cpp" "chrono": "cpp",
"optional": "cpp"
} }
} }
\ No newline at end of file
...@@ -26,66 +26,16 @@ class HttpSrv{ ...@@ -26,66 +26,16 @@ class HttpSrv{
// sn:module -> sn_of_evmgr // sn:module -> sn_of_evmgr
json configMap; json configMap;
protected: json config(string body){
public:
void run(){
// load configmap
json cnfm;
LVDB::getValue(cnfm, "configmap");
if(cnfm.size() != 0){
this->configMap = cnfm;
}
svr.Get("/config", [this](const Request& req, Response& res){
json ret; json ret;
ret["code"] = 0; ret["code"] = 0;
ret["time"] = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count();
ret["msg"] = "ok"; ret["msg"] = "ok";
if(!req.has_param("sn") || !req.has_param("module")||req.get_param_value("module").size()< 4){
ret["code"] = 1;
ret["msg"] = "evcloud bad req: no sn/module param";
spdlog::error(ret["msg"].get<string>());
}else{
string sn = req.get_param_value("sn");
string module = req.get_param_value("module");
string modname = module.substr(0,4);
if(modname == "evml") {
modname = "evml:" + module.substr(4, module.size());
}else{
modname = module;
}
string key = sn + ":" + modname;
if(this->configMap.count(key) != 0) {
json config;
ret = LVDB::getLocalConfig(config, this->configMap[key]);
if(ret < 0) {
ret["code"] = 1;
ret["msg"] = "evcloud failed to get config with k, v:" + key + " " + this->configMap[key].get<string>();
spdlog::error(ret["msg"].get<string>());
}else{
ret["data"] = config;
}
}else{
ret["code"] = 1;
ret["msg"] = "no config for sn " + sn, + ", module " + module;
}
}
res.set_content(ret.dump(), "text/json");
});
svr.Post("/config", [this](const Request& req, Response& res){
json ret;
ret["time"] = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count(); ret["time"] = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count();
ret["code"] = 0;
ret["msg"] = "ok";
try{ try{
json newConfig = json::parse(req.body); json newConfig = json::parse(body);
if(newConfig.count("data") == 0 || newConfig["data"].size() == 0) { if(newConfig.count("data") == 0 || newConfig["data"].size() == 0) {
ret["code"] = 1; ret["code"] = 1;
ret["msg"] = "evcloudsvc invalid config body received: " + req.body; ret["msg"] = "evcloudsvc invalid config body received: " + body;
spdlog::error(ret["msg"].get<string>()); spdlog::error(ret["msg"].get<string>());
}else{ }else{
json &data = newConfig["data"]; json &data = newConfig["data"];
...@@ -136,15 +86,120 @@ class HttpSrv{ ...@@ -136,15 +86,120 @@ class HttpSrv{
// save configmap // save configmap
LVDB::setValue(this->configMap, "configmap"); LVDB::setValue(this->configMap, "configmap");
}
}catch(exception &e) {
ret.clear();
ret["code"] = -1;
ret["msg"] = e.what();
}
return ret;
}
protected:
public:
void run(){
// load configmap
json cnfm;
LVDB::getValue(cnfm, "configmap");
if(cnfm.size() != 0){
this->configMap = cnfm;
}
svr.Post("/register", [this](const Request& req, Response& res){
json ret;
try{
json config = json::parse(req.body);
string sn = req.get_param_value("sn");
string module = req.get_param_value("module");
if(sn.empty()||module.empty()){
throw "no para sn/module";
}
string modname = module.substr(0,4);
if(modname == "evml") {
modname = "evml:" + module.substr(4, module.size());
}else{
modname = module;
} }
// TODO: restart other components string key = sn + ":" + modname;
if(this->configMap.count(key) == 0){
// //
spdlog::info("evcloudsvc no such edge module registred: {}, create new entry", key);
ret = this->config(req.body);
if(ret["code"] == 0) {
ret["data"] = config["data"];
}
}else{
// TODO: calc md5
int r;
ret["code"] = 0;
ret["msg"] = "ok";
string dk = this->configMap[key];
json data;
r = LVDB::getLocalConfig(data, dk);
if(ret < 0) {
ret["code"] = r;
ret["msg"] = "failed to get config for: " + dk;
}else{
ret["data"] = data;
}
}
}catch(exception &e) { }catch(exception &e) {
ret.clear(); ret.clear();
ret["code"] = 1; ret["code"] = -1;
ret["msg"] = e.what(); ret["msg"] = e.what();
} }
res.set_content(ret.dump(), "text/json");
});
svr.Get("/config", [this](const Request& req, Response& res){
json ret;
ret["code"] = 0;
ret["time"] = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count();
ret["msg"] = "ok";
if(!req.has_param("sn") || !req.has_param("module")||req.get_param_value("module").size()< 4){
ret["code"] = 1;
ret["msg"] = "evcloud bad req: no sn/module param";
spdlog::error(ret["msg"].get<string>());
}else{
string sn = req.get_param_value("sn");
string module = req.get_param_value("module");
string modname = module.substr(0,4);
if(modname == "evml") {
modname = "evml:" + module.substr(4, module.size());
}else{
modname = module;
}
string key = sn + ":" + modname;
if(this->configMap.count(key) != 0) {
json config;
ret = LVDB::getLocalConfig(config, this->configMap[key]);
if(ret < 0) {
ret["code"] = 1;
ret["msg"] = "evcloud failed to get config with k, v:" + key + " " + this->configMap[key].get<string>();
spdlog::error(ret["msg"].get<string>());
}else{
ret["data"] = config;
}
}else{
ret["code"] = 1;
ret["msg"] = "no config for sn " + sn, + ", module " + module;
}
}
res.set_content(ret.dump(), "text/json");
});
svr.Post("/config", [this](const Request& req, Response& res){
auto ret = this->config(req.body);
res.set_content(ret.dump(), "text/json"); res.set_content(ret.dump(), "text/json");
}); });
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论