init upload

This commit is contained in:
rami 2024-03-27 16:36:54 -04:00
commit 0b3b63f6b2
7 changed files with 197 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
*.o
server
posts/
crow_all.h
compile_commands.json

20
Makefile Executable file
View File

@ -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)

48
main.cpp Executable file
View File

@ -0,0 +1,48 @@
#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, "/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();
}

View File

65
templates/index.html Executable file
View File

@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<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">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
color: #d3c6aa;
font-family: 'JetBrains Mono', monospace;
}
html, body {
width: 100%;
height: 100%;
}
body {
background: #2d353b;
display: flex;
justify-content: center;
align-items: center;
}
ul {
display: flex;
list-style: none;
}
ul > li {
margin: 5px;
font-size: 15px;
}
#wrapper {
display: flex;
flex-direction: column;
}
</style>
</head>
<body>
<div id="wrapper">
<pre>
_ __ __
_______ ___ _ (_) /__ ___/ /__ _ __
/ __/ _ `/ ' \/ / '_// _ / -_) |/ /
/_/ \_,_/_/_/_/_/_/\_(_)_,_/\__/|___/
</pre>
<ul>
<li>[</li>
<li><a href="#">home</a></li>
<li><a href="#">about</a></li>
<li><a href="#">contact</a></li>
<li><a href="https://git.ramik.dev/rami">git</a></li>
<li><a href="/blog">blog</a></li>
<li>]</li>
</ul>
</div>
</body>
</html>

3
templates/posts.html Executable file
View File

@ -0,0 +1,3 @@
<ul>
{{& posts }}
</ul>

56
templates/template.html Normal file
View File

@ -0,0 +1,56 @@
<!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>
</head>
<body>
<div id="wrapper">
$body$
</div>
</body>
</html>