From 578313417bfba8bb0f5fec471ea71f9cdd34cd9d Mon Sep 17 00:00:00 2001 From: Winter Hille Date: Mon, 20 May 2024 20:23:38 -0700 Subject: [PATCH] initial --- README.md | 0 client-handler.fnl | 40 ++++++++ main.fnl | 3 + page-handler.fnl | 19 ++++ rpi-os.install | 16 ++++ server.fnl | 16 ++++ style.css | 226 +++++++++++++++++++++++++++++++++++++++++++++ template.html | 22 +++++ utils.fnl | 12 +++ 9 files changed, 354 insertions(+) create mode 100644 README.md create mode 100644 client-handler.fnl create mode 100644 main.fnl create mode 100644 page-handler.fnl create mode 100755 rpi-os.install create mode 100644 server.fnl create mode 100644 style.css create mode 100644 template.html create mode 100644 utils.fnl diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/client-handler.fnl b/client-handler.fnl new file mode 100644 index 0000000..0f32afa --- /dev/null +++ b/client-handler.fnl @@ -0,0 +1,40 @@ +(local utils (require :utils)) + +(local read-pages (. (require :page-handler) :read-pages)) + +(local client-handler {}) + +(fn client-handler.handle-client [client] + (let [request (client:receive :*l)] + + (print (: "Request from: %s:%s" :format (client:getpeername))) + (print request) + (while true + (local request (client:receive :*l)) + (print request) + (if (or (= request "") (not request)) + (lua :break))) + + (var uri (request:match "GET%s+([^%s]+)%s+HTTP")) + (set uri (uri:gsub "^/" "")) + + (var content "") + + (read-pages) + + ;; Maybe use scss instead of css + ;; (uri:match "%.css$") (.. "./" (utils.compile-sass (uri:gsub "%.css$" ".scss"))) + + (set content + (if (= uri "") (. _G.files :main) + (. _G.files uri) (. _G.files uri) + (utils.file-exists? uri) (with-open [file (io.open uri :r)] + (file:read :*a)) + "404 - Not Found")) + + (client:send "HTTP/1.1 200 OK\r\n\r\n") + (client:send content) + + (client:close))) + +client-handler diff --git a/main.fnl b/main.fnl new file mode 100644 index 0000000..f98f9e2 --- /dev/null +++ b/main.fnl @@ -0,0 +1,3 @@ +(set _G.files {}) + +(require :server) diff --git a/page-handler.fnl b/page-handler.fnl new file mode 100644 index 0000000..b8f1431 --- /dev/null +++ b/page-handler.fnl @@ -0,0 +1,19 @@ +(local cmark (require :cmark)) +(local lfs (require :lfs)) + +(local page-handler {}) + +(fn page-handler.read-pages [] + (each [file (lfs.dir :pages)] + (when (and (not= file ".") (not= file "..")) + (local markdown (with-open [markdown-file (io.open (.. :pages/ file) :r)] + (markdown-file:read :*a))) + + (local html-template (with-open [html-template-file (io.open :template.html :r)] + (html-template-file:read :*a))) + + (local html (cmark.markdown_to_html markdown (markdown:len) cmark.OPT_UNSAFE)) + + (tset _G.files (file:match "(.+)%..+") (html-template:format html))))) + +page-handler diff --git a/rpi-os.install b/rpi-os.install new file mode 100755 index 0000000..e0e85e4 --- /dev/null +++ b/rpi-os.install @@ -0,0 +1,16 @@ +#!/bin/sh + +echo "Installing lua..." +sudo apt install lua5.1 liblua5.1-dev + +echo "Installing luarocks..." +sudo apt install luarocks + +echo "Installing dependencies..." +sudo luarocks install cmark +sudo luarocks install luafilesystem +sudo luarocks install lsocket + +echo "Installing fennel..." +sudo curl -o /bin/fennel https://fennel-lang.org/downloads/fennel-1.4.2 +sudo chmod +x /bin/fennel diff --git a/server.fnl b/server.fnl new file mode 100644 index 0000000..29d9f43 --- /dev/null +++ b/server.fnl @@ -0,0 +1,16 @@ +(local socket (require :socket)) + +(local handle-client (. (require :client-handler) :handle-client)) + +(local port 8080) +(local server (socket.tcp)) + +(server:bind "*" port) +(server:listen) + +(while true + (local client (server:accept)) + + (local co (coroutine.create (fn [] (handle-client client)))) + + (coroutine.resume co)) diff --git a/style.css b/style.css new file mode 100644 index 0000000..9e97f9b --- /dev/null +++ b/style.css @@ -0,0 +1,226 @@ +@import url("https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap"); +@import url("https://fonts.googleapis.com/css2?family=Roboto+Condensed:ital,wght@0,100..900;1,100..900&display=swap"); +@import url("https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,100..700;1,100..700&display=swap"); +@import url("https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@100..900&display=swap"); +@import url("https://fonts.googleapis.com/css2?family=Roboto+Flex:opsz,wght@8..144,100..1000&display=swap"); +@import url("https://fonts.googleapis.com/css2?family=Roboto+Serif:ital,opsz,wght@0,8..144,100..900;1,8..144,100..900&display=swap"); + +:root { + --serif-font: "Roboto Serif", serif; + --sans-serif-font: "Roboto", sans-serif; + --monospace-font: "Roboto Mono", monospace; + + --white-10: #ffffff; + --white-20: #ebebeb; + --white-30: #d6d6d6; + --white-40: #c2c2c2; + --white-50: #adadad; + + --black-10: #525252; + --black-20: #3d3d3d; + --black-30: #292929; + --black-40: #141414; + --black-50: #000000; +} + +:root { + --foreground: var(--white-10); + --background: var(--black-50); + + --accent-10: var(--black-10); + --accent-20: var(--black-20); + --accent-30: var(--black-30); + --accent-40: var(--black-40); + --accent-50: var(--white-50); +} + +@media (prefers-color-scheme: light) { + :root { + --foreground: var(--black-50); + --background: var(--white-10); + + --accent-10: var(--white-50); + --accent-20: var(--white-40); + --accent-30: var(--white-30); + --accent-40: var(--white-20); + --accent-50: var(--black-10); + } +} + +body { + display: flex; + flex-direction: column; + box-sizing: border-box; + margin: 0; + background: var(--background); + padding: 1em; + min-height: 100vh; + color: var(--foreground); + font-family: var(--sans-serif-font); +} + +body>header { + position: sticky; + top: 0; + margin-top: -1em; + border-bottom: 0.1em solid var(--accent-50); + background: var(--background); + padding: 0.5em; +} + +body>header>a { + color: var(--foreground); + font-weight: 550; + font-size: 2em; + text-decoration: none; + + &::after { + content: "_"; + } + + &:hover::after { + animation: blink 0.5s alternate infinite; + } +} + +@keyframes blink { + to { + opacity: 0; + } + + from { + opacity: 1; + } +} + +body>.content { + flex: 1; + padding: 0.5em; + overflow-y: hidden; +} + +body>.content>main { + min-width: auto; +} + +body>.content>main { + min-width: 60ch; + max-width: 120ch; + + /* paragraphs */ + p { + margin: 0; + color: var(--accent-50); + } + + p+p { + margin-top: 1em; + } + + /* headings */ + h1, + h2, + h3, + h4, + h5, + h6 { + margin: 0.5em 0; + } + + /* links */ + a { + color: var(--foreground); + text-decoration: none; + + &:hover { + text-decoration: underline; + } + } + + /* images */ + img { + border-radius: 0.26em; + } + + /* block qutoes */ + blockquote { + display: inline-block; + margin: 0 1em; + border-left: 0.2em solid var(--accent-40); + padding-left: 0.5em; + } + + /* lists */ + ul, + ol { + margin: 0; + padding-left: 2em; + color: var(--accent-50); + + li { + margin: 0; + } + + li::marker { + color: var(--foreground); + } + } + + /* horizontal rule */ + hr { + margin: 0; + border: none; + border-bottom: 0.1em solid var(--accent-50); + } + + /* inline code */ + p>code { + display: inline-block; + border-radius: 0.23em; + background: var(--accent-40); + padding: 0.2em; + color: var(--foreground); + font-size: 0.8em; + line-height: 1; + font-family: var(--monospace-font); + } + + /* code blocks */ + p+pre { + margin-top: 1em; + } + + pre { + display: inline-block; + margin: 0; + border-radius: 0.26em; + background: var(--accent-40); + padding: 0.8em; + overflow: hidden; + } + + pre>code { + font-size: 0.8em; + font-family: var(--monospace-font); + } +} + +body>footer { + padding: 0.5em; + color: var(--accent-10); +} + +body>footer>a { + display: inline-block; + margin: -0.2em 0; + border-radius: 0.23em; + background: #42a5f54d; + padding: 0.2em; + color: #42a5f5; + line-height: 1; + text-decoration: none; + + &:hover { + text-decoration: underline; + } +} diff --git a/template.html b/template.html new file mode 100644 index 0000000..da10ddf --- /dev/null +++ b/template.html @@ -0,0 +1,22 @@ + + + + + + + + + +
+ bunbun.dev +
+
+
%s
+
+ + + + diff --git a/utils.fnl b/utils.fnl new file mode 100644 index 0000000..21647bf --- /dev/null +++ b/utils.fnl @@ -0,0 +1,12 @@ +(local utils {}) + +(fn utils.file-exists? [file-path] + (let [file (io.open file-path :r)] + (if file (do + (file:close) true) false))) + +(fn utils.compile-sass [file-path] + (with-open [out (io.popen (.. "sass -q " file-path))] + (out:read :*a))) + +utils