From 850d143366971e4a448bd9bc6997d158dea567ea Mon Sep 17 00:00:00 2001 From: root Date: Sat, 1 Mar 2025 14:51:27 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96url=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ServiceLogic.cpp | 23 ++++++++++++++--------- Settings.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++- Settings.h | 6 ++++++ 3 files changed, 68 insertions(+), 10 deletions(-) diff --git a/ServiceLogic.cpp b/ServiceLogic.cpp index 71402fb..53f102e 100644 --- a/ServiceLogic.cpp +++ b/ServiceLogic.cpp @@ -79,20 +79,25 @@ void visitAnalysis() { std::string url; if (root.contains("url")) { url = root["url"].as_string(); - } - auto database = Singleton::instance(); - if (std::filesystem::exists("amass_blog" + url) && (url.find("/我的博客/page") != 0)) { - if (url.size() > 1 && url.back() == '/') { + if (!url.empty() && (url.back() == '/')) { url.pop_back(); } + } - if (root.contains("visitor_uuid") && root.contains("user_agent")) { - auto timestamp = duration_cast(system_clock::now().time_since_epoch()); - auto visitorUuid = std::string(root["visitor_uuid"].as_string()); - auto userAgent = std::string(root["user_agent"].as_string()); - database->upsertVisitRecord(url, visitorUuid, userAgent, timestamp.count()); + auto database = Singleton::instance(); + auto settings = Singleton::instance(); + auto documentRoot = settings->documentRoot(); + if (!url.empty() && (documentRoot.empty() || std::filesystem::exists(documentRoot + url))) { + if (url.find("/我的博客/page") != 0) { // Docusaurus 这个路径过滤掉 + if (root.contains("visitor_uuid") && root.contains("user_agent")) { + auto timestamp = duration_cast(system_clock::now().time_since_epoch()); + auto visitorUuid = std::string(root["visitor_uuid"].as_string()); + auto userAgent = std::string(root["user_agent"].as_string()); + database->upsertVisitRecord(url, visitorUuid, userAgent, timestamp.count()); + } } } + auto urlStats = database->visitorStats(url); auto siteStats = database->siteStats(); diff --git a/Settings.cpp b/Settings.cpp index b8853f8..8c4ece3 100644 --- a/Settings.cpp +++ b/Settings.cpp @@ -1,6 +1,49 @@ #include "Settings.h" +#include "Core/Logger.h" +#include +#include +#include +#include + +constexpr auto SettingsFilePath = "settings.xml"; namespace Older { +Settings::Settings() { + if (!std::filesystem::exists(SettingsFilePath)) { + save(); + } + load(); +} + +void Settings::save() { + using namespace boost::property_tree; + ptree ptree; + ptree.put("Application.Threads", std::thread::hardware_concurrency()); + ptree.put("Application.SqlitePath", m_sqlitePath); + + ptree.put("Application.HttpServer.Address", m_server); + ptree.put("Application.HttpServer.DocumentRoot", m_documentRoot); + ptree.put("Application.HttpServer.DocumentRoot.", "静态网页文件存放位置,为空时网站统计将不判断页面页面是否存在"); + + xml_writer_settings settings('\t', 1); + write_xml(SettingsFilePath, ptree, std::locale(), settings); +} + +void Settings::load() { + using namespace boost::property_tree; + ptree ptree; + try { + read_xml(SettingsFilePath, ptree); + m_sqlitePath = ptree.get("Application.SqlitePath"); + m_threads = ptree.get("Application.Threads"); + + m_server = ptree.get("Application.HttpServer.Address"); + m_documentRoot = ptree.get("Application.HttpServer.DocumentRoot"); + } catch (const xml_parser_error &error) { + LOG(error) << "parse " << SettingsFilePath << " failed: " << error.message(); + } +} + uint32_t Settings::threads() const { return m_threads; } @@ -13,11 +56,15 @@ uint16_t Settings::port() const { return m_port; } +std::string Settings::documentRoot() const { + return m_documentRoot; +} + std::string Settings::live2dModelsRoot() const { return m_live2dModelsRoot; } -std::string Settings::sqlitePath() const{ +std::string Settings::sqlitePath() const { return m_sqlitePath; } diff --git a/Settings.h b/Settings.h index 365d6a7..647462e 100644 --- a/Settings.h +++ b/Settings.h @@ -7,9 +7,14 @@ namespace Older { class Settings { public: + Settings(); + void save(); + void load(); + uint32_t threads() const; std::string server() const; uint16_t port() const; + std::string documentRoot() const; std::string live2dModelsRoot() const; std::string sqlitePath() const; @@ -18,6 +23,7 @@ private: std::string m_server = "127.0.0.1"; uint16_t m_port = 8081; + std::string m_documentRoot; std::string m_live2dModelsRoot = "resources/live2d"; std::string m_sqlitePath = "database.sqlite"; };