Updated blog

This commit is contained in:
rami 2024-06-15 15:15:13 -04:00
parent 1f9081b540
commit 639c8e8815
14 changed files with 209 additions and 96 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
server server
posts/ posts/
compile_commands.json compile_commands.json
.cache

View File

@ -1,9 +1,9 @@
CC := g++ CC := g++
CFLAGS := -Wall -g -std=c++23 CFLAGS := -Wall -g -std=c++23 -I./include/
TARGET := server TARGET := server
LIBPATHS := LIBPATHS :=
LIBS := LIBS := -lcurl
SRCS := $(wildcard *.cpp) SRCS := $(wildcard *.cpp)
OBJS := $(patsubst %.cpp, %.o, $(SRCS)) OBJS := $(patsubst %.cpp, %.o, $(SRCS))
@ -18,3 +18,6 @@ clean:
all: run all: run
run: $(TARGET) run: $(TARGET)
./$(TARGET) ./$(TARGET)
pch: include/crow_all.h.gch
include/crow_all.h.gch: include/crow_all.h
$(CC) $(CFLAGS) -lpthread -c $< -o $@

1
README.md Normal file
View File

@ -0,0 +1 @@
I'm going to rewrite this in rust when I learn it

View File

@ -1,8 +1,9 @@
#include <crow_all.h>
#include <filesystem> #include <filesystem>
#include <string> #include <string>
#include <array> #include <array>
#include <memory> #include <memory>
#include "crow_all.h" #include <curl/curl.h>
std::string exec(const std::string &cmd) { std::string exec(const std::string &cmd) {
std::array<char, 128> buffer; std::array<char, 128> buffer;
@ -17,36 +18,58 @@ std::string exec(const std::string &cmd) {
return result; return result;
} }
int main() { std::string url_decode(const std::string& encoded) {
crow::SimpleApp app; int output_length;
const auto decoded_value = curl_easy_unescape(nullptr, encoded.c_str(), static_cast<int>(encoded.length()), &output_length);
CROW_ROUTE(app, "/")([](){ std::string result(decoded_value, output_length);
return crow::mustache::load_text("index.html"); curl_free(decoded_value);
}); return result;
}
CROW_ROUTE(app, "/contact")([](){
return crow::mustache::load_text("contact.html"); std::string process_file(const std::filesystem::path &path) {
}); return exec("pandoc --template='templates/post.txt' -t plain '" + path.string() + "'");;
}
CROW_ROUTE(app, "/blog")([](){
std::string posts = ""; namespace route {
std::filesystem::path postsDir("posts"); std::string root() {
for (auto const& entry : std::filesystem::directory_iterator{postsDir}) { return crow::mustache::load_text("index.html");
posts.append("<a href=\"/blog/"); }
posts.append(entry.path().filename());
posts.append("\"><li>"); crow::mustache::rendered_template blog() {
posts.append(entry.path().filename()); std::string posts = "";
posts.append("</li></a>"); std::filesystem::path postsDir("posts");
} std::vector<std::future<std::string>> futures;
for (auto const& entry : std::filesystem::directory_iterator{postsDir}) {
auto page = crow::mustache::load("posts.html"); futures.emplace_back(std::async(std::launch::async, process_file, entry.path()));
crow::mustache::context ctx( {{"posts", posts}} ); }
return page.render(ctx);
});
for (auto& future : futures) {
CROW_ROUTE(app, "/blog/<string>")([](const std::string &name){ posts += future.get();
return exec("pandoc --standalone --template templates/template.html ./posts/" + name); }
});
auto page = crow::mustache::load("posts.html");
app.port(8080).multithreaded().run(); 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("<h1>Not found</h1>");
}
};
int main() {
crow::SimpleApp app;
CROW_ROUTE(app, "/")(route::root);
CROW_ROUTE(app, "/blog")(route::blog);
CROW_ROUTE(app, "/blog/<string>")(route::post);
app.port(8080).multithreaded().run();
} }

BIN
static/blog.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
static/gitea.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

BIN
static/pencil.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

64
static/style.css Normal file
View File

@ -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;
}

BIN
static/tile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

View File

@ -6,14 +6,32 @@
<link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">
<meta name="keywords" content="Rami Kaldany">
<title>ramik.dev</title> <title>ramik.dev</title>
<style> <style>
@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');
* { * {
margin: 0; margin: 0;
padding: 0; padding: 0;
color: #d3c6aa; color: #d3c6aa;
font-family: 'JetBrains Mono', monospace; font-family: 'Ubuntu Mono', monospace;
font-size: 30px;
}
.gitea {
background: url(static/gitea.png);
background-size: 100% 100%;
width: 50px;
height: 27px;
margin: 5px;
}
.blog {
background: url(static/pencil.png);
background-size: 100% 100%;
width: 30px;
height: 30px;
margin: 5px;
} }
html, body { html, body {
@ -34,8 +52,12 @@
} }
ul > li { ul > li {
margin: 5px; margin: 10px;
font-size: 15px; }
ul > li > a {
display: flex;
flex-direction: row;
} }
#wrapper { #wrapper {
@ -55,10 +77,8 @@
<ul> <ul>
<li>[</li> <li>[</li>
<li><a href="#">../</a></li> <li><a href="#">../</a></li>
<li><a href="#">about</a></li> <li><a href="https://git.ramik.dev/rami">git</a></li>
<li><a href="/contact">contact</a></li> <li><a href="/blog">blog</a></li>
<li><a href="https://git.ramik.dev/rami">git</a></li>
<li><a href="/blog">blog</a></li>
<li>]</li> <li>]</li>
</ul> </ul>
</div> </div>

1
templates/post.txt Normal file
View File

@ -0,0 +1 @@
<li><span class="sourceCode">$date$</span>&nbsp;&mdash;&nbsp;<a class="title" href="$path$">$title$</a></li>

View File

@ -1,3 +1,34 @@
<ul> <!doctype html>
{{& posts }} <html lang="en">
</ul> <head>
<meta charset="utf-8">
<meta name="date" content='$date-meta$'>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/static/style.css">
<title>Blog</title>
<style>
ul {
padding: 0;
}
li {
width: 100%;
height: 30px;
list-style: none;
display: flex;
justify-content: left;
align-items: center;
}
</style>
</head>
<body>
<div id="wrapper">
<h1>All my posts: </h1>
<hr>
<ul>
{{& posts }}
</ul>
</div>
</body>
</html>

View File

@ -1,56 +1,25 @@
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="date" content='$date-meta$'> <meta name="date" content='$date$'>
<link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">
<style> <link rel="stylesheet" href="/static/style.css">
::selection { <title>$title$</title>
background: #e67e80;
color: #232a2e;
}
body {
color: #d3c6aa;
font-family: sans-serif;
background: #2d353b;
display: flex;
justify-content: center;
align-items: center;
}
a {
color: #d699b6;
}
.sourceCode code {
font-family: 'JetBrains Mono', monospace;
}
code {
background: #232a2e;
font-family: 'JetBrains Mono', monospace;
overflow-y: scroll;
}
div.sourceCode {
background: #232a2e;
overflow-y: scroll;
padding: 10px;
}
#wrapper {
width: 50%;
height: 100%;
}
</style>
<title>$title$</title>
</head> </head>
<body> <body>
<div id="wrapper"> <div id="wrapper">
$body$ <div class="title">
</div> <a class="back" title="Back" href="/blog">..</a>
<h2>
$title$
</h2>
<span class="date">$date$</span>
</div>
<hr>
$body$
</div>
</body> </body>
</html> </html>