From 0b3b63f6b2098a867440e1d7aef0da06c94fdef6 Mon Sep 17 00:00:00 2001 From: rami Date: Wed, 27 Mar 2024 16:36:54 -0400 Subject: [PATCH] init upload --- .gitignore | 5 +++ Makefile | 20 ++++++++++++ main.cpp | 48 ++++++++++++++++++++++++++++ templates/css/template.css | 0 templates/index.html | 65 ++++++++++++++++++++++++++++++++++++++ templates/posts.html | 3 ++ templates/template.html | 56 ++++++++++++++++++++++++++++++++ 7 files changed, 197 insertions(+) create mode 100644 .gitignore create mode 100755 Makefile create mode 100755 main.cpp create mode 100644 templates/css/template.css create mode 100755 templates/index.html create mode 100755 templates/posts.html create mode 100644 templates/template.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..681c71a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.o +server +posts/ +crow_all.h +compile_commands.json diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..f0eb8a3 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +CC := g++ +CFLAGS := -Wall -g -std=c++23 +TARGET := server + +LIBPATHS := +LIBS := + +SRCS := $(wildcard *.cpp) +OBJS := $(patsubst %.cpp, %.o, $(SRCS)) + +all: $(TARGET) +$(TARGET): $(OBJS) + $(CC) $(LIBPATHS) $(LIBS) -o $@ $^ +%.o: %.cpp + $(CC) $(CFLAGS) -lpthread -c $< +clean: + rm -rf $(TARGET) *.o +all: run +run: $(TARGET) + ./$(TARGET) diff --git a/main.cpp b/main.cpp new file mode 100755 index 0000000..e56f0fc --- /dev/null +++ b/main.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include "crow_all.h" + +std::string exec(const std::string &cmd) { + std::array buffer; + std::string result; + std::unique_ptr pipe(popen(cmd.c_str(), "r"), pclose); + if (!pipe) { + throw std::runtime_error("popen() failed!"); + } + while (fgets(buffer.data(), static_cast(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, "/blog")([](){ + std::string posts = ""; + std::filesystem::path postsDir("posts"); + for (auto const& entry : std::filesystem::directory_iterator{postsDir}) { + posts.append("
  • "); + posts.append(entry.path().filename()); + posts.append("
  • "); + } + + auto page = crow::mustache::load("posts.html"); + crow::mustache::context ctx( {{"posts", posts}} ); + return page.render(ctx); + }); + + CROW_ROUTE(app, "/blog/")([](const std::string &name){ + return exec("pandoc --standalone --template templates/template.html ./posts/" + name); + }); + + app.port(8080).multithreaded().run(); +} diff --git a/templates/css/template.css b/templates/css/template.css new file mode 100644 index 0000000..e69de29 diff --git a/templates/index.html b/templates/index.html new file mode 100755 index 0000000..9d9512f --- /dev/null +++ b/templates/index.html @@ -0,0 +1,65 @@ + + + + + + + + + Document + + + +
    +
    +                  _ __       __        
    +  _______ ___ _  (_) /__ ___/ /__ _  __
    + / __/ _ `/  ' \/ /  '_// _  / -_) |/ /
    +/_/  \_,_/_/_/_/_/_/\_(_)_,_/\__/|___/ 
    +		
    + +
    + + diff --git a/templates/posts.html b/templates/posts.html new file mode 100755 index 0000000..48a975a --- /dev/null +++ b/templates/posts.html @@ -0,0 +1,3 @@ +
      + {{& posts }} +
    diff --git a/templates/template.html b/templates/template.html new file mode 100644 index 0000000..dbf8d4d --- /dev/null +++ b/templates/template.html @@ -0,0 +1,56 @@ + + + + + + + + + + $title$ + + +
    + $body$ +
    + +