add code for arpa web n-gon
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
(import hyrule.collections [assoc])
|
||||
(import content.file-io [parse-html-file send-raw-file])
|
||||
(import content.comments [create-comment])
|
||||
(import content.arpa-n-gon :as arpa-n-gon)
|
||||
(import re)
|
||||
(import functools [lru-cache])
|
||||
(import os.path [isdir :as dir? isfile :as file? abspath])
|
||||
@ -81,15 +82,36 @@
|
||||
:if (setx value (. request (get "route") (get "parameters") (get param None)))
|
||||
#(param value)))))))
|
||||
|
||||
(defn shtml-file-response [file [code 200] [template-params {}]]
|
||||
(defn require-params [#* params [otherwise (fn [#* _] (error 409 "missing required parameters"))]]
|
||||
(fn [f]
|
||||
(fn [method post request #** kwargs]
|
||||
(if (all (lfor param params (in param (. request (get "route") (get "parameters") (keys)))))
|
||||
(f method post request #** kwargs)
|
||||
(otherwise method post request #** kwargs)))))
|
||||
|
||||
(defn 303-if-not-arpa [f]
|
||||
(fn [method path request]
|
||||
(if (. request (get "headers") (get "Host") (endswith "arpa"))
|
||||
(f method path request)
|
||||
(dict
|
||||
:code 303
|
||||
:headers {"Location" f"http://natalieee.net.8.f.9.e.0.7.4.0.1.0.0.2.ip6.arpa/{(. request (get "route") (get "unparsed_route"))}"}))))
|
||||
|
||||
(defn shtml-file-response [file [code 200] [no-exec False] [template-params {}]]
|
||||
(dict
|
||||
:code code
|
||||
:headers {"Connection" "keep-alive" "Keep-Alive" "timeout=5 max=200"}
|
||||
:body (parse-html-file f"./www/site/html/{file}" #** template-params)))
|
||||
:body (parse-html-file f"./www/site/html/{file}" :no-exec no-exec #** template-params)))
|
||||
|
||||
(defn raw-file-response [file [code 200]]
|
||||
(dict :code code #** (dict (zip ["headers" "body"] (send-raw-file f"./www/site/{file}")))))
|
||||
|
||||
(defn+ match-request [{method "method" {path "path"} "route" :as request}]
|
||||
;; (try
|
||||
((get (routes.get-route-by-path path) method) method path request))
|
||||
;; (except [Exception]
|
||||
;; (return (error 500 "server error")))))
|
||||
|
||||
(defn [(route "/" "GET")] /home #route-args (shtml-file-response "home.html"))
|
||||
(defn [(route "/html/*" "GET") (if-file-exists :base-path "./www/site/html" :otherwise (error 404 "not found"))] /html/* #route-args (shtml-file-response path))
|
||||
(defn [lru-cache (route "/assets/*" "GET") (if-file-exists :base-path "./www/site/" :otherwise (error 404 "not found"))] /assets/* #route-args (raw-file-response path))
|
||||
@ -97,6 +119,22 @@
|
||||
(defn [(route "/comment" "POST")] /comments #route-args (create-comment request))
|
||||
(defn [lru-cache (route "/robots.txt" "GET") ] /robots #route-args (dict :code 200 :headers {"Content-Type" "text/plain"} :body "User-agent *\nDisallow: /\n"))
|
||||
|
||||
(defn+ match-request [{method "method" {path "path"} "route" :as request}]
|
||||
((get (routes.get-route-by-path path) method) method path request))
|
||||
|
||||
;; *.arpa web n-gon
|
||||
(setv members (arpa-n-gon.get-members))
|
||||
|
||||
(defn [(route "/arpa-n-gon" GET) 303-if-not-arpa] /arpa-n-gon #route-args (shtml-file-response "arpa-n-gon.html" :template-params (dict :n_gon (arpa-n-gon.n-gon-name (len members)) :n_gon_inc (arpa-n-gon.n-gon-name (+ (len members) 1)) :n (len members))))
|
||||
|
||||
(defn [(route "/arpa-n-gon/nav" GET) 303-if-not-arpa (forward-params "current" "style") (require-params "current") ] /arpa-n-gon/nav #route-args [current [style None]]
|
||||
(shtml-file-response "arpa-n-gon-nav.html" :no-exec True :template-params (dict
|
||||
:style style
|
||||
:next (+ "http://" (get (arpa-n-gon.next-member members current) "arpa-domain"))
|
||||
:prev (+ "http://" (get (arpa-n-gon.prev-member members current) "arpa-domain"))
|
||||
:n_gon (arpa-n-gon.n-gon-name (len members)))))
|
||||
|
||||
(defn [(route "/arpa-n-gon/next" GET) 303-if-not-arpa (forward-params "current" )(require-params "current")] /arpa-n-gon/next #route-args [current] (dict
|
||||
:code 303
|
||||
:headers {"Location" (+ "http://" (get (arpa-n-gon.next-member members current) "arpa-domain"))}))
|
||||
|
||||
(defn [(route "/arpa-n-gon/prev" GET) 303-if-not-arpa (forward-params "current") (require-params "current")] /arpa-n-gon/next #route-args [current] (dict
|
||||
:code 303
|
||||
:headers {"Location" (+ "http://" (get (arpa-n-gon.prev-member members current) "arpa-domain"))}))
|
||||
|
Reference in New Issue
Block a user