forked from taoensso/sente
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server adapter for Macchiato Framework on Node.js
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
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |