add code.
All checks were successful
Deploy / PullDocker (push) Successful in 8s
Deploy / Build (push) Successful in 2m46s

This commit is contained in:
luocai 2024-10-31 19:40:29 +08:00
parent 5d35522a19
commit 1eaa4a6787
9 changed files with 177 additions and 7 deletions

View File

@ -1,6 +1,8 @@
add_library(WebApplication
WebApplication.h WebApplication.cpp
Hello.h Hello.cpp
Session.h Session.cpp
User.h User.cpp
)
target_include_directories(WebApplication
@ -16,5 +18,7 @@ target_link_libraries(WebApplication
PUBLIC wt
PUBLIC wttest
PUBLIC wthttp
PUBLIC wtdbo
PUBLIC wtdbosqlite3
PUBLIC Universal
)

View File

@ -1,5 +1,11 @@
#include "Hello.h"
#include "BoostLog.h"
#include "Session.h"
#include <Wt/Auth/AuthService.h>
#include <Wt/Auth/AuthWidget.h>
#include <Wt/Auth/Identity.h>
#include <Wt/Auth/PasswordService.h>
#include <Wt/WBootstrap2Theme.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WEnvironment.h>
#include <Wt/WLineEdit.h>
@ -7,7 +13,11 @@
#include <Wt/WText.h>
Hello::Hello(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env) {
LOG(info) << "app root: " << appRoot();
m_session = std::make_unique<Session>(appRoot() + "auth.db");
m_session->login().changed().connect(this, &Hello::authEvent);
setTitle("Hello world");
setTheme(std::make_shared<Wt::WBootstrap2Theme>());
Wt::WContainerWidget *top = nullptr;
if (!embedded) {
top = root();
@ -45,8 +55,27 @@ Hello::Hello(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env)
b->clicked().connect(this, &Hello::greet);
m_nameEdit->enterPressed().connect(this, &Hello::greet);
auto authWidget = std::make_unique<Wt::Auth::AuthWidget>(Session::auth(), m_session->users(), m_session->login());
authWidget->model()->addPasswordAuth(&Session::passwordAuth());
authWidget->setRegistrationEnabled(true);
authWidget->processEnvironment();
top->addWidget(std::move(authWidget));
}
Hello::~Hello() {
}
void Hello::greet() {
m_greeting->setText("Hello there, " + m_nameEdit->text());
}
void Hello::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.";
}
}

View File

@ -3,16 +3,21 @@
#include <Wt/WApplication.h>
class Session;
class Hello : public Wt::WApplication {
public:
Hello(const Wt::WEnvironment &env, bool embedded);
~Hello();
protected:
void greet();
void authEvent();
private:
Wt::WLineEdit *m_nameEdit = nullptr;
Wt::WText *m_greeting = nullptr;
std::unique_ptr<Session> m_session;
};
#endif // __HELLO_H__

View File

@ -0,0 +1,80 @@
#include "Session.h"
#include "BoostLog.h"
#include <Wt/Auth/Dbo/AuthInfo.h>
#include <Wt/Auth/Dbo/UserDatabase.h>
#include <Wt/Auth/FacebookService.h>
#include <Wt/Auth/GoogleService.h>
#include <Wt/Auth/HashFunction.h>
#include <Wt/Auth/PasswordService.h>
#include <Wt/Auth/PasswordStrengthValidator.h>
#include <Wt/Auth/PasswordVerifier.h>
#include <Wt/Dbo/backend/Sqlite3.h>
Wt::Auth::AuthService myAuthService;
Wt::Auth::PasswordService myPasswordService(myAuthService);
std::vector<std::unique_ptr<Wt::Auth::OAuthService>> myOAuthServices;
Session::Session(const std::string &sqliteDb) {
auto connection = std::make_unique<Wt::Dbo::backend::Sqlite3>(sqliteDb);
connection->setProperty("show-queries", "true");
setConnection(std::move(connection));
mapClass<User>("user");
mapClass<AuthInfo>("auth_info");
mapClass<AuthInfo::AuthIdentityType>("auth_identity");
mapClass<AuthInfo::AuthTokenType>("auth_token");
try {
createTables();
LOG(info) << "Created database.";
} catch (Wt::Dbo::Exception &e) {
LOG(error) << e.what() << ", using existing database";
}
m_users = std::make_unique<UserDatabase>(*this);
}
Session::~Session() {
}
Wt::Auth::AbstractUserDatabase &Session::users() {
return *m_users;
}
Wt::Auth::Login &Session::login() {
return m_login;
}
void Session::configureAuth() {
myAuthService.setAuthTokensEnabled(true, "logincookie");
// myAuthService.setEmailVerificationEnabled(true);
// myAuthService.setEmailVerificationRequired(true);
auto verifier = std::make_unique<Wt::Auth::PasswordVerifier>();
verifier->addHashFunction(std::make_unique<Wt::Auth::BCryptHashFunction>(7));
myPasswordService.setVerifier(std::move(verifier));
myPasswordService.setPasswordThrottle(std::make_unique<Wt::Auth::AuthThrottle>());
myPasswordService.setStrengthValidator(std::make_unique<Wt::Auth::PasswordStrengthValidator>());
if (Wt::Auth::GoogleService::configured()) {
myOAuthServices.push_back(std::make_unique<Wt::Auth::GoogleService>(myAuthService));
}
if (Wt::Auth::FacebookService::configured()) {
myOAuthServices.push_back(std::make_unique<Wt::Auth::FacebookService>(myAuthService));
}
for (const auto &oAuthService : myOAuthServices) {
oAuthService->generateRedirectEndpoint();
}
}
const Wt::Auth::AuthService &Session::auth() {
return myAuthService;
}
const Wt::Auth::PasswordService &Session::passwordAuth() {
return myPasswordService;
}

25
WebApplication/Session.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef __SESSION_H__
#define __SESSION_H__
#include "User.h"
#include <Wt/Auth/Login.h>
#include <Wt/Dbo/Session.h>
using UserDatabase = Wt::Auth::Dbo::UserDatabase<AuthInfo>;
class Session : public Wt::Dbo::Session {
public:
Session(const std::string &sqliteDb);
~Session();
Wt::Auth::AbstractUserDatabase &users();
Wt::Auth::Login &login();
static void configureAuth();
static const Wt::Auth::AuthService &auth();
static const Wt::Auth::PasswordService &passwordAuth();
private:
std::unique_ptr<UserDatabase> m_users;
Wt::Auth::Login m_login;
};
#endif // __SESSION_H__

4
WebApplication/User.cpp Normal file
View File

@ -0,0 +1,4 @@
#include "User.h"
#include <Wt/Dbo/Impl.h>
DBO_INSTANTIATE_TEMPLATES(User)

22
WebApplication/User.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef __USER_H__
#define __USER_H__
#include <Wt/Dbo/Types.h>
#include <Wt/WGlobal.h>
class User;
using AuthInfo = Wt::Auth::Dbo::AuthInfo<User>;
class User {
public:
template <class Action>
void persist(Action &a) {
// Wt::Dbo::field(a, name, "name");
}
// std::string name;
// int age;
};
DBO_EXTERN_TEMPLATES(User)
#endif // __USER_H__

View File

@ -1,6 +1,7 @@
#include "WebApplication.h"
#include "BoostLog.h"
#include "Hello.h"
#include "Session.h"
#include <Wt/WServer.h>
static std::unique_ptr<Wt::WApplication> createApplication(const Wt::WEnvironment &env) {
@ -20,9 +21,9 @@ WebApplication::WebApplication() {
m_server = std::make_unique<Wt::WServer>("./build", args);
m_server->addEntryPoint(Wt::EntryPointType::Application, createApplication);
m_server->addEntryPoint(Wt::EntryPointType::WidgetSet, createWidgetSet, "/gui/hello.js");
Session::configureAuth();
m_thread = std::thread(&WebApplication::run, this);
} catch (const std::exception &e) {
LOG(error) << e.what();
}

View File

@ -2,8 +2,8 @@
set -e
SERVER_ADDRESS=192.168.3.3
SERVER_PORT=5022
SERVER_ADDRESS=172.16.103.87
SERVER_PORT=22
USER=root
function update() {
@ -24,6 +24,10 @@ function init() {
scp -P $SERVER_PORT resource/frps.service $USER@$SERVER_ADDRESS:/etc/systemd/system/frps.service
ssh $USER@$SERVER_ADDRESS -p $SERVER_PORT <<EOF
if [ ! -d /root/Server ]; then
mkdir -p /root/Server /root/Server/logs
fi
if command -v openresty > /dev/null 2>&1; then
echo "OpenResty has installed."
else
@ -58,10 +62,6 @@ function init() {
apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
fi
if [ ! -d /root/Server ]; then
mkdir -p /root/Server /root/Server/logs
fi
if [ "$(docker ps -q -f name=librespeed)" ]; then
echo "librespeed has running."
else