diff --git a/build.sh b/build.sh index 4472992..ed94beb 100644 --- a/build.sh +++ b/build.sh @@ -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 diff --git a/client/e404.html b/client/e404.html new file mode 100644 index 0000000..ce4624d --- /dev/null +++ b/client/e404.html @@ -0,0 +1,11 @@ + + + + 404 + + + + +

404 lmao

+ + diff --git a/client/script.js b/client/script.js index 17357f5..9d37e82 100644 --- a/client/script.js +++ b/client/script.js @@ -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"); + } } diff --git a/server/meson.build b/server/meson.build index 56d98f7..d1163a8 100644 --- a/server/meson.build +++ b/server/meson.build @@ -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', diff --git a/server/src/main.cpp b/server/src/main.cpp index 36c686e..a3eb273 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -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("

there was an error :( it is: " + std::string(e.what()) + "

", "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 = 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("

there was an error :( it is: " + std::string(e.what()) + "

", "text/html"); + } catch (const std::exception& e) { + response.status = 500; response.set_content("

there was an error :( it is: " + std::string(e.what()) + "

", "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("

there was an error :( it is: " + std::string(e.what()) + "

", "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("

there was an error :( it is: " + std::string(e.what()) + "

", "text/html"); } catch (const std::exception& e) { + response.status = 500; response.set_content("

there was an error :( it is: " + std::string(e.what()) + "

", "text/html"); } });