170 lines
6.5 KiB
C++
170 lines
6.5 KiB
C++
#include "Application.h"
|
|
#include "BoostLog.h"
|
|
#include "BulmaTheme.h"
|
|
#include "Database/Session.h"
|
|
#include "Dialog.h"
|
|
#include "LoginPage.h"
|
|
#include "Restful.h"
|
|
#include "VisitorRecordsPage.h"
|
|
#include <Wt/Auth/AuthService.h>
|
|
#include <Wt/Auth/HashFunction.h>
|
|
#include <Wt/Auth/Identity.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/WContainerWidget.h>
|
|
#include <Wt/WEnvironment.h>
|
|
#include <Wt/WLineEdit.h>
|
|
#include <Wt/WPushButton.h>
|
|
#include <Wt/WServer.h>
|
|
#include <format>
|
|
|
|
namespace WebToolkit {
|
|
Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env) {
|
|
messageResourceBundle().use(appRoot() + "wt");
|
|
messageResourceBundle().use(appRoot() + "auth_strings");
|
|
messageResourceBundle().use(appRoot() + "auth_css_theme");
|
|
useStyleSheet("/resources/app.css");
|
|
LOG(info) << "app root: " << appRoot();
|
|
m_session = Database::session();
|
|
m_session->login().changed().connect(this, &Application::authEvent);
|
|
setTheme(std::make_shared<BulmaTheme>("bulma", !embedded));
|
|
if (!embedded) {
|
|
// setTheme(std::make_shared<Wt::WBootstrap2Theme>());
|
|
m_root = root();
|
|
} else {
|
|
std::unique_ptr<Wt::WContainerWidget> topPtr = std::make_unique<Wt::WContainerWidget>();
|
|
m_root = topPtr.get();
|
|
const std::string *div = env.getParameter("div");
|
|
if (div) {
|
|
setJavaScriptClass(*div);
|
|
bindWidget(std::move(topPtr), *div);
|
|
} else {
|
|
LOG(error) << "Missing: parameter: 'div'";
|
|
}
|
|
auto externalPath = env.getParameter("path");
|
|
if (externalPath != nullptr) {
|
|
m_externalPath = *externalPath;
|
|
LOG(info) << "external path: " << m_externalPath;
|
|
} else {
|
|
auto parameters = env.getParameterMap();
|
|
for (auto &p : parameters) {
|
|
LOG(info) << p.first;
|
|
}
|
|
}
|
|
LOG(info) << "url: " << url();
|
|
LOG(info) << "relative resources url: " << relativeResourcesUrl();
|
|
LOG(info) << "resources url: " << resourcesUrl();
|
|
}
|
|
|
|
if (!embedded) {
|
|
root()->addWidget(std::make_unique<Wt::WText>(
|
|
"Note: you can also run this application from within <a href=\"hello.html\">a web page</a>."));
|
|
}
|
|
LOG(info) << "internal path: " << internalPath();
|
|
|
|
m_root->addWidget(std::make_unique<Wt::WText>("Your name, please ? "));
|
|
m_nameEdit = m_root->addWidget(std::make_unique<Wt::WLineEdit>());
|
|
m_nameEdit->setFocus();
|
|
|
|
auto b = m_root->addWidget(std::make_unique<Wt::WPushButton>("点击我!"));
|
|
b->setMargin(5, Wt::Side::Left);
|
|
|
|
m_root->addWidget(std::make_unique<Wt::WBreak>());
|
|
|
|
m_greeting = m_root->addWidget(std::make_unique<Wt::WText>());
|
|
|
|
b->clicked().connect(this, &Application::greet);
|
|
m_nameEdit->enterPressed().connect(this, &Application::greet);
|
|
|
|
auto app = Amass::Singleton<Server>::instance();
|
|
|
|
m_root->addWidget(std::make_unique<Dialog>());
|
|
internalPathChanged().connect(this, &Application::handlePathChange);
|
|
handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath);
|
|
}
|
|
|
|
Application::~Application() {
|
|
}
|
|
|
|
void Application::greet() {
|
|
m_greeting->setText("Hello there, " + m_nameEdit->text());
|
|
setInternalPath(m_externalPath);
|
|
}
|
|
|
|
void Application::authEvent() {
|
|
if (m_session->login().loggedIn()) {
|
|
const Wt::Auth::User &u = m_session->login().user();
|
|
LOG(info) << "User " << u.id() << " (" << u.identity(Wt::Auth::Identity::LoginName) << ")"
|
|
<< " logged in.";
|
|
} else {
|
|
LOG(info) << "User logged out.";
|
|
}
|
|
}
|
|
|
|
void Application::handlePathChange(const std::string &path) {
|
|
LOG(info) << "handlePathChange: " << path;
|
|
if (path.starts_with("/wt/login") || path.starts_with("/wt/register")) {
|
|
m_root->clear();
|
|
m_root->setStyleClass("WtCenterContainer");
|
|
m_root->addNew<LoginPage>(m_session->users(), m_session->login());
|
|
} else if (path.starts_with("/wt/visitor/analysis")) {
|
|
m_root->clear();
|
|
m_root->addNew<VisitorRecordsPage>(*m_session);
|
|
}
|
|
}
|
|
|
|
Server::Server(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(&Server::createApplication, this, std::placeholders::_1, false));
|
|
m_server->addEntryPoint(Wt::EntryPointType::WidgetSet,
|
|
std::bind(&Server::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> Server::createApplication(const Wt::WEnvironment &env, bool embedded) {
|
|
return std::make_unique<Application>(env, embedded);
|
|
}
|
|
|
|
Server::~Server() {
|
|
}
|
|
|
|
void Server::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 &Server::authService() {
|
|
return *m_authService;
|
|
}
|
|
|
|
const Wt::Auth::PasswordService &Server::passwordService() {
|
|
return *m_passwordService;
|
|
}
|
|
} // namespace WebToolkit
|