Merge pull request 'updated and added more funcs to be small' (#26) from update-funcs into main
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Has been cancelled

Reviewed-on: https://gitea.home.com:3000/TolaMironcenko/openrouter/pulls/26
This commit was merged in pull request #26.
This commit is contained in:
wt
2025-04-19 14:06:20 +07:00
8 changed files with 29 additions and 77 deletions
-1
View File
@@ -9,7 +9,6 @@ bool authenticate(const std::string &token)
httplib::Client cli(config::config.getConfig()["auth_service"]); httplib::Client cli(config::config.getConfig()["auth_service"]);
std::string body = R"({"token":")" + token + R"("})"; std::string body = R"({"token":")" + token + R"("})";
// body << R"({"token":")" << token << R"("})";
httplib::Result res = cli.Post("/api/access", body, JSON_TYPE); httplib::Result res = cli.Post("/api/access", body, JSON_TYPE);
if (res->status == httplib::OK_200) if (res->status == httplib::OK_200)
{ {
+15 -8
View File
@@ -1,13 +1,20 @@
#pragma once #pragma once
#include <httplib.h> #include <httplib.h>
#include <json.hpp> #include <json.hpp>
#include <syslog.h>
static bool check_body(const httplib::Request& request) inline void check_body_empty(const httplib::Request& request, httplib::Response& response, std::string required_string) {
{ if (request.body.empty()) {
if (request.body.empty()) response.set_content(required_string, JSON_TYPE);
return false; syslog(LOG_ERR, "no body in request");
nlohmann::json json_body = nlohmann::json::parse(request.body); return;
if (json_body["token"] == nullptr) }
return false; }
return true;
inline void check_body_data(nlohmann::json body, std::string data, httplib::Response& response, std::string required_string) {
if (body[data] == nullptr) {
response.set_content(required_string, JSON_TYPE);
syslog(LOG_ERR, "Error request");
return;
}
} }
+1 -5
View File
@@ -38,11 +38,7 @@ namespace api
{ {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (!check_body(request)) check_body_empty(request, response, ACCESS_REQUIRED_STRING);
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
if (std::filesystem::exists(config::config.getConfigFileName())) { if (std::filesystem::exists(config::config.getConfigFileName())) {
std::filesystem::remove(config::config.getConfigFileName()); std::filesystem::remove(config::config.getConfigFileName());
std::filesystem::copy("../config_default.json", config::config.getConfigFileName()); std::filesystem::copy("../config_default.json", config::config.getConfigFileName());
+3 -18
View File
@@ -41,13 +41,8 @@ namespace api
{ {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (!check_body(request)) check_body_empty(request, response, ACCESS_REQUIRED_STRING);
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body); nlohmann::json json_body = nlohmann::json::parse(request.body);
if (!authenticate(json_body["token"])) if (!authenticate(json_body["token"]))
{ {
response.set_content(R"({"access":"reject"})", JSON_TYPE); response.set_content(R"({"access":"reject"})", JSON_TYPE);
@@ -62,19 +57,9 @@ namespace api
{ {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (!check_body(request)) check_body_empty(request, response, ACCESS_REQUIRED_STRING);
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body); nlohmann::json json_body = nlohmann::json::parse(request.body);
check_body_data(json_body, "settings", response, SETTINGS_REQUIRED_STRING);
if (json_body["settings"] == nullptr)
{
response.set_content(SETTINGS_REQUIRED_STRING, JSON_TYPE);
return;
}
if (!authenticate(json_body["token"])) if (!authenticate(json_body["token"]))
{ {
response.set_content(R"({"access":"reject"})", JSON_TYPE); response.set_content(R"({"access":"reject"})", JSON_TYPE);
+3 -18
View File
@@ -21,25 +21,10 @@ namespace api
{ {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (!check_body(request)) check_body_empty(request, response, ACCESS_REQUIRED_STRING);
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body); nlohmann::json json_body = nlohmann::json::parse(request.body);
check_body_data(json_body, "password", response, WIFI_PASSWORD_REQUIRED_STRING);
if (json_body["password"] == nullptr) check_body_data(json_body, "newpassword", response, WIFI_PASSWORD_REQUIRED_STRING);
{
response.set_content(WIFI_PASSWORD_REQUIRED_STRING, JSON_TYPE);
return;
}
if (json_body["newpassword"] == nullptr)
{
response.set_content(WIFI_PASSWORD_REQUIRED_STRING, JSON_TYPE);
return;
}
if (!authenticate(json_body["token"])) if (!authenticate(json_body["token"]))
{ {
response.set_content(R"({"access":"reject"})", JSON_TYPE); response.set_content(R"({"access":"reject"})", JSON_TYPE);
+1 -6
View File
@@ -18,13 +18,8 @@ namespace api
{ {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (!check_body(request)) check_body_empty(request, response, ACCESS_REQUIRED_STRING);
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body); nlohmann::json json_body = nlohmann::json::parse(request.body);
if (!authenticate(json_body["token"])) if (!authenticate(json_body["token"]))
{ {
response.set_content(R"({"access":"reject"})", JSON_TYPE); response.set_content(R"({"access":"reject"})", JSON_TYPE);
+3 -11
View File
@@ -3,6 +3,7 @@
#include <colors.h> #include <colors.h>
#include <types.hpp> #include <types.hpp>
#include <auth/auth.hpp> #include <auth/auth.hpp>
#include <helpers.hpp>
namespace api namespace api
{ {
@@ -12,18 +13,9 @@ namespace api
{ {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) check_body_empty(request, response, ACCESS_REQUIRED_STRING);
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body); nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) check_body_data(json_body, "token", response, ACCESS_REQUIRED_STRING);
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
if (authenticate(json_body["token"])) if (authenticate(json_body["token"]))
{ {
+3 -10
View File
@@ -6,6 +6,7 @@
#include <iostream> #include <iostream>
#include <json.hpp> #include <json.hpp>
#include <pthread.h> #include <pthread.h>
#include <helpers.hpp>
#define DEFAULT_TMP_FILE_NAME "/tmp/image" #define DEFAULT_TMP_FILE_NAME "/tmp/image"
#define DEFAULT_DST_FILE_NAME "/images/image" #define DEFAULT_DST_FILE_NAME "/images/image"
@@ -68,17 +69,9 @@ namespace api
{ {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) check_body_empty(request, response, ACCESS_REQUIRED_STRING);
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body); nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) check_body_data(json_body, "token", response, ACCESS_REQUIRED_STRING);
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
if (!authenticate(json_body["token"])) if (!authenticate(json_body["token"]))
{ {
response.set_content(R"({"access":"reject"})", JSON_TYPE); response.set_content(R"({"access":"reject"})", JSON_TYPE);