Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Express Integration #5

Open
njordhov opened this issue Dec 14, 2016 · 5 comments
Open

Express Integration #5

njordhov opened this issue Dec 14, 2016 · 5 comments

Comments

@njordhov
Copy link

njordhov commented Dec 14, 2016

I hacked Macchiato to work with the Express web framework, combining the two on the same server. It facilitates Express-based servers to be gradually converted to use Macchiato. And existing Express integrations such as Sente can immediately be used with Macchiato. This is the essential code:

(defn bidi-router [routes]                                                                                    
  (fn [req res]                                                                                                                  
    (if-let [route (some->> req :uri (bidi.bidi/match-route routes) :handler)]                                                   
      (route req res))))
                                                                                                                                 
(defn macchiato [http-router & [opts]]                                                                                           
  (let [route (macchiato.http/handler http-router opts)]                                                                         
    (fn [req res next]                                                                                                           
      (if-not (route req res)                                                                                                    
        (next)))))

Macchiato bidi routes can then be used with Express:

(doto (@express) 
    (.use "/" (macchiato (bidi-router routes))))

Perhaps it could be an optional module? I'd be glad to turn it into a PR.

@yogthos
Copy link
Member

yogthos commented Dec 14, 2016

This is a great idea. Express has a large ecosystem around it, so it could fill a lot of the gaps immediately. Perhaps it could be wrapped up as a library. I can see it growing to provide cljs wrappers for the core Express API such as routing and middleware.

I'm thinking of using template profiles the way I do in Luminus, so you could have a +express profile that includes the library and sets up the plumbing for it when a new project is created.

@theasp
Copy link
Contributor

theasp commented Jul 22, 2017

@TerjeNorderhaug, I've added a Macchiato server adapter to use Sente directly: taoensso/sente#307

Example: https://github.com/theasp/sente-nodejs-example/tree/macchiato

@yogthos
Copy link
Member

yogthos commented Jul 22, 2017

Thanks, mind if I add that under the official examples repo as well?

@theasp
Copy link
Contributor

theasp commented Jul 23, 2017

Sure, go right ahead, that's what it's for. :)

It might be a good idea to note that the example covers other frameworks too, just to avoid confusion.

@yogthos
Copy link
Member

yogthos commented Jul 23, 2017

Perfect, and I'm thinking I'll factor out the macchiato specific stuff to make it simpler. I guess should probably wait until new sente pr is merged though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants