This weekend I made a small web framework in Clojure. It was written in less than 4 hours, is less than 100 lines of code, and it doesn't do much. Here's the code for it.
The framework uses Ring to process requests and return responses. Ring represents HTTP requests and responses as native Clojure data structures, more specifically as maps.
The entry point is `app` (defined in `project.clj`) which takes a request and spits out a response. Here's what the framework does:
- Listen to HTTP traffic on port 3000 (with the help of ring.adapter.jetty)
- Safely serve static files in nested directories
- Connects Clojure functions to routes (routes as data!)
- Deals with one implicit argument for urls of format "/resource/:id" (for example: /users/foo)
- Deal with GET and POST requests (with the help of ring.middleware.params for putting form params in a :params key)
Here's what it doesn't do:
- Everything else.
- Use no dependencies (wrap-params and run-jetty specifically).
That's all. Full code on Github: https://github.com/oskarth/webframework