Older/WebApplication/WebApplication.cpp
luocai 8c0128c4e7
Some checks failed
Deploy / PullDocker (push) Failing after 2s
Deploy / Build (push) Successful in 1m59s
Deploy Docker Images / Docusaurus build and Server deploy (push) Successful in 2h4m57s
update docker images.
2024-11-07 18:21:04 +08:00

63 lines
2.2 KiB
C++

#include "WebApplication.h"
#include "BlogApplication.h"
#include "BoostLog.h"
#include "Hello.h"
#include "Restful.h"
#include "Session.h"
#include "model/BlogSession.h"
#include <Wt/Dbo/SqlConnectionPool.h>
#include <Wt/WServer.h>
static std::unique_ptr<Wt::WApplication> createApplication(const Wt::WEnvironment &env) {
return std::make_unique<Hello>(env, false);
}
std::unique_ptr<Wt::WApplication> createBlogApplication(const Wt::WEnvironment &env, Wt::Dbo::SqlConnectionPool *blogDb) {
return std::make_unique<BlogApplication>(env, *blogDb);
}
static std::unique_ptr<Wt::WApplication> createWidgetSet(const Wt::WEnvironment &env) {
return std::make_unique<Hello>(env, true);
}
WebApplication::WebApplication() {
try {
std::vector<std::string> args;
args.push_back("--approot=./build");
args.push_back("--docroot=./build");
args.push_back("--http-listen=127.0.0.1:8855");
// --docroot=. --no-compression --http-listen 127.0.0.1:8855
m_server = std::make_unique<Wt::WServer>("./build", args);
m_server->addEntryPoint(Wt::EntryPointType::Application, createApplication, "/hello");
BlogSession::configureAuth();
m_blogSqlConnectionPool = BlogSession::createConnectionPool(m_server->appRoot() + "database.sqlite");
m_server->addEntryPoint(Wt::EntryPointType::Application, std::bind(&createBlogApplication, std::placeholders::_1, m_blogSqlConnectionPool.get()),
"/blog");
m_server->addEntryPoint(Wt::EntryPointType::WidgetSet, createWidgetSet, "/gui/hello.js");
Session::configureAuth();
m_server->addResource(std::make_shared<JsonResource>(), "/json");
m_server->addResource(std::make_shared<PlaintextResource>(), "/plaintext");
m_server->addResource(std::make_shared<DbResource>("database.sqlite"), "/db");
m_thread = std::thread(&WebApplication::run, this);
} catch (const std::exception &e) {
LOG(error) << e.what();
}
}
WebApplication::~WebApplication() {
if (m_thread.joinable()) {
m_thread.join();
}
}
void WebApplication::run() {
try {
m_server->run();
} catch (const std::exception &e) {
LOG(error) << e.what();
}
}