view specific posts
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user