Giter VIP home page Giter VIP logo

gudu's Introduction

gudu

"Generate URL, Degenerate URL"

A bi-directional routing and URL generation library, where routes are configured as simple Clojure data structures.

Routes are defined as a data structure that is used at runtime to route requests and construct URLs. As the routes are just a data structure they can be easily inspected by other functions to produce related data, e.g. navigation, breadcrumbs.

Usage

(require '[gudu.core :as gd]
         '[gudu.segment :as gds]
         '[gudu.middleware :as gdm])

(def my-routes
  {:home gds/root
   :blog ["blog" {:latest gds/root
                  :post   [gds/string]}]})

;; URL Generation
(def gu (gd/gu my-routes))

(gu :home)                    ;; => "/"
(gu :blog)                    ;; => "/blog"
(gu :blog :post "great-post") ;; => "/blog/great-post"

;; URL Degeneration
(def du (gd/du my-routes))

(du "/")                  ;; => [:home]
(du "/blog")              ;; => [:blog :latest]
(du "/blog/another-post") ;; => [:blog :post "another-post"]

;; du is normally accessed indirectly via the gudu ring middleware
(defn get-handler [route]
  ;; ... return handler function based on route ...
  (case (first route)
    :home (fn [req] {:handler :home :uri (:uri req) :route route})
    :blog (fn [req] {:handler :blog :uri (:uri req) :route route})))
(def app
  (-> (gdm/router get-handler (fn [_req] {:handler :404}))
      (gdm/wrap-route my-routes)))

(app {:uri "/"})       ;; {:handler :home, :uri "/", :route [:home]}
(app {:uri "/blog"})   ;; {:handler :blog, :uri "/blog", :route [:blog :latest]}
(app {:uri "/blog/3"}) ;; {:handler :blog, :uri "/blog/post-3", :route [:blog :post "3"]}
(app {:uri "/404"})    ;; {:handler :404}

Documentation

Examples

See the gudu-examples project, in particular take a look at blog.clj.

License

Copyright © 2013 Matt Lee

Distributed under the Eclipse Public License, the same as Clojure.

gudu's People

Contributors

thatismatt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.