Skip to content

Commit

Permalink
[#307] Add server adapter for Macchiato Framework on Node.js (@theasp)
Browse files Browse the repository at this point in the history
https://macchiato-framework.github.io/

This needs the following to have a working session, and csrf token:
macchiato-framework/macchiato-core#20
  • Loading branch information
theasp authored and ptaoussanis committed Jan 13, 2019
1 parent d29165b commit c516d79
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
[[http-kit "2.3.0"]
[org.immutant/web "2.1.10"]
[nginx-clojure "0.4.5"]
[aleph "0.4.6"]]}]}
[aleph "0.4.6"]
[macchiato/core "0.2.14"]]}]}

:cljsbuild
{:test-commands {"node" ["node" :node-runner "target/main.js"]
Expand Down
36 changes: 36 additions & 0 deletions src/taoensso/sente/server_adapters/macchiato.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(ns taoensso.sente.server-adapters.macchiato
"Sente server adapter for Node.js with the Macchiato Framework
(https://macchiato-framework.github.io/)."
{:author "Andrew Phillips <@theasp>"}
(:require
[macchiato.middleware.anti-forgery :as csrf]
[taoensso.sente :as sente]
[taoensso.encore :as enc :refer-macros ()]
[taoensso.sente.server-adapters.generic-node :as generic-node]
[taoensso.timbre :as timbre
:refer-macros (tracef debugf infof warnf errorf)]))

(def csrf-path [:session :macchiato.middleware.anti-forgery/anti-forgery-token])

(defn wrap-macchiato
"Wraps a generic node Sente handler to work with Macchiato. This
remaps some keys of a Macchiato request to match what Sente and the
generic node adapter are expecting, calling `handler`. The generic
node adapter will call the appropriate methods on the Node.js response
object without using Macchiato's response function."
[handler]
(fn [req res raise]
(-> req
(assoc :response (:node/response req))
(assoc-in [:session :csrf-token] (get-in req csrf-path))
(handler))))

(defn make-macchiato-channel-socket-server!
"A customized `make-channel-socket-server!` that uses Node.js with
Macchiato as the web server."
[& [opts]]
(tracef "Making Macchiato chsk")
(-> (generic-node/get-sch-adapter)
(sente/make-channel-socket-server! opts)
(update :ajax-get-or-ws-handshake-fn wrap-macchiato)
(update :ajax-post-fn wrap-macchiato)))

0 comments on commit c516d79

Please sign in to comment.