ramik_dev/main.cpp
2024-03-27 17:46:21 -04:00

53 lines
1.4 KiB
C++
Executable File

#include <filesystem>
#include <string>
#include <array>
#include <memory>
#include "crow_all.h"
std::string exec(const std::string &cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
int main() {
crow::SimpleApp app;
CROW_ROUTE(app, "/")([](){
return crow::mustache::load_text("index.html");
});
CROW_ROUTE(app, "/contact")([](){
return crow::mustache::load_text("contact.html");
});
CROW_ROUTE(app, "/blog")([](){
std::string posts = "";
std::filesystem::path postsDir("posts");
for (auto const& entry : std::filesystem::directory_iterator{postsDir}) {
posts.append("<a href=\"/blog/");
posts.append(entry.path().filename());
posts.append("\"><li>");
posts.append(entry.path().filename());
posts.append("</li></a>");
}
auto page = crow::mustache::load("posts.html");
crow::mustache::context ctx( {{"posts", posts}} );
return page.render(ctx);
});
CROW_ROUTE(app, "/blog/<string>")([](const std::string &name){
return exec("pandoc --standalone --template templates/template.html ./posts/" + name);
});
app.port(8080).multithreaded().run();
}