From d37cf655dc7616b0fb3f1bae5cbe8feaa3a7a182 Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Thu, 14 Nov 2024 23:06:22 +0800 Subject: [PATCH] clean code. --- Dockerfiles/ubuntu2204.dockerfile | 2 ++ WebApplication/Hello.cpp | 9 +++++--- WebApplication/Hello.h | 2 +- WebApplication/Restful.cpp | 6 ++++++ WebApplication/Restful.h | 4 ++++ WebApplication/Session.cpp | 16 ++++++++------ WebApplication/Session.h | 4 +++- WebApplication/WebApplication.cpp | 35 +++++++++++++++++++++---------- WebApplication/WebApplication.h | 11 +++++++--- resources/build.sh | 5 ----- 10 files changed, 64 insertions(+), 30 deletions(-) diff --git a/Dockerfiles/ubuntu2204.dockerfile b/Dockerfiles/ubuntu2204.dockerfile index 4759e28..3735368 100644 --- a/Dockerfiles/ubuntu2204.dockerfile +++ b/Dockerfiles/ubuntu2204.dockerfile @@ -45,6 +45,8 @@ RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master && git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k \ && sed -i 's/ZSH_THEME=".*"/ZSH_THEME="powerlevel10k\/powerlevel10k"/' /root/.zshrc \ && sed -i 's/plugins=(.*)/plugins=(git z zsh-autosuggestions zsh-syntax-highlighting)/' /root/.zshrc \ + && sed -i 's/^setopt share_history/# setopt share_history/' ~/.oh-my-zsh/lib/history.zsh \ + && echo 'setopt no_share_history' >> ~/.oh-my-zsh/lib/history.zsh \ && chsh -s /bin/zsh root RUN cd /root \ diff --git a/WebApplication/Hello.cpp b/WebApplication/Hello.cpp index 14b05bf..59a564c 100644 --- a/WebApplication/Hello.cpp +++ b/WebApplication/Hello.cpp @@ -14,9 +14,10 @@ #include #include -Hello::Hello(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env) { +Hello::Hello(const Wt::WEnvironment &env, Wt::Dbo::SqlConnectionPool &connectionPool, bool embedded) + : Wt::WApplication(env) { LOG(info) << "app root: " << appRoot(); - m_session = std::make_unique(appRoot() + "auth.db"); + m_session = std::make_unique(connectionPool); m_session->login().changed().connect(this, &Hello::authEvent); setTitle("Hello world"); setTheme(std::make_shared()); @@ -59,7 +60,9 @@ Hello::Hello(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env) m_nameEdit->enterPressed().connect(this, &Hello::greet); auto app = Amass::Singleton::instance(); - auto authWidget = std::make_unique(app->authService(), m_session->users(), m_session->login()); + auto authWidget = + std::make_unique(app->authService(), m_session->users(), m_session->login()); + authWidget-> setInternalBasePath("/"); authWidget->model()->addPasswordAuth(&app->passwordService()); authWidget->setRegistrationEnabled(true); authWidget->processEnvironment(); diff --git a/WebApplication/Hello.h b/WebApplication/Hello.h index 735c5c4..3ec76f7 100644 --- a/WebApplication/Hello.h +++ b/WebApplication/Hello.h @@ -7,7 +7,7 @@ class Session; class Hello : public Wt::WApplication { public: - Hello(const Wt::WEnvironment &env, bool embedded); + Hello(const Wt::WEnvironment &env,Wt::Dbo::SqlConnectionPool &connectionPool, bool embedded); ~Hello(); protected: diff --git a/WebApplication/Restful.cpp b/WebApplication/Restful.cpp index 1ba460a..8763c35 100644 --- a/WebApplication/Restful.cpp +++ b/WebApplication/Restful.cpp @@ -1,4 +1,5 @@ #include "Restful.h" +#include "Session.h" #include #include #include @@ -9,6 +10,7 @@ DBO_INSTANTIATE_TEMPLATES(MyMessage) DbStruct *m_dbStruct; void AuthenticationResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) { + Session session(m_connectionPool); response.setMimeType("application/json"); response.addHeader("Server", "Wt"); @@ -46,6 +48,10 @@ int DbStruct::rand() { return distribution(rng); } +AuthenticationResource::AuthenticationResource(Wt::Dbo::SqlConnectionPool &connectionPool) + : m_connectionPool(connectionPool) { +} + DbStruct::DbStruct(const std::string &db) : rng(clock()), distribution(1, 10000) { session.setConnection(std::make_unique(db)); session.mapClass("world"); diff --git a/WebApplication/Restful.h b/WebApplication/Restful.h index ec9b368..9e26a03 100644 --- a/WebApplication/Restful.h +++ b/WebApplication/Restful.h @@ -46,7 +46,11 @@ struct DbStruct { class AuthenticationResource : public Wt::WResource { public: + AuthenticationResource(Wt::Dbo::SqlConnectionPool &connectionPool); void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) final; + +private: + Wt::Dbo::SqlConnectionPool &m_connectionPool; }; class PlaintextResource : public Wt::WResource { diff --git a/WebApplication/Session.cpp b/WebApplication/Session.cpp index efc0dfe..a614d9e 100644 --- a/WebApplication/Session.cpp +++ b/WebApplication/Session.cpp @@ -10,12 +10,8 @@ #include #include -Session::Session(const std::string &sqliteDb) { - auto connection = std::make_unique(sqliteDb); - - connection->setProperty("show-queries", "true"); - - setConnection(std::move(connection)); +Session::Session(Wt::Dbo::SqlConnectionPool &connectionPool) : m_connectionPool(connectionPool) { + setConnectionPool(m_connectionPool); mapClass("user"); mapClass("auth_info"); @@ -35,6 +31,14 @@ Session::Session(const std::string &sqliteDb) { Session::~Session() { } +Wt::Dbo::ptr Session::user() const { + if (m_login.loggedIn()) { + Wt::Dbo::ptr authInfo = m_users->find(m_login.user()); + return authInfo->user(); + } else + return Wt::Dbo::ptr(); +} + Wt::Auth::AbstractUserDatabase &Session::users() { return *m_users; } diff --git a/WebApplication/Session.h b/WebApplication/Session.h index 6b65dd9..f758351 100644 --- a/WebApplication/Session.h +++ b/WebApplication/Session.h @@ -9,12 +9,14 @@ using UserDatabase = Wt::Auth::Dbo::UserDatabase; class Session : public Wt::Dbo::Session { public: - Session(const std::string &sqliteDb); + Session(Wt::Dbo::SqlConnectionPool &connectionPool); ~Session(); + Wt::Dbo::ptr user() const; Wt::Auth::AbstractUserDatabase &users(); Wt::Auth::Login &login(); private: + Wt::Dbo::SqlConnectionPool &m_connectionPool; std::unique_ptr m_users; Wt::Auth::Login m_login; }; diff --git a/WebApplication/WebApplication.cpp b/WebApplication/WebApplication.cpp index 360200f..bc43cb5 100644 --- a/WebApplication/WebApplication.cpp +++ b/WebApplication/WebApplication.cpp @@ -8,17 +8,11 @@ #include #include #include +#include #include +#include #include -static std::unique_ptr createApplication(const Wt::WEnvironment &env) { - return std::make_unique(env, false); -} - -static std::unique_ptr createWidgetSet(const Wt::WEnvironment &env) { - return std::make_unique(env, true); -} - WebApplication::WebApplication() { try { std::vector args; @@ -27,10 +21,17 @@ WebApplication::WebApplication() { args.push_back("--http-listen=127.0.0.1:8855"); // --docroot=. --no-compression --http-listen 127.0.0.1:8855 initializeAuthenticationService(); + m_server = std::make_unique("./build", args); - m_server->addEntryPoint(Wt::EntryPointType::Application, createApplication, "/hello"); - m_server->addEntryPoint(Wt::EntryPointType::WidgetSet, createWidgetSet, "/gui/hello.js"); - m_server->addResource(std::make_shared(), "/auth"); + m_sqlConnectionPool = createConnectionPool(m_server->appRoot() + "database.sqlite"); + + m_server->addEntryPoint(Wt::EntryPointType::Application, + std::bind(&WebApplication::createApplication, this, std::placeholders::_1, false), + "/hello"); + m_server->addEntryPoint(Wt::EntryPointType::WidgetSet, + std::bind(&WebApplication::createApplication, this, std::placeholders::_1, true), + "/gui/hello.js"); + m_server->addResource(std::make_shared(*m_sqlConnectionPool), "/auth"); m_server->addResource(std::make_shared(), "/plaintext"); m_server->addResource(std::make_shared("database.sqlite"), "/db"); @@ -40,6 +41,18 @@ WebApplication::WebApplication() { } } +std::unique_ptr WebApplication::createConnectionPool(const std::string &sqlite3) { + auto connection = std::make_unique(sqlite3); + connection->setProperty("show-queries", "true"); + connection->setDateTimeStorage(Wt::Dbo::SqlDateTimeType::DateTime, + Wt::Dbo::backend::DateTimeStorage::PseudoISO8601AsText); + return std::make_unique(std::move(connection), 10); +} + +std::unique_ptr WebApplication::createApplication(const Wt::WEnvironment &env, bool embedded) { + return std::make_unique(env, *m_sqlConnectionPool, embedded); +} + WebApplication::~WebApplication() { } diff --git a/WebApplication/WebApplication.h b/WebApplication/WebApplication.h index 777c1b2..23400f1 100644 --- a/WebApplication/WebApplication.h +++ b/WebApplication/WebApplication.h @@ -1,11 +1,13 @@ #ifndef __WEBAPPLICATION_H__ #define __WEBAPPLICATION_H__ -#include #include "Singleton.h" +#include namespace Wt { class WServer; +class WApplication; +class WEnvironment; namespace Dbo { class SqlConnectionPool; @@ -19,7 +21,8 @@ class PasswordService; }; // namespace Wt class WebApplication { - friend class Amass::Singleton; + friend class Amass::Singleton; + public: ~WebApplication(); @@ -29,10 +32,12 @@ public: protected: WebApplication(); + static std::unique_ptr createConnectionPool(const std::string &sqlite3); + std::unique_ptr createApplication(const Wt::WEnvironment &env, bool embedded); private: std::unique_ptr m_server; - std::unique_ptr m_blogSqlConnectionPool; + std::unique_ptr m_sqlConnectionPool; std::unique_ptr m_authService; std::unique_ptr m_passwordService; diff --git a/resources/build.sh b/resources/build.sh index fa27e38..5141233 100755 --- a/resources/build.sh +++ b/resources/build.sh @@ -35,11 +35,6 @@ function build() { if [ $? -ne 0 ]; then exit 1 fi - cp WebApplication/blog.xml ./build/ - mkdir -p ${build_path}/css; cp WebApplication/blog.css ./build/css/ - cp WebApplication/rss.png ./build/css/ - cp WebApplication/blogexample.css ./build/css/ - cp WebApplication/asciidoc.css ./build/css/ build/UnitTest/UnitTest }