-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi.clj
65 lines (51 loc) · 1.93 KB
/
api.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(ns example.api
"Example HTTP app using full.boostrap dependencies
Contains 2 endpoints:
GET /hello/:username - returns greeting for the username
GET /github/:username - returns github user info for the given username"
(:require [full.http.server :as serv]
[full.core.log :as log]
[full.core.config :as conf]
[full.async :refer [<? go-try]]
[full.http.client :refer [req>]]
[full.json :refer [read-json]]
[full.core.dev :refer [start-nstracker]]
[ring.middleware.keyword-params :refer [wrap-keyword-params]]
[ring.middleware.params :refer [wrap-params]])
(:gen-class))
(def request-logger (org.slf4j.LoggerFactory/getLogger "example.request"))
(def port (conf/opt :port :default 8080))
(defn github-info> [username]
(go-try
(try
; In case the request fails with a 404, return nil.
(<? (req> {:base-url "https://api.github.com" :resource (str "users/" username)}))
(catch Exception _))))
(defn format-github-user
"Returns a subset of GitHub data."
[api-response]
(log/info api-response)
(select-keys api-response [:name :company :blog]))
(serv/defroutes app-routes
; Returns hello
(serv/GET "/hello/:username"
{:keys [params]} {:body (str "Hello, " (:username params)) :status 200})
; Returns a subset of GitHub user info
(serv/GET "/github/:username" {:keys [params]}
(if-let [user-info (some-> (:username params) github-info> <? format-github-user)]
{:status 200 :body user-info}
{:status 404 :body {:message "user not found"}})))
(defn api
"API middleware"
[routes]
(-> routes
(serv/json-response>)
(serv/log-track-request> :logger request-logger)
wrap-keyword-params
wrap-params))
(defn -main [& _]
(conf/configure)
(log/configure)
(start-nstracker)
(log/info "Running on" @port)
(serv/run-server (api #'app-routes) {:port @port :max-line 8193}))