Initial work for isolating post io

This commit is contained in:
Charlotte Allen 2020-07-22 15:51:29 -07:00
parent 71638b9018
commit 0674314e0d
5 changed files with 70 additions and 5 deletions

View File

@ -19,6 +19,7 @@
[metosin/reitit "0.3.10"]
[metosin/ring-http-response "0.9.1"]
[mount "0.1.16"]
[duratom "0.5.2"]
[nrepl "0.6.0"]
[org.clojure/clojure "1.10.1"]
[org.clojure/tools.cli "0.4.2"]
@ -29,6 +30,8 @@
[ring-webjars "0.2.0"]
[ring/ring-core "1.8.0"]
[ring/ring-defaults "0.3.2"]
[lein-cljfmt "0.6.6"]
[selmer "1.12.18"]]
:min-lein-version "2.0.0"

View File

@ -2,6 +2,7 @@
(:require
[shapey-shifty.handler :as handler]
[shapey-shifty.nrepl :as nrepl]
[shapey-shifty.index.index :as index]
[luminus.http-server :as http]
[shapey-shifty.config :refer [env]]
[clojure.tools.cli :refer [parse-opts]]
@ -21,6 +22,10 @@
[["-p" "--port PORT" "Port number"
:parse-fn #(Integer/parseInt %)]])
(mount/defstate index :start (if-let [path (env :index-path)]
(index/create-index path)
(index/create-index "resources/index.edn")))
(mount/defstate ^{:on-reload :noop} http-server
:start
(http/start

View File

@ -1,13 +1,20 @@
(ns shapey-shifty.index.index
(:require [clucy.core :as clucy]
[duratom.core :as dur]
[shapey-shifty.posts.posts-io :as post-io]))
(def index-path (atom "resources/index"))
(defn create-index [index-path]
(let [index (dur/duratom :local-file :file-path index-path :init [])]
index))
(def post-index (clucy/disk-index @index-path))
(defn add-post-to-index [post]
(clucy/add post-index post))
(defn add-post-to-index [index post]
(let [metadata (dissoc post :content)]
(if-let [existing-post (empty (filter #(= (:key %) (:key post)) index))]
(conj index metadata))))
(defn crawl-posts!
([path]
@ -17,5 +24,5 @@
clojure.java.io/file
file-seq
(filter #(.isFile %))
(mapv #(parsing-fn %))
(mapv parsing-fn)
(apply add-post-to-index))))

View File

@ -1,6 +1,32 @@
(ns shapey-shifty.posts.core)
(ns shapey-shifty.posts.core
(:require [clojure.spec.alpha :as s]))
(defn create-empty-post [] {:type nil :properties {:name nil :author nil :published nil :content nil}})
(def posts (atom []))
(defn create-empty-post [] {::type :note ::key (java.util.UUID/randomUUID) ::content ""
::properties
{::name nil
::author nil
::published nil
::stub nil
::filename nil
::status :preview}})
(s/def ::name (s/nilable string?))
(s/def ::author string?)
(s/def ::published keyword?)
(s/def ::content string?)
(s/def ::stub string?)
(s/def ::filename (s/nilable string?))
(s/def ::type keyword?)
(s/def ::key uuid?)
(s/def ::status keyword?)
(s/def ::properties
(s/keys :req [::name ::author ::published ::stub ::filename]))
(s/def ::post
(s/keys :req [::type ::properties ::content]))
(defn set-publish-date [post date]
(assoc-in post [:properties :published] date))

View File

@ -3,6 +3,12 @@
[shapey-shifty.posts.core :as core]
[shapey-shifty.authors.author-core :as author]))
(defprotocol PostKeeper
(create-post [this post])
(get-posts [this])
(update-post [this post])
(delete-post [this post]))
(def post-filename "post.json")
(def base-posts-path "resources/posts")
@ -43,3 +49,21 @@
(let [path (format "%s/%s/%s/%s" base-posts-path (pathmap-to-path dt-path) n post-filename)
f (clojure.java.io/file path)]
(read-post f))))
(defn datetime-filename-resolver [base-path filename post]
(if (:filename post) (:filename post)))
(defrecord FileBasedPostKeeper [filename-resolver base-path filename]
PostKeeper
(create-post [this post]
(let [path (str base-path (filename-resolver post) filename)]
(clojure.java.io/make-parents path)
(spit path post)))
(get-posts [this] (->> base-path
clojure.java.io/file
file-seq
(filter #(.isDirectory %))
(filter #(.exists %))
read-post))
(update-post [this post] nil)
(delete-post [this post] nil))