yay initial commit

This commit is contained in:
2026-07-20 10:57:07 +10:00
commit e1751a3ece
18 changed files with 22476 additions and 0 deletions

102
server/src/main.cpp Normal file
View File

@@ -0,0 +1,102 @@
#include <exception>
#include <iostream>
#include <mutex>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
#include "httplib/httplib.h"
#include "bcrypt/bcrypt.h"
#include "db.h"
#include "post.h"
#include "generated/header.h"
#include "generated/footer.h"
int main() {
const bin2cpp::File& headerfile = bin2cpp::getHeaderHtmlFile();
const bin2cpp::File& footerfile = bin2cpp::getFooterHtmlFile();
std::string header{headerfile.getBuffer(), headerfile.getSize()};
std::string footer{footerfile.getBuffer(), footerfile.getSize()};
//std::vector<Post> posts = {};
//std::unordered_map<std::string, std::string> users = {};
Database database{"chookchat.db"};
std::mutex data_mutex;
httplib::Server svr;
svr.Get("/", [&header, &footer, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::cout << "requested /" << std::endl;
std::stringstream ss;
ss << header;
std::lock_guard<std::mutex> lock(data_mutex);
try {
std::vector<Post> posts = database.getTopPosts(100);
for (const auto& post : posts) {
ss << post.genHtml();
}
ss << footer;
response.set_content(ss.str(), "text/html");
} catch (const std::runtime_error& e) {
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
}
});
svr.Post("/make_post", [&header, &footer, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::cout << "making a post at /make_post" << std::endl;
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 userId = 0;
std::optional<User> user = database.getUserByName(username);
if (user.has_value()) {
if (!bcrypt::validatePassword(password, user->passwordHash)) {
// noooo wrong password
response.set_content("<p>wrong password lmao</p>", "text/html");
return;
}
userId = user->id;
} else {
if (username.empty()) {
response.set_content("<p>hey you can't have an empty username!!!!1!!!1! >:(</p>", "text/html");
return;
}
// create user
User newUser{0, username, bcrypt::generateHash(password)};
database.addUser(newUser);
userId = newUser.id;
}
// and now we add their post
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");
}
});
svr.listen("0.0.0.0", 8080);
}