forked from chookspace/chookchat
view specific posts
This commit is contained in:
1
build.sh
1
build.sh
@@ -2,5 +2,6 @@
|
|||||||
|
|
||||||
bin2cpp --file=client/footer.html --output=server/src/generated
|
bin2cpp --file=client/footer.html --output=server/src/generated
|
||||||
bin2cpp --file=client/header.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/style.css --output=server/src/generated
|
||||||
bin2cpp --file=client/script.js --output=server/src/generated
|
bin2cpp --file=client/script.js --output=server/src/generated
|
||||||
|
|||||||
11
client/e404.html
Normal file
11
client/e404.html
Normal 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>
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
function like(id) {
|
async function like(id) {
|
||||||
|
|
||||||
const usernameBox = document.getElementById('username');
|
const usernameBox = document.getElementById('username');
|
||||||
const passwordBox = document.getElementById('password');
|
const passwordBox = document.getElementById('password');
|
||||||
|
|
||||||
@@ -8,8 +7,12 @@ function like(id) {
|
|||||||
formData.append('password', passwordBox.value);
|
formData.append('password', passwordBox.value);
|
||||||
formData.append('post', id);
|
formData.append('post', id);
|
||||||
|
|
||||||
fetch('/like', {
|
const result = await fetch('/like', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: formData
|
body: formData
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (result.status < 200 || result.status >= 400) {
|
||||||
|
alert("your post didnt get the like because the server thought your opinion was invalid");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ sources = [
|
|||||||
'src/main.cpp',
|
'src/main.cpp',
|
||||||
'src/post.cpp',
|
'src/post.cpp',
|
||||||
'src/db.cpp',
|
'src/db.cpp',
|
||||||
|
'src/generated/e404.cpp',
|
||||||
'src/generated/header.cpp',
|
'src/generated/header.cpp',
|
||||||
'src/generated/footer.cpp',
|
'src/generated/footer.cpp',
|
||||||
'src/generated/style.cpp',
|
'src/generated/style.cpp',
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <iostream>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@@ -12,6 +11,7 @@
|
|||||||
#include "db.h"
|
#include "db.h"
|
||||||
#include "post.h"
|
#include "post.h"
|
||||||
|
|
||||||
|
#include "generated/e404.h"
|
||||||
#include "generated/header.h"
|
#include "generated/header.h"
|
||||||
#include "generated/footer.h"
|
#include "generated/footer.h"
|
||||||
#include "generated/style.h"
|
#include "generated/style.h"
|
||||||
@@ -20,11 +20,13 @@
|
|||||||
int main() {
|
int main() {
|
||||||
const bin2cpp::File& headerfile = bin2cpp::getHeaderHtmlFile();
|
const bin2cpp::File& headerfile = bin2cpp::getHeaderHtmlFile();
|
||||||
const bin2cpp::File& footerfile = bin2cpp::getFooterHtmlFile();
|
const bin2cpp::File& footerfile = bin2cpp::getFooterHtmlFile();
|
||||||
|
const bin2cpp::File& e404file = bin2cpp::getE404HtmlFile();
|
||||||
const bin2cpp::File& stylefile = bin2cpp::getStyleCssFile();
|
const bin2cpp::File& stylefile = bin2cpp::getStyleCssFile();
|
||||||
const bin2cpp::File& scriptfile = bin2cpp::getScriptJsFile();
|
const bin2cpp::File& scriptfile = bin2cpp::getScriptJsFile();
|
||||||
|
|
||||||
std::string header{headerfile.getBuffer(), headerfile.getSize()};
|
std::string header{headerfile.getBuffer(), headerfile.getSize()};
|
||||||
std::string footer{footerfile.getBuffer(), footerfile.getSize()};
|
std::string footer{footerfile.getBuffer(), footerfile.getSize()};
|
||||||
|
std::string e404{e404file.getBuffer(), e404file.getSize()};
|
||||||
std::string style{stylefile.getBuffer(), stylefile.getSize()};
|
std::string style{stylefile.getBuffer(), stylefile.getSize()};
|
||||||
std::string script{scriptfile.getBuffer(), scriptfile.getSize()};
|
std::string script{scriptfile.getBuffer(), scriptfile.getSize()};
|
||||||
|
|
||||||
@@ -61,6 +63,31 @@ int main() {
|
|||||||
response.set_content(ss.str(), "text/html");
|
response.set_content(ss.str(), "text/html");
|
||||||
|
|
||||||
} catch (const std::runtime_error& e) {
|
} 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");
|
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));
|
database.addPost(Post(post, userId));
|
||||||
response.set_redirect("/");
|
response.set_redirect("/");
|
||||||
} catch (const std::runtime_error& e) {
|
} 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");
|
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);
|
uint64_t postNum = std::stoull(post);
|
||||||
database.addLike(postNum, 0);
|
database.addLike(postNum, 0);
|
||||||
} catch (const std::runtime_error& e) {
|
} 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");
|
response.set_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
|
||||||
} catch (const std::exception& e) {
|
} 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_content("<p>there was an error :( it is: " + std::string(e.what()) + "</p>", "text/html");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user