Skip to content

Commit

Permalink
Start: moving toward serving from in memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
teesloane committed May 4, 2020
1 parent 0854c15 commit 8fb1cb5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
52 changes: 32 additions & 20 deletions clojure/src/firn/build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
[org.httpkit.server :as http]
;; [reitit.ring :as ring]
[ring.middleware.file :as r-file]
[ring.util.response :as response]
[ring.adapter.jetty :as jetty]
[me.raynes.fs :as fs]))
[ring.util.response :refer [response]]
;; [ring.adapter.jetty :as jetty]
[me.raynes.fs :as fs]
[clojure.string :as s]))

(set! *warn-on-reflection* true)
(declare server)
Expand Down Expand Up @@ -70,13 +71,11 @@
"Takes a config, of which we can presume has :processed-files.
Iterates on these files, and writes them to html using layouts."
[config]
(doseq [f (config :processed-files)]
(let [out-file-name (str (config :dir-site) (f :path-web) ".html")
out-file (file/htmlify config f)
out-html (out-file :as-html)]
(doseq [[_ f] (config :processed-files)]
(let [out-file-name (str (config :dir-site) (f :path-web) ".html")]
(when-not (file/is-private? config f)
(io/make-parents out-file-name)
(spit out-file-name out-html)))))
(spit out-file-name (f :as-html))))))

(defn all-files
"Processes all files in the org-directory"
Expand All @@ -89,28 +88,41 @@
;; -- Server --

(defn file-server-middleware
[{:keys [dir-site dir-files]}]
[{:keys [dir-site dir-files] :as config}]

(fn [request]
;; Run our build setup.
;; This is naive; everytime we visit a page it runs the build step.
(all-files {:dir-files dir-files})
(let [res-with-file (r-file/wrap-file request dir-site)]
(res-with-file request))))
(let [res-file-system ((r-file/wrap-file request dir-site) request)
req-uri-file (s/join "" (-> request :uri rest)) ;; we have to trim the requested uri because it comes in as "/my-link"; but they are in memory as "my-link"
file-html (get-in config [:processed-files req-uri-file :as-html])
four-oh-four {:status 404 :body "File not found."}]
;; TODO: - re-process single file when hit by route.
;; TODO: - all links with .html in them need to be stripped?
;; TODO: - make sure index is rendering from memory?
(cond
(some? file-html) (response file-html)
(some? res-file-system) res-file-system
:else four-oh-four))))

(defstate server
:start (let [args (mount/args)
path (get args :-path (u/get-cwd))
path-to-site (str path "_firn/_site")
opts {:dir-site path-to-site :dir-files path}
port 3333]
:start (let [args (mount/args)
dir-files (get args :-path (u/get-cwd))
path-to-site (str dir-files "_firn/_site")
;; config (setup (config/prepare dir-files))
config (-> (config/prepare dir-files) setup file/process-all) ;; basically doing `all-files`
port 3333]
(println "Building site...")
;; (all-files {:dir-files dir-files}) ;; we don't need this; we hold files in memory rn
(if-not (fs/exists? path-to-site)
(println "Couldn't find a _firn/ folder. Have you run `Firn new` and created a site yet?")
(do (println "🏔 Starting Firn development server on:" port)
(http/run-server (file-server-middleware opts) {:port port}))))
(http/run-server (file-server-middleware config) {:port port}))))
:stop (when server (server :timeout 100)))

(defn serve
[opts]
;; TODO: build the whole site before running the server.
(mount/start-with-args opts))

;; (serve {:-path "/Users/tees/Dropbox/wiki/"})
(serve {:-path "/Users/tees/Dropbox/wiki/"})

10 changes: 6 additions & 4 deletions clojure/src/firn/file.clj
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@
(let [layout (keyword (get-keyword f "FIRN_LAYOUT"))
as-html (when-not (is-private? config f)
(layout/apply-layout config f layout))]
(change f {:as-html as-html})))
as-html
#_(change f {:as-html as-html})))

(defn process-one
"Munge the 'file' datastructure; slowly filling it up, using let-shadowing.
Expand All @@ -170,7 +171,8 @@
new-file (change new-file {:keywords (get-keywords new-file)
:org-title (get-keyword new-file "TITLE")
:links (file-metadata :links)
:logbook (file-metadata :logbook)})]
:logbook (file-metadata :logbook)})
new-file (change new-file {:as-html (htmlify config new-file)})]
new-file))

(defn process-all
Expand All @@ -183,7 +185,7 @@
;; recurse over the org-files, gradually processing them and
;; pulling out links, logs, and other useful data.
(loop [org-files (config :org-files)
output []]
output {}]
(if (empty? org-files)
(assoc config
:processed-files output
Expand All @@ -194,7 +196,7 @@
(let [next-file (first org-files)
processed-file (process-one config next-file)
org-files (rest org-files)
output (conj output processed-file)
output (assoc output (processed-file :path-web) processed-file)
keyword-map (keywords->map processed-file)
new-site-map (merge keyword-map {:path (processed-file :path-web)})
file-metadata (extract-metadata processed-file)] ;; FIXME: why are we calling this once when we can pull the results out from `processed-file / via procssed one`?!
Expand Down
2 changes: 1 addition & 1 deletion clojure/src/firn/org.clj
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
(defn parsed-org-date->unix-time
"Converts the parsed org date (ex: [2020-04-27 Mon 15:39] -> 1588003740000)
and turns it into a unix timestamp."
[{:keys [year month day hour minute] :as pod}
[{:keys [year month day hour minute] :as processed-org-date-time}
{:keys [name] :as file}]
(let [pod->str (str year "-" month "-" day "T" hour ":" minute ":00.000-0000")
sdf (java.text.SimpleDateFormat. "yyyy-MM-dd'T'HH:mm:ss.SSSZ")]
Expand Down
4 changes: 3 additions & 1 deletion clojure/test/firn/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
[firn.config :as config]))

(def test-dir "test/firn/demo_org")
(def wiki-dir "/Users/tees/Dropbox/wiki/")

(defn build-test-files
[dir-to-build]
(fs/delete-dir (config/make-dir-firn dir-to-build))
(build/new-site {:dir-files dir-to-build})
(build/all-files {:dir-files dir-to-build}))

(build-test-files test-dir)
;; (build-test-files test-dir)
(build-test-files wiki-dir)
3 changes: 2 additions & 1 deletion clojure/test/firn/org_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
;; ie; missing end-date.
(t/deftest parsed-org-date->unix-time
(t/testing "returns the expected value."
(t/is (= 1585683360000 (sut/parsed-org-date->unix-time (sample-logentry :start))))))
(t/is (= 1585683360000
(sut/parsed-org-date->unix-time (sample-logentry :start) (stub/gtf :tf-1 :processed))))))

0 comments on commit 8fb1cb5

Please sign in to comment.