duct-framework / module.web

Duct module for running web applications
21 stars 19 forks source link

How to set route to a Reagent SPA base html page in resources/public #10

Closed lrosso closed 6 years ago

lrosso commented 6 years ago

Hi,

How could I set a route to a Reagent SPA base html page in resources/public?

Among the tutorials and demos I have found out so far, I haven't been able to discover any description or example about this.

Congrats for such cool projects !!

Thanks in advance !

Luis

weavejester commented 6 years ago

There's a middleware function in this library called wrap-route-aliases. You can use it like:

{:duct.core/handler
 {:middleware [#ig/ref :duct.middleware.web/route-aliases]}

 :duct.middleware.web/route-aliases
 {"/" "/index.html"}}

I believe that should work, as long as I haven't made a mistake.

lrosso commented 6 years ago

Hi James !

Thanks for the reply !

Unfortunately, it hasn't worked.

As I added supplied configs in project's edn file, I realized it used Ataraxy routing rather than module.web and, hence, perhaps this is not the right context for the question...

In order to supply more context to initial question, the project I am working on is: https://github.com/walkable-server/realworld

I am trying to integrate it, as back-end, with the following one, as its corresponding front-end Reagent SPA: https://github.com/polymeris/re-frame-realword-example-app

More precisely, I am working on its edn config: https://github.com/walkable-server/realworld/blob/master/resources/conduit/config.edn

What I have already get working right is having the application serving static files from its resources/public dir, by means of the following config:

:duct.middleware.web/defaults {:static {:resources ["public"]}}

However, it is still required to reference explicitly in the URL the index.html available in that dir, instead of having it supplied by default.

Again, thanks in advance !

Luis

weavejester commented 6 years ago

Here's a full working configuration I whipped up:

{:duct.core/project-ns  foo
 :duct.core/environment :production

 :duct.module/logging  {}
 :duct.module.web/site {}
 :duct.module/cljs     {:main foo.client}

 :duct.module/ataraxy {}

 :duct.core/handler
 {:middleware [#ig/ref :duct.middleware.web/format
               #ig/ref :duct.middleware.web/defaults
               #ig/ref :duct.middleware.web/route-aliases]}

 :duct.middleware.web/format {}
 :duct.middleware.web/route-aliases
 {"/" "/index.html"}}

I'm using the :duct.module.web/site module here instead of the api module, and adding on the :duct.middleware.web/format middleware manually. I'd recommend doing this over starting with the API module. In future I plan to add a SPA module to make this easier.

I've also added in the :duct.middleware.web/defaults middleware key to guarantee that it's called inside the route aliases, since otherwise the server will hit the 404 "not-found" middleware first. This might have been the reason you couldn't get the middleware to work.

lrosso commented 6 years ago

Hi James,

It worked.

Thank you very much.

Luis