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
posts/
compile_commands.json
.cache

View File

@ -1,9 +1,9 @@
CC := g++
CFLAGS := -Wall -g -std=c++23
CFLAGS := -Wall -g -std=c++23 -I./include/
TARGET := server
LIBPATHS :=
LIBS :=
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 $@

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 <string>
#include <array>
#include <memory>
#include "crow_all.h"
#include <curl/curl.h>
std::string exec(const std::string &cmd) {
std::array<char, 128> 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("<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();
std::string url_decode(const std::string& encoded) {
int output_length;
const auto decoded_value = curl_easy_unescape(nullptr, encoded.c_str(), static_cast<int>(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<std::future<std::string>> 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("<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.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">
<meta name="keywords" content="Rami Kaldany">
<title>ramik.dev</title>
<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;
padding: 0;
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 {
@ -34,8 +52,12 @@
}
ul > li {
margin: 5px;
font-size: 15px;
margin: 10px;
}
ul > li > a {
display: flex;
flex-direction: row;
}
#wrapper {
@ -55,10 +77,8 @@
<ul>
<li>[</li>
<li><a href="#">../</a></li>
<li><a href="#">about</a></li>
<li><a href="/contact">contact</a></li>
<li><a href="https://git.ramik.dev/rami">git</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>
</ul>
</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>
{{& posts }}
</ul>
<!doctype html>
<html lang="en">
<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>
<html lang="en">
<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">
<style>
::selection {
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>
<meta charset="utf-8">
<meta name="date" content='$date$'>
<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>$title$</title>
</head>
<body>
<div id="wrapper">
$body$
</div>
<div id="wrapper">
<div class="title">
<a class="back" title="Back" href="/blog">..</a>
<h2>
$title$
</h2>
<span class="date">$date$</span>
</div>
<hr>
$body$
</div>
</body>
</html>