aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/clj/queue_api/config.clj12
-rw-r--r--src/clj/queue_api/core.clj48
-rw-r--r--src/clj/queue_api/handler.clj19
-rw-r--r--src/clj/queue_api/middleware.clj15
-rw-r--r--src/clj/queue_api/routes/services.clj44
5 files changed, 138 insertions, 0 deletions
diff --git a/src/clj/queue_api/config.clj b/src/clj/queue_api/config.clj
new file mode 100644
index 0000000..30ed9a5
--- /dev/null
+++ b/src/clj/queue_api/config.clj
@@ -0,0 +1,12 @@
+(ns queue-api.config
+ (:require [cprop.core :refer [load-config]]
+ [cprop.source :as source]
+ [mount.core :refer [args defstate]]))
+
+(defstate env
+ :start
+ (load-config
+ :merge
+ [(args)
+ (source/from-system-props)
+ (source/from-env)]))
diff --git a/src/clj/queue_api/core.clj b/src/clj/queue_api/core.clj
new file mode 100644
index 0000000..1647375
--- /dev/null
+++ b/src/clj/queue_api/core.clj
@@ -0,0 +1,48 @@
+(ns queue-api.core
+ (:require [queue-api.handler :as handler]
+ [luminus.repl-server :as repl]
+ [luminus.http-server :as http]
+ [queue-api.config :refer [env]]
+ [clojure.tools.cli :refer [parse-opts]]
+ [clojure.tools.logging :as log]
+ [mount.core :as mount])
+ (:gen-class))
+
+(def cli-options
+ [["-p" "--port PORT" "Port number"
+ :parse-fn #(Integer/parseInt %)]])
+
+(mount/defstate ^{:on-reload :noop} http-server
+ :start
+ (http/start
+ (-> env
+ (assoc :handler #'handler/app)
+ (update :io-threads #(or % (* 2 (.availableProcessors (Runtime/getRuntime)))))
+ (update :port #(or (-> env :options :port) %))))
+ :stop
+ (http/stop http-server))
+
+(mount/defstate ^{:on-reload :noop} repl-server
+ :start
+ (when-let [nrepl-port (env :nrepl-port)]
+ (repl/start {:port nrepl-port}))
+ :stop
+ (when repl-server
+ (repl/stop repl-server)))
+
+
+(defn stop-app []
+ (doseq [component (:stopped (mount/stop))]
+ (log/info component "stopped"))
+ (shutdown-agents))
+
+(defn start-app [args]
+ (doseq [component (-> args
+ (parse-opts cli-options)
+ mount/start-with-args
+ :started)]
+ (log/info component "started"))
+ (.addShutdownHook (Runtime/getRuntime) (Thread. stop-app)))
+
+(defn -main [& args]
+ (start-app args))
diff --git a/src/clj/queue_api/handler.clj b/src/clj/queue_api/handler.clj
new file mode 100644
index 0000000..97afd48
--- /dev/null
+++ b/src/clj/queue_api/handler.clj
@@ -0,0 +1,19 @@
+(ns queue-api.handler
+ (:require [compojure.core :refer [routes wrap-routes]]
+ [queue-api.routes.services :refer [service-routes]]
+ [compojure.route :as route]
+ [queue-api.env :refer [defaults]]
+ [mount.core :as mount]
+ [queue-api.middleware :as middleware]))
+
+(mount/defstate init-app
+ :start ((or (:init defaults) identity))
+ :stop ((or (:stop defaults) identity)))
+
+(mount/defstate app
+ :start
+ (middleware/wrap-base
+ (routes
+ #'service-routes
+ (route/not-found
+ "page not found"))))
diff --git a/src/clj/queue_api/middleware.clj b/src/clj/queue_api/middleware.clj
new file mode 100644
index 0000000..6d1ea43
--- /dev/null
+++ b/src/clj/queue_api/middleware.clj
@@ -0,0 +1,15 @@
+(ns queue-api.middleware
+ (:require [queue-api.env :refer [defaults]]
+ [queue-api.config :refer [env]]
+ [ring.middleware.flash :refer [wrap-flash]]
+ [immutant.web.middleware :refer [wrap-session]]
+ [ring.middleware.defaults :refer [site-defaults wrap-defaults]]))
+
+(defn wrap-base [handler]
+ (-> ((:middleware defaults) handler)
+ wrap-flash
+ (wrap-session {:cookie-attrs {:http-only true}})
+ (wrap-defaults
+ (-> site-defaults
+ (assoc-in [:security :anti-forgery] false)
+ (dissoc :session)))))
diff --git a/src/clj/queue_api/routes/services.clj b/src/clj/queue_api/routes/services.clj
new file mode 100644
index 0000000..cc8ac04
--- /dev/null
+++ b/src/clj/queue_api/routes/services.clj
@@ -0,0 +1,44 @@
+(ns queue-api.routes.services
+ (:require [ring.util.http-response :refer :all]
+ [compojure.api.sweet :refer :all]
+ [schema.core :as s]))
+
+(defapi service-routes
+ {:swagger {:ui "/swagger-ui"
+ :spec "/swagger.json"
+ :data {:info {:version "1.0.0"
+ :title "Sample API"
+ :description "Sample Services"}}}}
+
+ (context "/api" []
+ :tags ["thingie"]
+
+ (GET "/plus" []
+ :return Long
+ :query-params [x :- Long, {y :- Long 1}]
+ :summary "x+y with query-parameters. y defaults to 1."
+ (ok (+ x y)))
+
+ (POST "/minus" []
+ :return Long
+ :body-params [x :- Long, y :- Long]
+ :summary "x-y with body-parameters."
+ (ok (- x y)))
+
+ (GET "/times/:x/:y" []
+ :return Long
+ :path-params [x :- Long, y :- Long]
+ :summary "x*y with path-parameters"
+ (ok (* x y)))
+
+ (POST "/divide" []
+ :return Double
+ :form-params [x :- Long, y :- Long]
+ :summary "x/y with form-parameters"
+ (ok (/ x y)))
+
+ (GET "/power" []
+ :return Long
+ :header-params [x :- Long, y :- Long]
+ :summary "x^y with header-parameters"
+ (ok (long (Math/pow x y))))))