Files
natalieee.net/srv/content/router.hy
2025-05-11 15:36:23 -07:00

118 lines
3.7 KiB
Hy

(require hyrule.destructure [defn+ setv+])
(require hyrule.argmove [doto])
(import content.file-io [parse-html-file send-raw-file])
(import urllib.parse [quote-plus urlparse])
(import os [mkdir])
(import os.path [isdir :as dir? isfile :as file? abspath])
(import datetime [datetime])
(import bleach [clean])
(import validators [domain :as domain?])
(defmacro unless [test #* body]
`(when (not ~test) (do ~@body)))
(defn error [code message]
(return (dict
:code code
:body (parse-html-file "./www/site/html/error.html"))))
(defn+ match-request [{method "method" {path "path"} "route" :as request}]
(when (.startswith path "/assets/")
(when (!= method "GET")
(return (error 405 "method not allowed")))
(setv [headers data] (send-raw-file (+ "./www/site" path)))
(return (dict
:code 200
:headers headers
:body data)))
(when (.startswith path "/html/")
(when (!= method "GET")
(return (error 405 "method not allowed")))
(when (file? (+ "./www/site/html" path))
(unless (= path "/html/view-thought.html")
(return (dict
:code 200
:body (parse-html-file f"./www/site/html{path}"))))
(return (dict
:code 200
:body (parse-html-file "./www/site/html/html/view-thought.html" #** (. request (get "route") (get "parameters")))))))
(when (= path "/style.css")
(when (!= method "GET")
(return (error 405 "method not allowed")))
(setv [headers data] (send-raw-file "./www/site/assets/style.css"))
(return (dict
:code 200
:headers headers
:body data)))
(when (= path "/")
(when (!= method "GET")
(return (error 405 "method not allowed")))
(return (dict
:code 200
:body (parse-html-file "./www/site/html/home.html" :route "/"))))
(when (= path "/test.html")
(when (!= method "GET")
(return (error 405 "method not allowed")))
(return (dict
:code 200
:body (parse-html-file "./www/site/html/test.html" :route "/test.html"))))
(when (= path "/comment")
(cond
(= method "POST") (do
(setv now (datetime.now))
(setv url-comment-dir (quote-plus (. request (get "route") (get "parameters") (get "route")) :safe ""))
(when (not (dir? "./www/data"))
(mkdir "./www/data"))
(when (not (dir? f"./www/data/comments"))
(mkdir "./www/data/comments"))
(when (not (dir? f"./www/data/comments/{url-comment-dir}"))
(mkdir f"./www/data/comments/{url-comment-dir}"))
(setv+ {{comment "comment" name "name" site "site"} "body"} request)
(when (.startswith site "//")
(setv site (.replace site "//" "")))
(setv [protocol domain path #* _] (urlparse
(if (in "://" site)
site
(+ "http://" site))))
(setv site-valid? (= (domain? domain) True))
(when (not protocol)
(setv protocol "//"))
(print protocol domain path site-valid?)
(with [f (open f"./www/data/comments/{url-comment-dir}/{(.strftime now "%Y-%m-%d_%H:%M:%S_%f")}" "w")]
(.write f f"<div class=\"comment\"><span style=\"font-weight: bold\">{(if (and site site-valid?) f"<a href=\"{(clean protocol)}://{(clean domain)}{(clean path)}\">" "")}{(clean name)}{(if (and site site-valid?) "</a>" "")}</span> at {(.strftime now "%Y-%m-%d %H:%M:%S")}:<br><pre>{(clean comment)}</pre></div>"))
(return (dict
:code 301
:headers {"Location" (. request (get "headers") (get "Referer"))})))
(= method "GET") (error 405 "method not allowed")))
(return (dict
:code 404
:body (parse-html-file "./www/site/html/error.html" :code 404 :message f"{path} not found"))))