This commit is contained in:
2026-07-21 19:50:36 +10:00
parent 0965749fc8
commit dd7e1f8981
9 changed files with 372 additions and 35 deletions

View File

@@ -16,6 +16,7 @@
#include "generated/header_bottom.h"
#include "generated/footer.h"
#include "generated/login.h"
#include "generated/settings.h"
#include "generated/style.h"
#include "generated/script.h"
@@ -42,16 +43,18 @@ std::optional<User> getLoggedInUser(const httplib::Request& request, Database& d
int main() {
const bin2cpp::File& headerTopFile = bin2cpp::getHeader_topHtmlFile();
const bin2cpp::File& headerBottomFile = bin2cpp::getHeader_bottomHtmlFile();
const bin2cpp::File& footerfile = bin2cpp::getFooterHtmlFile();
const bin2cpp::File& loginfile = bin2cpp::getLoginHtmlFile();
const bin2cpp::File& e404file = bin2cpp::getE404HtmlFile();
const bin2cpp::File& stylefile = bin2cpp::getStyleCssFile();
const bin2cpp::File& scriptfile = bin2cpp::getScriptJsFile();
const bin2cpp::File& footerfile = bin2cpp::getFooterHtmlFile();
const bin2cpp::File& loginfile = bin2cpp::getLoginHtmlFile();
const bin2cpp::File& settingsfile = bin2cpp::getSettingsHtmlFile();
const bin2cpp::File& e404file = bin2cpp::getE404HtmlFile();
const bin2cpp::File& stylefile = bin2cpp::getStyleCssFile();
const bin2cpp::File& scriptfile = bin2cpp::getScriptJsFile();
std::string headerTop{headerTopFile.getBuffer(), headerTopFile.getSize()};
std::string headerBottom{headerBottomFile.getBuffer(), headerBottomFile.getSize()};
std::string footer{footerfile.getBuffer(), footerfile.getSize()};
std::string login{loginfile.getBuffer(), loginfile.getSize()};
std::string settings{settingsfile.getBuffer(), settingsfile.getSize()};
std::string e404{e404file.getBuffer(), e404file.getSize()};
std::string style{stylefile.getBuffer(), stylefile.getSize()};
std::string script{scriptfile.getBuffer(), scriptfile.getSize()};
@@ -78,6 +81,14 @@ int main() {
response.set_content(login, "text/html");
});
svr.Get("/settings", [&settings](const httplib::Request& request, httplib::Response& response) {
response.set_content(settings, "text/html");
});
svr.Get("/settings.html", [&settings](const httplib::Request& request, httplib::Response& response) {
response.set_content(settings, "text/html");
});
svr.Post("/login", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::string username = request.form.get_field("username");
std::string password = request.form.get_field("password");
@@ -139,7 +150,7 @@ int main() {
response.set_content("<p>hey you can't have an empty username!!!!1!!!1! >:(</p>", "text/html");
return;
}
User newUser{0, username, bcrypt::generateHash(password)};
User newUser{0, username, bcrypt::generateHash(password), ""};
database.addUser(newUser);
user = newUser;
}
@@ -187,6 +198,9 @@ int main() {
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
@@ -228,7 +242,8 @@ int main() {
std::stringstream ss;
ss << headerTop << "<meta content='See posts from " << username << " on Chookchat' property='og:title' />" << headerBottom;
ss << "<div id='posts-header'><h1>" << username << "'s posts</h1></div>";
ss << "<div id='posts-header'><h1>" << username << "</h1></div>";
ss << "<p>" << user->bio << "</p>";
for (const auto& post : posts) {
ss << post.genHtml();
}
@@ -270,7 +285,7 @@ int main() {
return;
}
// create user
User newUser{0, username, bcrypt::generateHash(password)};
User newUser{0, username, bcrypt::generateHash(password), ""};
database.addUser(newUser);
userId = newUser.id;
}
@@ -336,17 +351,134 @@ int main() {
svr.Get("/me", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::lock_guard<std::mutex> lock(data_mutex);
std::optional<User> user = getLoggedInUser(request, database);
response.set_header("Cache-Control", "no-store");
try {
std::optional<User> user = getLoggedInUser(request, database);
response.set_header("Cache-Control", "no-store");
if (!user.has_value()) {
response.set_header("X-Logged-In", "false");
return;
if (!user.has_value()) {
response.set_header("X-Logged-In", "false");
return;
}
response.set_header("X-Logged-In", "true");
response.set_header("X-Username", user->name);
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
response.set_header("X-Logged-In", "true");
response.set_header("X-Username", user->name);
});
// settings endpoints
svr.Post("/settings/changePassword", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::string oldpassword = request.form.get_field("oldpassword");
std::string newpassword = request.form.get_field("newpassword");
std::lock_guard<std::mutex> lock(data_mutex);
try {
std::optional<User> user = getLoggedInUser(request, database);
if (!user.has_value()) {
response.status = 401;
response.set_content("your session is invalid", "text/plain");
return;
}
if (!bcrypt::validatePassword(oldpassword, user->passwordHash)) {
response.status = 400;
response.set_content("invalid password", "text/plain");
return;
}
user->passwordHash = bcrypt::generateHash(newpassword);
database.updateUser(*user);
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
svr.Post("/settings/changeUsername", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::string password = request.form.get_field("password");
std::string newusername = request.form.get_field("newusername");
std::lock_guard<std::mutex> lock(data_mutex);
try {
std::optional<User> user = getLoggedInUser(request, database);
if (!user.has_value()) {
response.status = 401;
response.set_content("your session is invalid", "text/plain");
return;
}
if (!bcrypt::validatePassword(password, user->passwordHash)) {
response.status = 400;
response.set_content("invalid password", "text/plain");
return;
}
user->name = newusername;
database.updateUser(*user);
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
svr.Post("/settings/changeBio", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::string bio = request.form.get_field("bio");
std::lock_guard<std::mutex> lock(data_mutex);
try {
std::optional<User> user = getLoggedInUser(request, database);
if (!user.has_value()) {
response.status = 401;
response.set_content("your session is invalid", "text/plain");
return;
}
user->bio = bio;
database.updateUser(*user);
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
svr.Post("/settings/invalidateAllSessions", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::lock_guard<std::mutex> lock(data_mutex);
try {
std::optional<User> user = getLoggedInUser(request, database);
if (!user.has_value()) {
response.status = 401;
response.set_content("your session is invalid", "text/plain");
return;
}
database.invalidateUserSessions(*user);
} catch (const std::runtime_error& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
} catch (const std::exception& e) {
response.status = 500;
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
svr.listen("0.0.0.0", 8080);