Older/WebApplication/WebApplication.cpp
amass 059d6d3879
Some checks failed
Deploy / Build (push) Failing after 3m33s
use wt dbo.
2024-11-26 22:58:54 +08:00

67 lines
2.7 KiB
C++

#include "WebApplication.h"
#include "BoostLog.h"
#include "Database/Session.h"
#include "Hello.h"
#include "Restful.h"
#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>
#include <Wt/Dbo/FixedSqlConnectionPool.h>
#include <Wt/Dbo/SqlConnectionPool.h>
#include <Wt/Dbo/backend/Sqlite3.h>
#include <Wt/WServer.h>
#include <format>
WebApplication::WebApplication(uint16_t port, const std::string &documentRoot) {
try {
std::vector<std::string> args;
args.push_back(std::format("--docroot={};/resources", documentRoot));
args.push_back(std::format("--approot={}/resources", documentRoot));
args.push_back(std::format("--http-listen=127.0.0.1:{}", port));
initializeAuthenticationService();
m_server = std::make_unique<Wt::WServer>(std::format("{}/resources", documentRoot), args);
m_server->addEntryPoint(Wt::EntryPointType::Application,
std::bind(&WebApplication::createApplication, this, std::placeholders::_1, false));
m_server->addEntryPoint(Wt::EntryPointType::WidgetSet,
std::bind(&WebApplication::createApplication, this, std::placeholders::_1, true), "/wt/app.js");
m_server->addResource(std::make_shared<AuthenticationResource>(), "/auth");
m_server->addResource(std::make_shared<PlaintextResource>(), "/plaintext");
m_server->addResource(std::make_shared<DbResource>(std::format("{}/database.sqlite", documentRoot)), "/db");
m_server->start();
} catch (const std::exception &e) {
LOG(error) << e.what();
}
}
std::unique_ptr<Wt::WApplication> WebApplication::createApplication(const Wt::WEnvironment &env, bool embedded) {
return std::make_unique<Hello>(env, embedded);
}
WebApplication::~WebApplication() {
}
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;
}
const Wt::Auth::PasswordService &WebApplication::passwordService() {
return *m_passwordService;
}