Older/WebApplication/WebApplication.cpp

77 lines
3.4 KiB
C++
Raw Normal View History

2024-10-30 23:59:59 +08:00
#include "WebApplication.h"
#include "BoostLog.h"
#include "Hello.h"
2024-11-01 19:05:20 +08:00
#include "Restful.h"
2024-10-31 19:40:29 +08:00
#include "Session.h"
2024-11-11 19:21:48 +08:00
#include <Wt/Auth/AuthService.h>
#include <Wt/Auth/HashFunction.h>
#include <Wt/Auth/PasswordService.h>
#include <Wt/Auth/PasswordStrengthValidator.h>
#include <Wt/Auth/PasswordVerifier.h>
2024-11-14 23:06:22 +08:00
#include <Wt/Dbo/FixedSqlConnectionPool.h>
2024-11-01 19:05:20 +08:00
#include <Wt/Dbo/SqlConnectionPool.h>
2024-11-14 23:06:22 +08:00
#include <Wt/Dbo/backend/Sqlite3.h>
2024-10-30 23:59:59 +08:00
#include <Wt/WServer.h>
2024-11-20 14:55:56 +08:00
#include <format>
2024-10-30 23:59:59 +08:00
2024-11-20 21:10:31 +08:00
WebApplication::WebApplication(uint16_t port, const std::string &documentRoot) {
2024-10-30 23:59:59 +08:00
try {
std::vector<std::string> args;
2024-11-21 00:03:57 +08:00
args.push_back(std::format("--approot={}/resources", documentRoot));
2024-11-20 21:10:31 +08:00
args.push_back(std::format("--docroot={}", documentRoot));
2024-11-19 23:39:40 +08:00
args.push_back("--config=resources/wt_config.xml");
2024-11-20 14:55:56 +08:00
args.push_back(std::format("--http-listen=127.0.0.1:{}", port));
2024-10-30 23:59:59 +08:00
// --docroot=. --no-compression --http-listen 127.0.0.1:8855
2024-11-11 19:21:48 +08:00
initializeAuthenticationService();
2024-11-14 23:06:22 +08:00
2024-10-30 23:59:59 +08:00
m_server = std::make_unique<Wt::WServer>("./build", args);
2024-11-14 23:06:22 +08:00
m_sqlConnectionPool = createConnectionPool(m_server->appRoot() + "database.sqlite");
m_server->addEntryPoint(Wt::EntryPointType::Application,
2024-11-20 14:55:56 +08:00
std::bind(&WebApplication::createApplication, this, std::placeholders::_1, false), "/hello");
2024-11-14 23:06:22 +08:00
m_server->addEntryPoint(Wt::EntryPointType::WidgetSet,
2024-11-20 14:55:56 +08:00
std::bind(&WebApplication::createApplication, this, std::placeholders::_1, true), "/wt/app.js");
2024-11-14 23:06:22 +08:00
m_server->addResource(std::make_shared<AuthenticationResource>(*m_sqlConnectionPool), "/auth");
2024-11-01 19:05:20 +08:00
m_server->addResource(std::make_shared<PlaintextResource>(), "/plaintext");
m_server->addResource(std::make_shared<DbResource>("database.sqlite"), "/db");
2024-10-30 23:59:59 +08:00
2024-11-11 19:21:48 +08:00
m_server->start();
2024-10-30 23:59:59 +08:00
} catch (const std::exception &e) {
LOG(error) << e.what();
}
}
2024-11-14 23:06:22 +08:00
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");
2024-11-20 14:55:56 +08:00
connection->setDateTimeStorage(Wt::Dbo::SqlDateTimeType::DateTime, Wt::Dbo::backend::DateTimeStorage::PseudoISO8601AsText);
2024-11-14 23:06:22 +08:00
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);
}
2024-10-30 23:59:59 +08:00
WebApplication::~WebApplication() {
2024-11-11 19:21:48 +08:00
}
2024-10-30 23:59:59 +08:00
2024-11-11 19:21:48 +08:00
void WebApplication::initializeAuthenticationService() {
m_authService = std::make_unique<Wt::Auth::AuthService>();
m_authService->setAuthTokensEnabled(true, "logincookie");
m_passwordService = std::make_unique<Wt::Auth::PasswordService>(*m_authService);
auto verifier = std::make_unique<Wt::Auth::PasswordVerifier>();
verifier->addHashFunction(std::make_unique<Wt::Auth::BCryptHashFunction>(7));
m_passwordService->setVerifier(std::move(verifier));
m_passwordService->setPasswordThrottle(std::make_unique<Wt::Auth::AuthThrottle>());
m_passwordService->setStrengthValidator(std::make_unique<Wt::Auth::PasswordStrengthValidator>());
}
const Wt::Auth::AuthService &WebApplication::authService() {
return *m_authService;
2024-10-30 23:59:59 +08:00
}
2024-11-10 18:33:39 +08:00
2024-11-11 19:21:48 +08:00
const Wt::Auth::PasswordService &WebApplication::passwordService() {
return *m_passwordService;
}