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"
:url "https://bitgloo.com"
:license {:name "GPL-3.0-or-later" :url "https://www.gnu.org/licenses/gpl-3.0.en.html"}

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

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

Loading…
Cancel
Save