Convert Figma logo to code with AI

weavejester logocompojure

A concise routing library for Ring/Clojure

4,103
258
4,103
6

Top Related Projects

1,474

A fast data-driven routing library for Clojure/Script

3,808

Clojure HTTP server abstraction

The Pedestal Server-side Libraries

Quick Overview

Compojure is a concise routing library for Ring, a Clojure web application library. It provides a simple and expressive way to define routes and handle HTTP requests in Clojure web applications, making it easier to create RESTful APIs and web services.

Pros

  • Simple and intuitive syntax for defining routes
  • Seamless integration with Ring and other Clojure web libraries
  • Supports route nesting and parameter extraction
  • Lightweight and minimal, allowing for easy customization

Cons

  • Limited built-in features compared to full-stack web frameworks
  • May require additional libraries for more complex web applications
  • Learning curve for developers new to Clojure or functional programming
  • Less active development in recent years

Code Examples

  1. Basic route definition:
(ns myapp.handler
  (:require [compojure.core :refer :all]
            [compojure.route :as route]))

(defroutes app-routes
  (GET "/" [] "Hello World")
  (GET "/users/:id" [id] (str "User " id))
  (POST "/api/data" request (handle-data request))
  (route/not-found "Not Found"))
  1. Route nesting and middleware:
(defroutes api-routes
  (GET "/items" [] (get-items))
  (POST "/items" [] (create-item)))

(def app
  (-> (routes
        (context "/api" [] api-routes)
        (route/not-found "Not Found"))
      (wrap-json-response)
      (wrap-json-body)))
  1. Using destructuring in route handlers:
(defroutes app-routes
  (GET "/search" [q :<< params]
    (str "Searching for: " q))
  (GET "/user/:id" {{id :id} :params}
    (str "User ID: " id)))

Getting Started

To use Compojure in your Clojure project, add the following dependency to your project.clj file:

[compojure "1.7.0"]

Then, in your namespace, require Compojure:

(ns myapp.core
  (:require [compojure.core :refer :all]
            [compojure.route :as route]))

(defroutes app-routes
  (GET "/" [] "Hello, Compojure!")
  (route/not-found "Not Found"))

(def app app-routes)

This sets up a basic Compojure application with a single route. You can now run this application using a Ring-compatible web server like Jetty or HTTP Kit.

Competitor Comparisons

1,474

A fast data-driven routing library for Clojure/Script

Pros of Reitit

  • More performant routing, especially for large applications
  • Supports data-driven routing with coercion and validation
  • Better integration with Swagger and OpenAPI for API documentation

Cons of Reitit

  • Steeper learning curve due to more advanced features
  • Less established community compared to Compojure
  • May be overkill for simple applications

Code Comparison

Compojure routing example:

(defroutes app-routes
  (GET "/" [] "Hello World")
  (GET "/user/:id" [id] (str "User: " id))
  (POST "/api/data" {body :body} (process-data body))
  (route/not-found "Not Found"))

Reitit routing example:

(def router
  (ring/router
    [["/api"
      ["/ping" {:get (constantly {:status 200, :body "pong"})}]
      ["/user/:id" {:get {:parameters {:path {:id int?}}
                          :handler (fn [{{{:keys [id]} :path} :parameters}]
                                     {:status 200
                                      :body {:id id}})}}]]]))

Both Compojure and Reitit are popular routing libraries for Clojure web applications. Compojure is known for its simplicity and ease of use, making it a great choice for beginners and smaller projects. Reitit, on the other hand, offers more advanced features and better performance, making it suitable for larger and more complex applications.

3,808

Clojure HTTP server abstraction

Pros of Ring

  • More low-level and flexible, allowing for greater control over HTTP handling
  • Provides a comprehensive set of middleware for common web application tasks
  • Serves as the foundation for many Clojure web frameworks, including Compojure

Cons of Ring

  • Requires more boilerplate code for basic routing and request handling
  • Steeper learning curve for beginners compared to Compojure's simpler API
  • Less opinionated, which may lead to inconsistencies across different projects

Code Comparison

Ring example:

(defn handler [request]
  {:status 200
   :headers {"Content-Type" "text/html"}
   :body "Hello, World!"})

(def app
  (-> handler
      (wrap-params)
      (wrap-keyword-params)))

Compojure example:

(defroutes app
  (GET "/" [] "Hello, World!")
  (POST "/submit" [name]
    (str "Hello, " name "!")))

Ring provides a lower-level API for handling HTTP requests and responses, while Compojure offers a more concise and expressive way to define routes and handle requests. Ring's flexibility allows for more customization, but Compojure's simplicity makes it easier to get started with web development in Clojure. Both libraries can be used together, with Compojure building on top of Ring's core functionality.

The Pedestal Server-side Libraries

Pros of Pedestal

  • More comprehensive framework with built-in support for async processing and WebSocket
  • Better suited for building large-scale, production-ready applications
  • Offers more advanced routing capabilities and interceptors for request handling

Cons of Pedestal

  • Steeper learning curve due to its more complex architecture
  • Potentially overkill for simple web applications or APIs
  • Less flexibility compared to Compojure's minimalist approach

Code Comparison

Compojure route definition:

(defroutes app-routes
  (GET "/" [] "Hello World")
  (GET "/users/:id" [id] (str "User " id))
  (POST "/users" {body :body} (create-user body)))

Pedestal route definition:

(def routes
  #{["/greet" :get [respond-hello] :route-name :greet]
    ["/users/:id" :get [check-id respond-user] :route-name :get-user]
    ["/users" :post [check-user-body create-user] :route-name :create-user]})

Both Compojure and Pedestal are popular Clojure web frameworks, but they cater to different needs. Compojure is lightweight and easy to use, making it ideal for small to medium-sized projects. Pedestal, on the other hand, offers a more robust set of features and is better suited for building complex, scalable web applications. The choice between the two depends on the specific requirements of your project and your familiarity with Clojure web development.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Compojure

Build Status

Compojure is a small routing library for Ring that allows web applications to be composed of small, independent parts.

Installation

Add the following dependency to your project.clj file:

[compojure "1.7.1"]

Documentation

Community

Usage

This small Compojure application demonstrates creating a Ring handler from two routes:

(ns hello-world.core
  (:require [compojure.core :refer :all]
            [compojure.route :as route]))

(defroutes app
  (GET "/" [] "<h1>Hello World</h1>")
  (route/not-found "<h1>Page not found</h1>"))

Also refer to the Getting Started page on the wiki.

License

Copyright © 2024 James Reeves

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