unify not found response

master
Clyne 2 years ago
parent dbd5c38bc7
commit 11ad43675f
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -1,4 +1,4 @@
(defproject bitgloo-web "0.3" (defproject bitgloo-web "0.4"
:description "bitgloo website framework" :description "bitgloo website framework"
:url "https://bitgloo.com" :url "https://bitgloo.com"
:license {:name "GPL-3.0-or-later" :url "https://www.gnu.org/licenses/gpl-3.0.en.html"} :license {:name "GPL-3.0-or-later" :url "https://www.gnu.org/licenses/gpl-3.0.en.html"}

@ -1,5 +1,5 @@
@font-face { @font-face {
/* https://indestructibletype.com/Jost.html */ /* https://indestructibletype.com/Jost.html */
font-family: Jost; font-family: Jost;
src: url('/Jost-400-Book.otf'); src: url('/Jost-400-Book.otf');
} }
@ -9,17 +9,19 @@ html, body {
font-family: Jost; font-family: Jost;
} }
body {
padding: 0em 1.5em;
}
#title-container { #title-container {
padding-top: 4em; padding-top: 4em;
padding-bottom: 0.5em; padding-bottom: 0.5em;
} }
#container {
padding: 0em 1.5em;
}
#posts { #posts {
min-height: 400px; min-height: 400px;
} }
#logo { #logo {
height: 90px; height: 90px;
} }
@ -38,9 +40,6 @@ h1 {
font-size: 4em; font-size: 4em;
display: inline; display: inline;
padding-left: 0.1em; padding-left: 0.1em;
}
h1 {
color: black; color: black;
} }
@ -70,9 +69,6 @@ a, a code {
color: #ff3300; color: #ff3300;
} }
p, li, span {
}
#footer { #footer {
color: #666; color: #666;
text-align: right; text-align: right;

@ -13,8 +13,6 @@
(def page-footer [:div#footer [:span "© Clyne Sullivan 2023"]]) (def page-footer [:div#footer [:span "© Clyne Sullivan 2023"]])
(def page-not-found [:div "Page not found"])
(defn md-file? [path] (str/ends-with? path ".md")) (defn md-file? [path] (str/ends-with? path ".md"))
(defn get-file-list [path] (->> path io/file .listFiles sort)) (defn get-file-list [path] (->> path io/file .listFiles sort))
@ -23,14 +21,10 @@
(defn render-md-files [file-list] (for [f file-list] [:div.block (parse-md-file f)])) (defn render-md-files [file-list] (for [f file-list] [:div.block (parse-md-file f)]))
(defn load-page-contents [path]
(let [file-list (filter md-file? (get-file-list path))]
(if (empty? file-list)
page-not-found
[:div#posts (render-md-files file-list)])))
(defn load-page [path] (defn load-page [path]
(page/html5 (let [file-list (filter md-file? (get-file-list path))]
[:head [:title "bitgloo"] (page/include-css "/main.css")] (when-not (empty? file-list)
[:body [:div#container page-header (load-page-contents path) page-footer]])) (page/html5
[:head [:title "bitgloo"] (page/include-css "/main.css")]
[:body page-header [:div#posts (render-md-files file-list)] page-footer]))))

@ -1,6 +1,7 @@
(ns bitgloo-web.core (ns bitgloo-web.core
(:gen-class) (:gen-class)
(:require [bitgloo-web.content :as content]) (:require [bitgloo-web.content :as content])
(:require [clojure.java.io :as io])
(:require [clojure.string :as str]) (:require [clojure.string :as str])
(:require [ring.adapter.jetty :refer [run-jetty]]) (:require [ring.adapter.jetty :refer [run-jetty]])
(:require [ring.middleware.resource :refer [wrap-resource]]) (:require [ring.middleware.resource :refer [wrap-resource]])
@ -8,6 +9,8 @@
(:require [ring.middleware.not-modified :refer [wrap-not-modified]]) (:require [ring.middleware.not-modified :refer [wrap-not-modified]])
(:require [ring.util.response :as resp])) (:require [ring.util.response :as resp]))
(def not-found (resp/not-found "Not found"))
(def image-extensions [".jpg" ".bmp"]) (def image-extensions [".jpg" ".bmp"])
(defn image? [path] (some (partial str/ends-with? path) image-extensions)) (defn image? [path] (some (partial str/ends-with? path) image-extensions))
@ -15,22 +18,23 @@
(defn home-redirect [uri] (if (= "/" uri) "/home" uri)) (defn home-redirect [uri] (if (= "/" uri) "/home" uri))
(defn request-handler [content-path request] (defn request-handler [content-path request]
(when (= :get (:request-method request)) (let [path (str content-path (home-redirect (:uri request)))]
(-> (:uri request) (when-let [content (content/load-page path)]
(home-redirect) (-> (resp/response content)
((partial str content-path)) (resp/content-type "text/html")
(content/load-page) (resp/charset "utf8")))))
(resp/response)
(resp/content-type "text/html")
(resp/charset "utf8"))))
(defn wrap-content-images [handler content-path] (defn wrap-content-images [handler content-path]
(fn [request] (fn [request]
(let [uri (:uri request)] (let [uri (:uri request) path (str content-path uri)]
(if (and (= :get (:request-method request)) (image? uri)) (if (image? uri)
(resp/file-response (str content-path uri)) (when (.exists (io/file path)) (resp/file-response path))
(handler request))))) (handler request)))))
(defn wrap-only-gets [handler]
(fn [request]
(when (= :get (:request-method request)) (handler request))))
(defn wrap-log-request [handler] (defn wrap-log-request [handler]
(fn [request] (fn [request]
(-> request (-> request
@ -41,6 +45,8 @@
((partial apply println))) ((partial apply println)))
(handler request))) (handler request)))
(defn wrap-not-found [handler] (fn [request] (or (handler request) not-found)))
(defn -main [& args] (defn -main [& args]
(if (= 2 (count args)) (if (= 2 (count args))
(let [port (-> args first Integer/parseInt) (let [port (-> args first Integer/parseInt)
@ -50,6 +56,8 @@
(wrap-resource "public") (wrap-resource "public")
(wrap-content-type) (wrap-content-type)
(wrap-not-modified) (wrap-not-modified)
(wrap-only-gets)
(wrap-not-found)
(wrap-log-request) (wrap-log-request)
(run-jetty {:port port}))) (run-jetty {:port port})))
(println "usage: bitgloo port content-path"))) (println "usage: bitgloo port content-path")))

Loading…
Cancel
Save