view specific posts

This commit is contained in:
2026-07-20 20:11:06 +10:00
parent 94b5365a1c
commit eff5313f26
5 changed files with 52 additions and 6 deletions

View File

@@ -2,5 +2,6 @@
bin2cpp --file=client/footer.html --output=server/src/generated
bin2cpp --file=client/header.html --output=server/src/generated
bin2cpp --file=client/e404.html --output=server/src/generated
bin2cpp --file=client/style.css --output=server/src/generated
bin2cpp --file=client/script.js --output=server/src/generated

11
client/e404.html Normal file
View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>404</title>
<link rel="stylesheet" type="text/css" href="/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>404 lmao</h1>
</body>
</html>

View File

@@ -1,5 +1,4 @@
function like(id) {
async function like(id) {
const usernameBox = document.getElementById('username');
const passwordBox = document.getElementById('password');
@@ -8,8 +7,12 @@ function like(id) {
formData.append('password', passwordBox.value);
formData.append('post', id);
fetch('/like', {
method: 'POST',
body: formData
const result = await fetch('/like', {
method: 'POST',
body: formData
});
if (result.status < 200 || result.status >= 400) {
alert("your post didnt get the like because the server thought your opinion was invalid");
}
}

View File

@@ -4,6 +4,7 @@ sources = [
'src/main.cpp',
'src/post.cpp',
'src/db.cpp',
'src/generated/e404.cpp',
'src/generated/header.cpp',
'src/generated/footer.cpp',
'src/generated/style.cpp',

View File

@@ -1,5 +1,4 @@
#include <cstdint>
#include <iostream>
#include <mutex>
#include <sstream>
#include <stdexcept>
@@ -12,6 +11,7 @@
#include "db.h"
#include "post.h"
#include "generated/e404.h"
#include "generated/header.h"
#include "generated/footer.h"
#include "generated/style.h"
@@ -20,11 +20,13 @@
int main() {
const bin2cpp::File& headerfile = bin2cpp::getHeaderHtmlFile();
const bin2cpp::File& footerfile = bin2cpp::getFooterHtmlFile();
const bin2cpp::File& e404file = bin2cpp::getE404HtmlFile();
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 e404{e404file.getBuffer(), e404file.getSize()};
std::string style{stylefile.getBuffer(), stylefile.getSize()};
std::string script{scriptfile.getBuffer(), scriptfile.getSize()};
@@ -61,6 +63,31 @@ int main() {
response.set_content(ss.str(), "text/html");
} 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");
}
});
svr.Get("/posts/:id", [&header, &footer, &e404, &database, &data_mutex](const httplib::Request& request, httplib::Response& response) {
std::string postId = request.path_params.at("id");
try {
uint64_t postIdNum = std::stoll(postId);
std::optional<Post> post = database.getPost(postIdNum);
if (!post.has_value()) {
response.status = 404;
response.set_content(e404, "text/html");
return;
}
std::stringstream ss;
ss << header << post->genHtml() << footer;
response.set_content(ss.str(), "text/html");
} 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");
}
});
@@ -98,6 +125,7 @@ int main() {
database.addPost(Post(post, userId));
response.set_redirect("/");
} 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");
}
@@ -114,8 +142,10 @@ int main() {
uint64_t postNum = std::stoull(post);
database.addLike(postNum, 0);
} 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");
}
});