keep working on the like button

This commit is contained in:
2026-07-20 19:02:04 +10:00
parent 7d0727a7aa
commit 6241824960
9 changed files with 67 additions and 4 deletions

View File

@@ -1,3 +1,4 @@
#include <cstdint>
#include <iostream>
#include <mutex>
#include <sstream>
@@ -14,15 +15,18 @@
#include "generated/header.h"
#include "generated/footer.h"
#include "generated/style.h"
#include "generated/script.h"
int main() {
const bin2cpp::File& headerfile = bin2cpp::getHeaderHtmlFile();
const bin2cpp::File& footerfile = bin2cpp::getFooterHtmlFile();
const bin2cpp::File& stylefile = bin2cpp::getStyleCssFile();
const bin2cpp::File& scriptfile = bin2cpp::getScriptJsFile();
std::string header{headerfile.getBuffer(), headerfile.getSize()};
std::string footer{footerfile.getBuffer(), footerfile.getSize()};
std::string style{stylefile.getBuffer(), stylefile.getSize()};
std::string script{scriptfile.getBuffer(), scriptfile.getSize()};
Database database{"chookchat.db"};
@@ -34,6 +38,10 @@ int main() {
response.set_content(style, "text/css");
});
svr.Get("/script.js", [&script](const httplib::Request& request, httplib::Response& response) {
response.set_content(script, "text/css");
});
svr.Get("/", [&header, &footer, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::stringstream ss;
@@ -57,7 +65,7 @@ int main() {
}
});
svr.Post("/make_post", [&header, &footer, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
svr.Post("/make_post", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::string username = request.get_param_value("username");
std::string password = request.get_param_value("password");
@@ -90,11 +98,26 @@ int main() {
database.addPost(Post(post, userId));
response.set_redirect("/");
} catch (const std::runtime_error& e) {
response.set_content("<p>there was an error :( it is: " +std::string(e.what()) + "</p>", "text/html");
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
svr.Post("/like", [&database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::string username = request.get_param_value("username");
std::string password = request.get_param_value("password");
std::string post = request.get_param_value("post");
std::lock_guard<std::mutex> lock(data_mutex);
try {
uint64_t postNum = std::stoull(post);
database.addLike(postNum, 0);
} catch (const std::runtime_error& e) {
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
svr.listen("0.0.0.0", 8080);
}