Skip to content

Commit

Permalink
Add RSS feed
Browse files Browse the repository at this point in the history
  • Loading branch information
S4G4R committed May 26, 2024
1 parent 59e4485 commit b47d640
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
[alekcz/storyblok-clj "1.2.0"]
[camel-snake-kebab/camel-snake-kebab "0.4.3"]
[clojure.java-time "1.3.0"]
[superstructor/re-highlight "2.0.2"]]
[superstructor/re-highlight "2.0.2"]
[clj-rss "0.4.0"]]
:plugins [[lein-environ "1.1.0"]]
:main ^:skip-aot com.sagar.casa
:target-path "target/%s"
Expand Down
12 changes: 12 additions & 0 deletions src/com/sagar/casa/rss.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns com.sagar.casa.rss
(:require [com.sagar.casa.api.storyblok :as api]
[clj-rss.core :as rss]))


(defn rss-feed
[]
(->> (api/get-story :blogs)
(map #(select-keys % [:title :slug :description]))
(map #(assoc % :link (str "https://sagarvrajalal.com/" (:slug %))))
(map #(dissoc % :slug))
(apply rss/channel-xml)))
32 changes: 21 additions & 11 deletions src/com/sagar/casa/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
(:require [com.sagar.casa.middleware :as middleware]
com.sagar.casa.ui
[donut.system :as ds]
[com.sagar.casa.rss :as rss]
[hyperfiddle.electric :as e]
[hyperfiddle.electric-ring-adapter :as electric-ring]
[ring.adapter.jetty :as ring]
[ring.middleware.cookies :as cookies]
[ring.middleware.params :refer [wrap-params]]
[shadow.cljs.devtools.api :as shadow-api]
[shadow.cljs.devtools.server :as shadow-server]
[taoensso.timbre :as timbre])
[taoensso.timbre :as timbre]
[ring.util.response :as res])
(:import (org.eclipse.jetty.server.handler.gzip
GzipHandler)
(org.eclipse.jetty.websocket.server.config
Expand All @@ -21,6 +23,18 @@
;; See Dockerfile
(not-empty (System/getProperty "HYPERFIDDLE_ELECTRIC_APP_VERSION")))

(def entrypoint
(fn [handler]
(e/boot-server {} com.sagar.casa.ui/Root handler)))


(defn rss-middleware
[handler]
(fn [request]
(if (= (:uri request) "/feed.xml")
(-> (res/response (rss/rss-feed))
(res/content-type "application/xml"))
(handler request))))

(defn electric-websocket-middleware
"Open a websocket and boot an Electric server program defined by `entrypoint`.
Expand All @@ -30,15 +44,16 @@
- see `hyperfiddle.electric-ring-adapter/wrap-reject-stale-client`
- an Electric `entrypoint`: a function (fn [ring-request] (e/boot-server {} my-ns/My-e-defn ring-request))
"
[next-handler config entrypoint]
[next-handler config]
(-> (electric-ring/wrap-electric-websocket next-handler entrypoint)
(cookies/wrap-cookies)
(electric-ring/wrap-reject-stale-client config)
(wrap-params)))

(defn middleware [config entrypoint]
(defn middleware [config]
(-> (middleware/http-middleware config) ; 2. serve regular http content
(electric-websocket-middleware config entrypoint))) ; 1. intercept electric websocket
(electric-websocket-middleware config) ; 1. intercept electric websocket
(rss-middleware)))

(defn- add-gzip-handler!
"Makes Jetty server compress responses. Optional but recommended."
Expand Down Expand Up @@ -69,16 +84,11 @@
(.setMaxTextMessageSize wsContainer (* 100 1024 1024))))))


(def entrypoint
(fn [handler]
(e/boot-server {} com.sagar.casa.ui/Root handler)))


(def server
#::ds{:start (fn [{{:keys [host port] :as opts} ::ds/config}]
;; Start electric compiler and server
(timbre/warn (str "Starting server on " host ":" port))
(ring/run-jetty (middleware opts entrypoint) opts))
(ring/run-jetty (middleware opts) opts))
:config {:host (ds/ref [:env :http-host])
:port (ds/ref [:env :http-port])
:join? false
Expand All @@ -98,7 +108,7 @@
(shadow-api/dev :dev)
;; Start electric compiler and server
(timbre/warn (str "Starting server on " host ":" port))
(ring/run-jetty (middleware opts entrypoint) opts))
(ring/run-jetty (middleware opts) opts))
:stop (fn [{server ::ds/instance}]
(timbre/warn "Stopping HTTP Server...")
(.stop server)
Expand Down
13 changes: 10 additions & 3 deletions src/com/sagar/casa/ui/blog.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,16 @@
:fluid true
:class-name "pt-3 col-md-6 overflow-auto mx-auto"}
[:> Stack {:gap 2}
[:a {:style {:text-decoration :none}
:href routes/home}
"↰ Home"]

[:> Row
[:> Col {:class-name "text-start"}
[:a {:style {:text-decoration :none}
:href routes/home}
"↰ Home"]]
[:> Col {:class-name "text-end"}
[:a {:style {:text-decoration :none}
:href routes/rss}
"RSS " [:i {:class-name "fa fa-rss"}]]]]
[:div
[:h2 "Blog"]
[:hr {:style {:border-color :black}}]]
Expand Down
1 change: 1 addition & 0 deletions src/com/sagar/casa/ui/routes.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
(def blogpost (partial str blog "/"))
(def not-found "/not-found")
(def literature "/literature")
(def rss "/feed.xml")

0 comments on commit b47d640

Please sign in to comment.