GithubHelp home page GithubHelp logo

trail's Introduction

trail

Ring routing library

Install

Looking for v1? Look no further my friend!

Add this to your project.clj

[trail "2.1.0"]

Usage

(ns your-app.core
  (require [trail.core :as trail]))

These are the routes

(def routes [
  [:get    "/items"           items/index]
  [:get    "/items/:id"       items/show]
  [:get    "/items/:id/fresh" items/fresh]
  [:get    "/items/:id/edit"  items/edit]
  [:post   "/items"           items/create]
  [:put    "/items/:id"       items/change]
  [:delete "/items/:id"       items/delete]
])

And you can write your routes as data if you wanted to. Or there are some functions if you prefer parens

(def routes
  (-> (trail/get    "/items"           items/index)
      (trail/get    "/items/:id"       items/show)
      (trail/get    "/items/:id/fresh" items/fresh)
      (trail/get    "/items/:id/edit"  items/edit)
      (trail/post   "/items"           items/create)
      (trail/put    "/items/:id"       items/change)
      (trail/delete "/items/:id"       items/delete)))

There's also a function that turns your route into a ring handler function

(trail/match-routes your-route-map)

Here's a more complete example

(ns your-app.core
  (require [trail.core :as trail]
           [org.httpkit.server :as server]
           [ring.middleware.defaults :as ring-defaults]
           [your-app.controllers.items :as items]))

(def routes
  (-> (trail/get    "/items"           items/index)
      (trail/get    "/items/:id"       items/show)
      (trail/get    "/items/:id/fresh" items/fresh)
      (trail/get    "/items/:id/edit"  items/edit)
      (trail/post   "/items"           items/create)
      (trail/put    "/items/:id"       items/change)
      (trail/delete "/items/:id"       items/delete)))

(def app
  (-> (trail/match-routes routes) ; it's this one here
      (ring-defaults/wrap-defaults site-defaults)))

(server/run-server app {:port 1337})

The last thing this library has that other routing libraries do not is the concept of a resource shamelessly stolen from rails routing

So instead of this

(ns your-app.core
  (require [trail.core :as trail]
           [your-app.controllers.items :as items]))

(def routes
  (-> (trail/get    "/items"           items/index)
      (trail/get    "/items/:id"       items/show)
      (trail/get    "/items/:id/fresh" items/fresh)
      (trail/get    "/items/:id/edit"  items/edit)
      (trail/post   "/items"           items/create)
      (trail/put    "/items/:id"       items/change)
      (trail/delete "/items/:id"       items/delete)))

You can do this

(ns your-app.core
  (require [trail.core :as trail]
           [your-app.controllers.items :as items]))

(def routes
  (-> (trail/resource :items)))

And you can do this

(ns your-app.core
  (:require [trail.core :as trail]
            [your-app.controllers.posts :as posts]
            [your-app.controllers.tags :as tags]))

(def routes
  (-> (trail/resource :posts :tags))

; this gives you
;
; GET "/posts/:post-id/tags" => tags/index
; GET "/posts/:post-id/tags/:id" => tags/show
; GET "/posts/:post-id/tags/new" => tags/fresh
; GET "/posts/:post-id/tags/:id/edit" => tags/edit
; POST "/posts/:post-id/tags" => tags/create
; PUT "/posts/:post-id/tags/:id" => tags/change
; DELETE "/posts/:post-id/tags/:id" => tags/delete

Don't want all of the routes a resource gives you?

(def routes
 (-> (trail/resource :posts :only [:index :show])))
 
; =>
[
 [:get "/posts"     posts/index]
 [:get "/posts/:id" posts/show]
]

Want all of the routes except certain ones?

(def routes
 (-> (trail/resource :posts :except [:index :show])))
 
 ; =>
 [
  [:get    "/posts/:id/fresh" posts/fresh]
  [:get    "/posts/:id/edit"  posts/edit]
  [:post   "/posts"           posts/create]
  [:put    "/posts/:id"       posts/change]
  [:delete "/posts/:id"       posts/delete]
 ]

Why?

I like shortcuts, so rails resource routing really appealed to me. I also like clojure so I combined them.

trail's People

Contributors

swlkr avatar

Stargazers

 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.