code-reviews

sneakypeet 2016-12-30T14:25:29.000747Z

Hi all. I have been spending the last 2 days writing my first clojure code and would just like to get some feedback. All the code does is start a server that listens on 2 routes. On the topics route it will call the message-handler function that gets passed in. I would just like to know if I am on the write track, or if I should change my thinking in some way to be more clojure and less everything else 🙂 thank you

sneakypeet 2016-12-30T14:25:31.000748Z

(ns pubsub-watcher.listener
  (:require [org.httpkit.server :as server]
            [compojure.core :refer :all]
            [ring.middleware.defaults :refer :all]
            [pubsub-watcher.log :refer [log]]))

(defn- handle-new-message
  "Executes the message handler with the topic and message on a seperate thread and returns"
  [message-handler topic message]
  (future (message-handler topic message))
  (str "Messeage posted to " topic " topic"))

(defn- create-routes
  [message-handler]
  (routes
   (GET "/" [] "All Your Queues Are Belong To Us")
   (POST "/topics/:topic/message" {{topic :topic} :params body :body} (handle-new-message message-handler topic body))))

(defn- create-server
  [routes]
  (wrap-defaults routes site-defaults))

(defn- start-server
  [port server]
  (log (str "** Starting Server on Port " port))
  (server/run-server server {:port port}))

(defn start
  ([port message-handler]
   (let [start-server-on-port (partial start-server port)]
     (-> message-handler
         create-routes
         create-server
         start-server-on-port)))
  ([message-handler]
   (start 3333 message-handler)))