clean code.
Some checks failed
Deploy / Build (push) Failing after 6m49s
Deploy Docker Images / Build dockerfile and Server deploy (push) Successful in 1h12m51s

This commit is contained in:
amass 2024-11-14 23:06:22 +08:00
parent 3392f8a0b3
commit d37cf655dc
10 changed files with 64 additions and 30 deletions

View File

@ -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 \ && 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/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/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 && chsh -s /bin/zsh root
RUN cd /root \ RUN cd /root \

View File

@ -14,9 +14,10 @@
#include <Wt/WPushButton.h> #include <Wt/WPushButton.h>
#include <Wt/WText.h> #include <Wt/WText.h>
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(); LOG(info) << "app root: " << appRoot();
m_session = std::make_unique<Session>(appRoot() + "auth.db"); m_session = std::make_unique<Session>(connectionPool);
m_session->login().changed().connect(this, &Hello::authEvent); m_session->login().changed().connect(this, &Hello::authEvent);
setTitle("Hello world"); setTitle("Hello world");
setTheme(std::make_shared<Wt::WBootstrap2Theme>()); setTheme(std::make_shared<Wt::WBootstrap2Theme>());
@ -59,7 +60,9 @@ Hello::Hello(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env)
m_nameEdit->enterPressed().connect(this, &Hello::greet); m_nameEdit->enterPressed().connect(this, &Hello::greet);
auto app = Amass::Singleton<WebApplication>::instance(); auto app = Amass::Singleton<WebApplication>::instance();
auto authWidget = std::make_unique<Wt::Auth::AuthWidget>(app->authService(), m_session->users(), m_session->login()); auto authWidget =
std::make_unique<Wt::Auth::AuthWidget>(app->authService(), m_session->users(), m_session->login());
authWidget-> setInternalBasePath("/");
authWidget->model()->addPasswordAuth(&app->passwordService()); authWidget->model()->addPasswordAuth(&app->passwordService());
authWidget->setRegistrationEnabled(true); authWidget->setRegistrationEnabled(true);
authWidget->processEnvironment(); authWidget->processEnvironment();

View File

@ -7,7 +7,7 @@ class Session;
class Hello : public Wt::WApplication { class Hello : public Wt::WApplication {
public: public:
Hello(const Wt::WEnvironment &env, bool embedded); Hello(const Wt::WEnvironment &env,Wt::Dbo::SqlConnectionPool &connectionPool, bool embedded);
~Hello(); ~Hello();
protected: protected:

View File

@ -1,4 +1,5 @@
#include "Restful.h" #include "Restful.h"
#include "Session.h"
#include <Wt/Dbo/Impl.h> #include <Wt/Dbo/Impl.h>
#include <Wt/Dbo/Json.h> #include <Wt/Dbo/Json.h>
#include <Wt/Dbo/backend/Sqlite3.h> #include <Wt/Dbo/backend/Sqlite3.h>
@ -9,6 +10,7 @@ DBO_INSTANTIATE_TEMPLATES(MyMessage)
DbStruct *m_dbStruct; DbStruct *m_dbStruct;
void AuthenticationResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) { void AuthenticationResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) {
Session session(m_connectionPool);
response.setMimeType("application/json"); response.setMimeType("application/json");
response.addHeader("Server", "Wt"); response.addHeader("Server", "Wt");
@ -46,6 +48,10 @@ int DbStruct::rand() {
return distribution(rng); return distribution(rng);
} }
AuthenticationResource::AuthenticationResource(Wt::Dbo::SqlConnectionPool &connectionPool)
: m_connectionPool(connectionPool) {
}
DbStruct::DbStruct(const std::string &db) : rng(clock()), distribution(1, 10000) { DbStruct::DbStruct(const std::string &db) : rng(clock()), distribution(1, 10000) {
session.setConnection(std::make_unique<Wt::Dbo::backend::Sqlite3>(db)); session.setConnection(std::make_unique<Wt::Dbo::backend::Sqlite3>(db));
session.mapClass<World>("world"); session.mapClass<World>("world");

View File

@ -46,7 +46,11 @@ struct DbStruct {
class AuthenticationResource : public Wt::WResource { class AuthenticationResource : public Wt::WResource {
public: public:
AuthenticationResource(Wt::Dbo::SqlConnectionPool &connectionPool);
void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) final; void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) final;
private:
Wt::Dbo::SqlConnectionPool &m_connectionPool;
}; };
class PlaintextResource : public Wt::WResource { class PlaintextResource : public Wt::WResource {

View File

@ -10,12 +10,8 @@
#include <Wt/Auth/PasswordVerifier.h> #include <Wt/Auth/PasswordVerifier.h>
#include <Wt/Dbo/backend/Sqlite3.h> #include <Wt/Dbo/backend/Sqlite3.h>
Session::Session(const std::string &sqliteDb) { Session::Session(Wt::Dbo::SqlConnectionPool &connectionPool) : m_connectionPool(connectionPool) {
auto connection = std::make_unique<Wt::Dbo::backend::Sqlite3>(sqliteDb); setConnectionPool(m_connectionPool);
connection->setProperty("show-queries", "true");
setConnection(std::move(connection));
mapClass<User>("user"); mapClass<User>("user");
mapClass<AuthInfo>("auth_info"); mapClass<AuthInfo>("auth_info");
@ -35,6 +31,14 @@ Session::Session(const std::string &sqliteDb) {
Session::~Session() { Session::~Session() {
} }
Wt::Dbo::ptr<User> Session::user() const {
if (m_login.loggedIn()) {
Wt::Dbo::ptr<AuthInfo> authInfo = m_users->find(m_login.user());
return authInfo->user();
} else
return Wt::Dbo::ptr<User>();
}
Wt::Auth::AbstractUserDatabase &Session::users() { Wt::Auth::AbstractUserDatabase &Session::users() {
return *m_users; return *m_users;
} }

View File

@ -9,12 +9,14 @@ using UserDatabase = Wt::Auth::Dbo::UserDatabase<AuthInfo>;
class Session : public Wt::Dbo::Session { class Session : public Wt::Dbo::Session {
public: public:
Session(const std::string &sqliteDb); Session(Wt::Dbo::SqlConnectionPool &connectionPool);
~Session(); ~Session();
Wt::Dbo::ptr<User> user() const;
Wt::Auth::AbstractUserDatabase &users(); Wt::Auth::AbstractUserDatabase &users();
Wt::Auth::Login &login(); Wt::Auth::Login &login();
private: private:
Wt::Dbo::SqlConnectionPool &m_connectionPool;
std::unique_ptr<UserDatabase> m_users; std::unique_ptr<UserDatabase> m_users;
Wt::Auth::Login m_login; Wt::Auth::Login m_login;
}; };

View File

@ -8,17 +8,11 @@
#include <Wt/Auth/PasswordService.h> #include <Wt/Auth/PasswordService.h>
#include <Wt/Auth/PasswordStrengthValidator.h> #include <Wt/Auth/PasswordStrengthValidator.h>
#include <Wt/Auth/PasswordVerifier.h> #include <Wt/Auth/PasswordVerifier.h>
#include <Wt/Dbo/FixedSqlConnectionPool.h>
#include <Wt/Dbo/SqlConnectionPool.h> #include <Wt/Dbo/SqlConnectionPool.h>
#include <Wt/Dbo/backend/Sqlite3.h>
#include <Wt/WServer.h> #include <Wt/WServer.h>
static std::unique_ptr<Wt::WApplication> createApplication(const Wt::WEnvironment &env) {
return std::make_unique<Hello>(env, false);
}
static std::unique_ptr<Wt::WApplication> createWidgetSet(const Wt::WEnvironment &env) {
return std::make_unique<Hello>(env, true);
}
WebApplication::WebApplication() { WebApplication::WebApplication() {
try { try {
std::vector<std::string> args; std::vector<std::string> args;
@ -27,10 +21,17 @@ WebApplication::WebApplication() {
args.push_back("--http-listen=127.0.0.1:8855"); args.push_back("--http-listen=127.0.0.1:8855");
// --docroot=. --no-compression --http-listen 127.0.0.1:8855 // --docroot=. --no-compression --http-listen 127.0.0.1:8855
initializeAuthenticationService(); initializeAuthenticationService();
m_server = std::make_unique<Wt::WServer>("./build", args); m_server = std::make_unique<Wt::WServer>("./build", args);
m_server->addEntryPoint(Wt::EntryPointType::Application, createApplication, "/hello"); m_sqlConnectionPool = createConnectionPool(m_server->appRoot() + "database.sqlite");
m_server->addEntryPoint(Wt::EntryPointType::WidgetSet, createWidgetSet, "/gui/hello.js");
m_server->addResource(std::make_shared<AuthenticationResource>(), "/auth"); 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<AuthenticationResource>(*m_sqlConnectionPool), "/auth");
m_server->addResource(std::make_shared<PlaintextResource>(), "/plaintext"); m_server->addResource(std::make_shared<PlaintextResource>(), "/plaintext");
m_server->addResource(std::make_shared<DbResource>("database.sqlite"), "/db"); m_server->addResource(std::make_shared<DbResource>("database.sqlite"), "/db");
@ -40,6 +41,18 @@ WebApplication::WebApplication() {
} }
} }
std::unique_ptr<Wt::Dbo::SqlConnectionPool> WebApplication::createConnectionPool(const std::string &sqlite3) {
auto connection = std::make_unique<Wt::Dbo::backend::Sqlite3>(sqlite3);
connection->setProperty("show-queries", "true");
connection->setDateTimeStorage(Wt::Dbo::SqlDateTimeType::DateTime,
Wt::Dbo::backend::DateTimeStorage::PseudoISO8601AsText);
return std::make_unique<Wt::Dbo::FixedSqlConnectionPool>(std::move(connection), 10);
}
std::unique_ptr<Wt::WApplication> WebApplication::createApplication(const Wt::WEnvironment &env, bool embedded) {
return std::make_unique<Hello>(env, *m_sqlConnectionPool, embedded);
}
WebApplication::~WebApplication() { WebApplication::~WebApplication() {
} }

View File

@ -1,11 +1,13 @@
#ifndef __WEBAPPLICATION_H__ #ifndef __WEBAPPLICATION_H__
#define __WEBAPPLICATION_H__ #define __WEBAPPLICATION_H__
#include <memory>
#include "Singleton.h" #include "Singleton.h"
#include <memory>
namespace Wt { namespace Wt {
class WServer; class WServer;
class WApplication;
class WEnvironment;
namespace Dbo { namespace Dbo {
class SqlConnectionPool; class SqlConnectionPool;
@ -19,7 +21,8 @@ class PasswordService;
}; // namespace Wt }; // namespace Wt
class WebApplication { class WebApplication {
friend class Amass::Singleton<WebApplication>; friend class Amass::Singleton<WebApplication>;
public: public:
~WebApplication(); ~WebApplication();
@ -29,10 +32,12 @@ public:
protected: protected:
WebApplication(); WebApplication();
static std::unique_ptr<Wt::Dbo::SqlConnectionPool> createConnectionPool(const std::string &sqlite3);
std::unique_ptr<Wt::WApplication> createApplication(const Wt::WEnvironment &env, bool embedded);
private: private:
std::unique_ptr<Wt::WServer> m_server; std::unique_ptr<Wt::WServer> m_server;
std::unique_ptr<Wt::Dbo::SqlConnectionPool> m_blogSqlConnectionPool; std::unique_ptr<Wt::Dbo::SqlConnectionPool> m_sqlConnectionPool;
std::unique_ptr<Wt::Auth::AuthService> m_authService; std::unique_ptr<Wt::Auth::AuthService> m_authService;
std::unique_ptr<Wt::Auth::PasswordService> m_passwordService; std::unique_ptr<Wt::Auth::PasswordService> m_passwordService;

View File

@ -35,11 +35,6 @@ function build() {
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
exit 1 exit 1
fi 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 build/UnitTest/UnitTest
} }