Skip to content

Commit

Permalink
Config validation fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Kraynyukhov committed May 9, 2018
1 parent 8520334 commit 2c29307
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 104 deletions.
19 changes: 10 additions & 9 deletions include/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,27 @@ namespace LAppS
},
{
"services", {
{{"echo", {
{"echo", {
{"internal", false},
{"request_target", "/echo"},
{"protocol", "raw"},
{"max_inbound_message_size",16*1024*1024}, // autobahn-testsuite uses up to 16MB messages in fuzzingclinet.
{"instances", 4}
}}},
{{"echo_lapps", {
}},
{"echo_lapps", {
{"request_target", "/echo_lapps"},
{"protocol", "LAppS"},
{"max_inbound_message_size",1024},
{"instances", 3}
}}},
{{"time_broadcast", {
}},
{"time_broadcast", {
{"internal", true},
{"instances", 1}
}}},
{{"broadcast_blob", {
}},
{"broadcast_blob", {
{"internal", true},
{"instances", 1}
}}}
}}
/**,
{{"data_source", {
{"internal", true},
Expand Down Expand Up @@ -127,11 +127,12 @@ namespace LAppS

while(it!=lapps_config["services"].end())
{
const std::string app_path=apps_dir+it.value().begin().key()+"/?.lua";
const std::string app_path=apps_dir+"/"+it.key()+"/?.lua";
lua_path=lua_path+";"+app_path;
++it;
}
mEnv.setEnv("LUA_PATH",lua_path.c_str());
std::cout << mEnv.getEnv("LUA_PATH") << std::endl;
}
const json& getWSConfig() const
{
Expand Down
12 changes: 7 additions & 5 deletions include/Env.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@ namespace environment
private:
std::map<std::string,std::string> config;

const char* getEnv(const std::string& variable) const
{
return secure_getenv(variable.c_str());
}
public:
LAppSEnv(): config({
{"LAPPS_HOME","/opt/lapps"},
{"LAPPS_CONF_DIR","/opt/lapps/etc/conf"},
{"WS_CONFIG","ws.json"},
{"LAPPS_CONFIG","lapps.json"}
{"LAPPS_CONFIG","lapps.json"},
{"LUA_PATH",""}
}){
auto it=config.begin();

Expand All @@ -60,6 +57,11 @@ namespace environment
}
);
}

const char* getEnv(const std::string& variable) const
{
return secure_getenv(variable.c_str());
}

void setEnv(const std::string& name, const std::string& value)
{
Expand Down
181 changes: 91 additions & 90 deletions include/wsServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,117 +90,119 @@ namespace LAppS
try
{
auto it=LAppSConfig::getInstance()->getLAppSConfig().find("services");
if(it!=LAppSConfig::getInstance()->getLAppSConfig().end())
if(it!=LAppSConfig::getInstance()->getLAppSConfig().end()) // services are defined
{
for(size_t i=0;i<it.value().size();++i)
auto services=it.value().begin();

while(services!=it.value().end())
{
auto service=it.value()[i].begin();
std::string service_name=service.key();
std::string service_name=services.key();

auto options=services.value();

for(;service != it.value()[i].end();++service)
bool internal=false;

auto find_internal=options.find("internal");

if(find_internal!=options.end()) // is internal?
{
internal=find_internal.value();
}

if(internal)
{
bool internal=false;

auto find_internal=service.value().find("internal");
if(find_internal!=service.value().end()) // is internal?
auto instances_it=options.find("instances");
if(instances_it != options.end())
{
size_t instances=instances_it.value();
for(size_t i=0;i<instances;++i)
{
itc::getLog()->info(__FILE__,__LINE__,"Starting service %s instance %u",service_name.c_str(),i);
::InternalAppsRegistry::getInstance()->startInstance(service_name);
}
}
}
else
{
std::string proto;
std::string app_target;
size_t max_inbound_message_size=std::numeric_limits<size_t>::max();

auto found=options.find("max_inbound_message_size");
if((found != options.end())&&found.value().is_number_integer())
{
internal=find_internal.value();
max_inbound_message_size=found.value();
}

if(internal)

found=options.find("protocol");
if(found != options.end())
{
auto instances_it=service.value().find("instances");
if(instances_it != service.value().end())
{
size_t instances=instances_it.value();
for(size_t i=0;i<instances;++i)
{
itc::getLog()->info(__FILE__,__LINE__,"Starting service %s instance %u",service_name.c_str(),i);
::InternalAppsRegistry::getInstance()->startInstance(service_name);
}
}
proto=found.value();
}
else
{
std::string proto;
std::string app_target;
size_t max_inbound_message_size=std::numeric_limits<size_t>::max();

auto found=service.value().find("max_inbound_message_size");
if((found != service.value().end())&&found.value().is_number_integer())
{
max_inbound_message_size=found.value();
}
proto.clear();
}

found=service.value().find("protocol");
if(found != service.value().end())
{
proto=found.value();
}
else
{
proto.clear();
}
found=service.value().find("request_target");
if(found != service.value().end())
{
app_target=found.value();
}
else
{
app_target.clear();
}
found=options.find("request_target");
if(found != options.end())
{
app_target=found.value();
}
else
{
app_target.clear();
}

if((!app_target.empty())&&(!proto.empty()))
{
std::regex request_target("^[/][[:alpha:][:digit:]_-]*([/]?[[:alpha:][:digit:]_-]+)*$");

if((!app_target.empty())&&(!proto.empty()))
if(std::regex_match(app_target,request_target))
{
std::regex request_target("^[/][[:alpha:][:digit:]_-]*([/]?[[:alpha:][:digit:]_-]+)*$");
if(proto.empty())
throw std::system_error(EINVAL,std::system_category(), "Application protocol is empty in configuration of the service "+service_name);
if(proto == "raw")
{
size_t instances=1;

if(std::regex_match(app_target,request_target))
found=options.find("instances");
if(found != options.end())
{
instances=found.value();
}
for(size_t i=0;i<instances;++i)
{
itc::getLog()->info(__FILE__,__LINE__,"Starting service %s instance %u",service_name.c_str(),i);
::ApplicationRegistry::getInstance()->regApp(
std::make_shared<LAppRAW>(service_name,app_target,max_inbound_message_size)
);
}
}
else if(proto == "LAppS")
{
if(proto.empty())
throw std::system_error(EINVAL,std::system_category(), "Application protocol is empty in configuration of the service "+service_name);
if(proto == "raw")
size_t instances=1;

found=options.find("instances");
if(found != options.end())
{
size_t instances=1;

found=service.value().find("instances");
if(found != service.value().end())
{
instances=found.value();
}
for(size_t i=0;i<instances;++i)
{
itc::getLog()->info(__FILE__,__LINE__,"Starting service %s instance %u",service_name.c_str(),i);
::ApplicationRegistry::getInstance()->regApp(
std::make_shared<LAppRAW>(service_name,app_target,max_inbound_message_size)
);
}
instances=found.value();
}
else if(proto == "LAppS")
for(size_t i=0;i<instances;++i)
{
size_t instances=1;

found=service.value().find("instances");
if(found != service.value().end())
{
instances=found.value();
}
for(size_t i=0;i<instances;++i)
{
::ApplicationRegistry::getInstance()->regApp(
std::make_shared<LAppLAPPS>(service_name,app_target,max_inbound_message_size)
);
}
}else{
throw std::system_error(EINVAL,std::system_category(), "Incorrect protocol is specified for target "+app_target+" in service "+service_name+". Only two protocols are supported: raw, LAppS");
::ApplicationRegistry::getInstance()->regApp(
std::make_shared<LAppLAPPS>(service_name,app_target,max_inbound_message_size)
);
}
} else throw std::system_error(EINVAL,std::system_category(), "Incorrect request target: "+app_target+" in configuration of service "+service.key());
} else throw std::system_error(EINVAL,std::system_category(), "Undefined request_target or protocol keyword which are both mandatory");
}
}else{
throw std::system_error(EINVAL,std::system_category(), "Incorrect protocol is specified for target "+app_target+" in service "+service_name+". Only two protocols are supported: raw, LAppS");
}
} else throw std::system_error(EINVAL,std::system_category(), "Incorrect request target: "+app_target+" in configuration of service "+service_name);
} else throw std::system_error(EINVAL,std::system_category(), "Undefined request_target or protocol keyword which are both mandatory");
}
++services;
}
}

size_t max_listeners=LAppSConfig::getInstance()->getWSConfig()["listeners"];

for(size_t i=0;i<max_listeners;++i)
Expand All @@ -216,7 +218,6 @@ namespace LAppS
)
));
}

}catch(const std::exception& e)
{
itc::getLog()->fatal(__FILE__,__LINE__,"Caught an exception: %s. Abort.",e.what());
Expand Down

0 comments on commit 2c29307

Please # to comment.