Skip to content

Instantly share code, notes, and snippets.

@tolitius
Created July 3, 2020 22:00
Show Gist options
  • Save tolitius/6a6dabfee5df0c6baa97d604560d460d to your computer and use it in GitHub Desktop.
Save tolitius/6a6dabfee5df0c6baa97d604560d460d to your computer and use it in GitHub Desktop.
an example of starting a server with reitit routes

separate namespace for routes:

(ns hubble.routes
  (:require [reitit..]))

(defn make-routes [config]
  [["/api/hubble" {:swagger {:tags ["ops"]}}
    ;; ....

and a separate namespace for the server:

(ns hubble.server
  (:require [org.httpkit.server :as server]
            [funcade.middleware.reitit :as fun]
            [mount.core :refer [defstate]]
            [hubble.env :as env]
            [hubble.routes :as routes]
            ;;...
            )

(defn app [{:keys [security] :as config}]
  (ring/ring-handler
    (ring/router
      (routes/make-routes config)
      {:exception pretty/exception
       :conflicts nil
       :data      {:coercion   (malli-coercion/create)
                   :muuntaja   m/instance
                   :middleware [swagger/swagger-feature
                                parameters/parameters-middleware
                                custom-exception-middleware
                                ;; ...
                                (fun/wrap-jwt-authentication security)
                                fun/scope-middleware]}})
    (ring/routes
      (swagger-ui/create-swagger-ui-handler
        {:path "/api/hubble"
         :url  "/api/hubble/swagger.json"})
      (ring/create-default-handler))))

(defn start-server [{:keys [server] :as config}]
  (let [server (-> (app config)
                   (server/run-server server))]
    {:stop-server server}))
    
(defstate ^{:on-reload :noop} http-server
          :start (start-server env/config)
          :stop ((:stop-server http-server)))    

and to start/stop it:

(mount/start)
(mount/stop)

or just start/stop the server by itself without affecting other components:

(mount/start #'hubble.server/http-server)
(mount/stop #'hubble.server/http-server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment