aboutsummaryrefslogtreecommitdiffstats
path: root/src/bitgloo_web/core.clj
blob: 169b54563657393580c05fd6179b2bea88ed314c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(ns bitgloo-web.core
  (:gen-class)
  (:require [clojure.java.io :as io])
  (:require [clojure.string :as str])
  (:require [hiccup.core :as hiccup])
  (:require [hiccup.page :as page])
  (:require [ring.adapter.jetty :refer [run-jetty]])
  (:require [ring.middleware.resource :refer [wrap-resource]])
  (:require [ring.middleware.content-type :refer [wrap-content-type]])
  (:require [ring.middleware.not-modified :refer [wrap-not-modified]])
  (:require [ring.util.response :as resp])
  (:require [markdown.core :refer [md-to-html-string]]))

(def image-extensions [".jpg" ".bmp"])

(def page-header
  [:div#title-container
   [:a {:href "/"}
    [:img#logo {:src "/logo.jpg" :alt "bitgloo logo"}]
    [:h1 "bitgloo"]]])

(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 image? [path] (some (partial str/ends-with? path) image-extensions))

(defn get-file-list [path] (->> path io/file .listFiles sort))

(defn parse-md-file [file] (-> file slurp md-to-html-string))

(defn render-md-files [file-list] (for [file file-list] [:div.block (parse-md-file file)]))

(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 content-path]
  (let [true-path (str content-path (if (= 1 (count path)) "/home" path))]
    (hiccup/html
      (page/html5
        [:head [:title "bitgloo"] (page/include-css "/main.css")]
        [:body [:div#container page-header (load-page-contents true-path) page-footer]]))))

(defn request-handler [content-path request]
  (when (= :get (:request-method request))
    (-> request
        :uri
        (load-page content-path)
        (resp/response)
        (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))
        (handler request)))))

(defn wrap-log-request [handler]
  (fn [request]
    (-> request
        (select-keys [:headers :protocol :request-method :uri])
        (update :headers get "x-forwarded-for")
        (vals)
        (conj "request:")
        ((partial apply println)))
    (handler request)))

(defn -main [& args]
  (if (= 2 (count args))
    (let [port (-> args first Integer/parseInt)
          content-path (second args)]
      (-> (partial request-handler content-path)
          (wrap-content-images content-path)
          (wrap-resource "public")
          (wrap-content-type)
          (wrap-not-modified)
          (wrap-log-request)
          (run-jetty {:port port})))
    (println "usage: bitgloo port content-path")))