Skip to content

Instantly share code, notes, and snippets.

@iantruslove
Last active April 21, 2016 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iantruslove/e72455aa468938ba78c16f29bbec8f9a to your computer and use it in GitHub Desktop.
Save iantruslove/e72455aa468938ba78c16f29bbec8f9a to your computer and use it in GitHub Desktop.
Ring Webapp Example

Ring Webapp Example


Links


"Snippets" project: build a web service for sharing text snippets

(gist.github.com anyone?)

Interface:

$ curl -X POST \
    -H "content-type: text/plain" \
    -d "Hello, world!" \
    http://server/snippets/
# Should return HTTP 302 with a Location header for us to follow

$ curl -X GET http://server/snippets/1234
Hello, world!

Snippets: Intro to Ring

  • Ring is a simple (conceptual) framework for building web applications.
    • It also has some useful code.
  • Request maps:
{:server-port 8080
 :server-name "localhost"
 :uri "/foo/bar/baz"
 :scheme :https
 :headers {"content-type" "text/plain"
 :body <some Java stream object>}
  • Response maps:
{:status 200
 :headers ["content-type" "text/plain"]
 :body "Hello, world!"}

Snippets: Ring Basics

  • Creating and modifying responses
    • The functions in ring.util.response are useful

Snippets: Ring Handlers

  • Straightforward requirements:
    • Take a single parameter (request map)
    • Return a response map
(fn [req]
  {:status 200 :body ""})  
  • Sketch out the GET and POST handlers...

Snippets: Storing snippets in an atom

  • reset! and swap!

Snippets: UUIDs

  • We need them, Java has them.

Snippets: Put it together

  • Write the rest of the code!

Snippets: Simple Extensions

  • Web pages
  • URL shortening
  • Durable datastore
  • Automatic content conversion (if possible)
    • e.g. JSON <--> EDN, JSON --> HTML, ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment