Skip to content

Commit

Permalink
Server adapter for Macchiato Framework on Node.js
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 committed Jul 22, 2017
1 parent e3d4173 commit ac99f3a
Showing 1 changed file with 36 additions and 0 deletions.
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 ac99f3a

Please sign in to comment.