Repo: https://github.com/runexec/stray
 

Stray

Stray is an alternative routing library for Compojure

;; Leiningen Dependency

[stray "1.01"]

(ns example
  (:require [stray.core :refer [router]]))

(def routes
  {:public {:index-page {:http/path "/"
                         :http/method :GET
                         :http/req :namespace.core/example-handler}
            :user-page {:http/path "/user/:id"
                        :http/method :GET
                        :http/req :namespace.core/handler}}
   :dev {:new-welcome-page {:http/path "/welcome/:user"
                            :http/method :GET
                            :http/req :project.users/new-welcome}}})

(defn example-handler [req]
  (str "req: " (keys req)))

;; lein ring servers in project.clj
;; :ring {:handler namespace.core/public-app}

(def public-app (router routes :public))

(def dev-app (router routes :dev))

Tutorial

  • Create a Project
lein new example-project
  • Add to project.clj
:dependencies [[stray "1.01"]]
:plugins [[lein-ring "0.8.12"]]
:ring {:handler example-project.core/public-app}
  • Refer router in src/example_project/core.clj
(ns example-project.core
  (:require [stray.core :refer [router]]))
  • Define routes
(def routes
  {:public {:hello-page {:http/path "/hello/:name"
                         :http/method :GET
                         :http/req :example-project.core/hello-handler}}
   {:dev {:hello-page {:http/path "/hello/:name"
                       :http/method :GET
                       :http/req :example-project.core/hello-handler}}}
   {:other {:hello-page {:http/path "/hello/:name"
                         :http/method :GET
                         :http/req :example-project.core/hello-handler}}}})
  • Define handlers
(defn hello-handler [req]
  "I got a ring/compojure request!!!")

  • Define applications
(def public-app (router routes :public))
  • Save and run
lein ring server

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to