diff --git a/.gitignore b/.gitignore index b33f817..dc5c6fc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ server posts/ compile_commands.json +.cache diff --git a/Makefile b/Makefile index f0eb8a3..4f35430 100755 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ CC := g++ -CFLAGS := -Wall -g -std=c++23 +CFLAGS := -Wall -g -std=c++23 -I./include/ TARGET := server -LIBPATHS := -LIBS := +LIBPATHS := +LIBS := -lcurl SRCS := $(wildcard *.cpp) OBJS := $(patsubst %.cpp, %.o, $(SRCS)) @@ -18,3 +18,6 @@ clean: all: run run: $(TARGET) ./$(TARGET) +pch: include/crow_all.h.gch +include/crow_all.h.gch: include/crow_all.h + $(CC) $(CFLAGS) -lpthread -c $< -o $@ diff --git a/README.md b/README.md new file mode 100644 index 0000000..4b13c63 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +I'm going to rewrite this in rust when I learn it diff --git a/crow_all.h b/include/crow_all.h similarity index 100% rename from crow_all.h rename to include/crow_all.h diff --git a/main.cpp b/main.cpp index f22c8c2..9c60a6c 100755 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,9 @@ +#include #include #include #include #include -#include "crow_all.h" +#include std::string exec(const std::string &cmd) { std::array buffer; @@ -17,36 +18,58 @@ std::string exec(const std::string &cmd) { 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("
  • "); - 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(); +std::string url_decode(const std::string& encoded) { + int output_length; + const auto decoded_value = curl_easy_unescape(nullptr, encoded.c_str(), static_cast(encoded.length()), &output_length); + std::string result(decoded_value, output_length); + curl_free(decoded_value); + return result; +} + +std::string process_file(const std::filesystem::path &path) { + return exec("pandoc --template='templates/post.txt' -t plain '" + path.string() + "'");; +} + +namespace route { + std::string root() { + return crow::mustache::load_text("index.html"); + } + + crow::mustache::rendered_template blog() { + std::string posts = ""; + std::filesystem::path postsDir("posts"); + std::vector> futures; + for (auto const& entry : std::filesystem::directory_iterator{postsDir}) { + futures.emplace_back(std::async(std::launch::async, process_file, entry.path())); + } + + + for (auto& future : futures) { + posts += future.get(); + } + + auto page = crow::mustache::load("posts.html"); + crow::mustache::context ctx( {{"posts", posts}} ); + return page.render(ctx); + } + + std::string post(const std::string &name) { + std::string decoded_name = url_decode(name); + if (std::filesystem::exists("posts/" + decoded_name)) + return exec("pandoc --standalone --template templates/template.html './posts/" + decoded_name + "'"); + else + return std::string("

    Not found

    "); + } +}; + +int main() { + crow::SimpleApp app; + + CROW_ROUTE(app, "/")(route::root); + + CROW_ROUTE(app, "/blog")(route::blog); + + CROW_ROUTE(app, "/blog/")(route::post); + + app.port(8080).multithreaded().run(); } diff --git a/static/blog.png b/static/blog.png new file mode 100644 index 0000000..8ad02e5 Binary files /dev/null and b/static/blog.png differ diff --git a/static/gitea.png b/static/gitea.png new file mode 100644 index 0000000..9dd87b3 Binary files /dev/null and b/static/gitea.png differ diff --git a/static/pencil.png b/static/pencil.png new file mode 100644 index 0000000..24a77a7 Binary files /dev/null and b/static/pencil.png differ diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..f8a7303 --- /dev/null +++ b/static/style.css @@ -0,0 +1,64 @@ +@import url('https://fonts.googleapis.com/css2?family=Ubuntu+Mono:ital,wght@0,400;0,700;1,400;1,700&family=Ubuntu:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap'); + +* { + font-family: "Ubuntu", sans-serif; +} + +::selection { + background: #e67e80; + color: #232a2e; +} + +body { + margin: 0; + padding: 0; + background: url(/static/tile.png); + color: #d3c6aa; + font-family: sans-serif; + display: flex; + justify-content: center; + align-items: center; +} + +a { + color: #d699b6; +} + +.sourceCode code { + font-family: "Ubuntu Mono", monospace; +} + +code { + background: #232a2e; + font-family: "Ubuntu Mono", monospace; + overflow-y: scroll; +} + +.sourceCode { + background: #232a2e; + overflow-y: scroll; + padding: 10px; +} +html,body{min-height: 100vh;} +#wrapper { + width: 50%; + min-height: 100vh; + background: #2d353b; + padding: 0px 30px 0px 30px; + line-height: 1.5; +} + +.title { + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; +} + +.date { + font-family: "Ubuntu Mono", monospace; +} + +.back { + font-size: 20px; +} diff --git a/static/tile.png b/static/tile.png new file mode 100644 index 0000000..05c3466 Binary files /dev/null and b/static/tile.png differ diff --git a/templates/index.html b/templates/index.html index b1fa640..9144152 100755 --- a/templates/index.html +++ b/templates/index.html @@ -6,14 +6,32 @@ - ramik.dev + + +
    +

    All my posts:

    +
    +
      + {{& posts }} +
    +
    + + diff --git a/templates/template.html b/templates/template.html index dbf8d4d..7bf9c1f 100644 --- a/templates/template.html +++ b/templates/template.html @@ -1,56 +1,25 @@ - - - - - - - $title$ + + + + + + + $title$ -
    - $body$ -
    +
    +
    + .. +

    + $title$ +

    + $date$ +
    +
    + $body$ +