This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-10-27
Channels
- # announcements (1)
- # beginners (71)
- # braveandtrue (4)
- # cider (1)
- # cljs-dev (4)
- # cljsrn (8)
- # clojure (111)
- # clojure-italy (5)
- # clojure-spec (22)
- # clojure-uk (8)
- # clojurescript (80)
- # cryogen (14)
- # cursive (7)
- # data-science (1)
- # datomic (25)
- # dirac (1)
- # figwheel-main (4)
- # fulcro (13)
- # incanter (1)
- # off-topic (6)
- # other-languages (3)
- # pathom (11)
- # portkey (5)
- # re-frame (13)
- # reagent (3)
- # reitit (24)
- # ring-swagger (7)
- # shadow-cljs (63)
- # spacemacs (3)
- # specter (4)
- # tools-deps (9)
Hello, I am trying to add reitit to the reagent-template. But I get stuck on getting the handler to return anything else than a 404. This is the definition of my app
:
(ns b.handler
(:require [reitit.ring :as reitit-ring]
[b.middleware :refer [middleware]]
[hiccup.page :refer [include-js include-css html5]]
[config.core :refer [env]]))
(def mount-target
[:div#app
[:h3 "ClojureScript has not been compiled!"]
[:p "please run "
[:b "lein figwheel"]
" in order to start the compiler"]])
(defn head []
[:head
[:meta {:charset "utf-8"}]
[:meta {:name "viewport"
:content "width=device-width, initial-scale=1"}]
(include-css (if (env :dev) "/css/site.css" "/css/site.min.css"))])
(defn loading-page []
(html5
(head)
[:body {:class "body-container"}
mount-target
(include-js "/js/app.js")]))
(defn index-handler
[_request]
{:status 200
:headers {"Content-Type" "text/html"}
:body (loading-page)})
(def about-handler index-handler)
(def items-handler index-handler)
(def item-handler index-handler)
(def app
(reitit-ring/ring-handler
(reitit-ring/router
["/"
["" {:get {:handler index-handler}}
"items" ["" {:get {:handler items-handler}}
["/item-:item-id" {:get {:handler item-handler}}]]
"about" {:get {:handler about-handler}}]]
{:data {:middleware middleware}})
(reitit-ring/routes
(reitit-ring/create-resource-handler {:path "/" :root "/public"})
(reitit-ring/create-default-handler))))
and middleware
is:
(def middleware
[wrap-params
wrap-content-type
wrap-exceptions
wrap-reload])
@pez the route syntax is not right, It should be either:
;; nested
[["/" {:get {:handler index-handler}}]
["/items"
["" {:get {:handler items-handler}}]
["/:item-id" {:get {:handler item-handler}}]]
["/about" {:get {:handler about-handler}}]]
or:
;; flattened
[["/" {:get {:handler index-handler}}]
["/items" {:get {:handler items-handler}}]
["/items/:item-id" {:get {:handler item-handler}}]
["/about" {:get {:handler about-handler}}]]
if there is no other route meta-data, it can be shortened to:
[["/" {:get index-handler}]
["/items" {:get items-handler}]
["/items/:item-id" {:get item-handler}]
["/about" {:get about-handler}]]
currentlty, the path-parameters need to fill the whole path segments, so "/item-:item-id"
doesn’t work.
Tries to spec the route data format, but the errors were so hairy, that I didn’t help. Need to add some hand-crafted validation for that.
((compojure.core/GET "/item-:id" [id]
(str id))
{:request-method :get, :uri "item-2"})
; nil
I have a PR pending that takes the reagent-template from Compojure to Bidi, but then decided I’d rather take it all the way to reitit.
Hmm.. actually, that works with Compojure:
((compojure.core/GET "/item-:id" [id]
(str id))
{:request-method :get, :uri "/item-2"})
; {:status 200, :headers {"Content-Type" "text/html; charset=utf-8"}, :body "2"}
Cool. But the item-:item-id
syntax is not important. I’ll go for item/:item-id
to keep the template simple.
agree, it’s simpler. working on with error messages and route debugger right now. trying to make thing user-friendly
Some kind of debugging middleware, maybe would be helpful for the problem I stumbled upon here.
you can ask the route-table from a router to see what routes are there and there is small guide on how to debug mw (https://metosin.github.io/reitit/ring/transforming_middleware_chain.html). But we can do much better! goal is to provide a small web-socket based debugger with routes visualized and a step-debugger for requests: how did each step (mw/interceptor) change the request.
Looking forward to that debugger. I had similar problems with bidi when I started with it. Just getting used to the syntax.
not yet a stop-debugger, but visualization seems easy: https://twitter.com/ikitommi/status/1056175691480342533
Chime in, if you have some input, @ikitommi: https://github.com/reagent-project/reagent-template/pull/151