Adding fetching of the schema from git

This commit is contained in:
stephb9959
2021-09-30 08:00:34 -07:00
parent b51981bb1c
commit 1ca84df9d1
6 changed files with 2199 additions and 15 deletions

2
build
View File

@@ -1 +1 @@
240
242

View File

@@ -5,10 +5,13 @@
#include <iostream>
#include <fstream>
#include "ConfigurationValidator.h"
#include "Utils.h"
#include "Daemon.h"
namespace OpenWifi {
static const std::string GitUCentralJSONSchemaFile{"https://raw.githubusercontent.com/blogic/ucentral-schema/main/ucentral.schema.json"};
static json DefaultUCentralSchema = R"(
{
"$id": "https://openwrt.org/ucentral.schema.json",
@@ -2158,19 +2161,23 @@ namespace OpenWifi {
if(Initialized_)
return;
std::string FileName{ Daemon()->DataDir() + "/ucentral.schema.json" };
try {
std::ifstream input(FileName);
std::stringstream schema_file;
schema_file << input.rdbuf();
input.close();
auto schema = json::parse(schema_file.str());
Validator_->set_root_schema(schema);
Initialized_ = Working_ = true;
} catch (const std::exception &E ) {
Validator_->set_root_schema(DefaultUCentralSchema);
Initialized_ = Working_ = true;
std::string GitSchema;
if(Utils::wgets(GitUCentralJSONSchemaFile, GitSchema)) {
} else {
std::string FileName{ Daemon()->DataDir() + "/ucentral.schema.json" };
try {
std::ifstream input(FileName);
std::stringstream schema_file;
schema_file << input.rdbuf();
input.close();
GitSchema = schema_file.str();
} catch (const Poco::Exception &E) {
GitSchema = DefaultUCentralSchema;
}
}
Validator_->set_root_schema(GitSchema);
Initialized_ = Working_ = true;
}
bool ConfigurationValidator::Validate(const std::string &C) {

View File

@@ -24,6 +24,9 @@
#include "Poco/File.h"
#include "Poco/StreamCopier.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include "Poco/Net/HTTPSClientSession.h"
#include "Poco/Net/HTTPResponse.h"
#include "uCentralProtocol.h"
#include "Daemon.h"
@@ -488,4 +491,33 @@ namespace OpenWifi::Utils {
return Result;
}
bool wgets(const std::string &URL, std::string &Response) {
try {
Poco::URI uri(URL);
Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort());
// prepare path
std::string path(uri.getPathAndQuery());
if (path.empty()) {
path = "/";
}
// send request
Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_GET, path, Poco::Net::HTTPMessage::HTTP_1_1);
session.sendRequest(req);
Poco::Net::HTTPResponse res;
std::istream &is = session.receiveResponse(res);
std::ostringstream os;
Poco::StreamCopier::copyStream(is,os);
Response = os.str();
return true;
} catch (...) {
}
return false;
}
}

View File

@@ -73,6 +73,8 @@ namespace OpenWifi::Utils {
[[nodiscard]] std::string BinaryFileToHexString( const Poco::File &F);
[[nodiscard]] std::string SecondsToNiceText(uint64_t Seconds);
[[nodiscard]] bool wgets(const std::string &URL, std::string &Response);
template< typename T >
std::string int_to_hex( T i )
{

View File

@@ -42,7 +42,11 @@
{
"type": "entity",
"name": "BC",
"children": [],
"children": [{
"type": "entity",
"name": "Bowen",
"children": [],
"venues": []}],
"venues": [{
"type": "venue",
"name": "BC Place",

File diff suppressed because it is too large Load Diff