-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.clj
84 lines (75 loc) · 3.28 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(ns andrewslai.clj.main
(:gen-class)
(:require [aleph.http :as http]
[andrewslai.clj.http-api.middleware :as mw]
[andrewslai.clj.init.env :as env]
[cheshire.core :as json]
[clojure.string :as string]
[taoensso.timbre :as log]
[taoensso.timbre.appenders.core :as appenders]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Logging
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- json-log-output
"Useful when shipping logs via FluentBit (the recommended log router for AWS
ECS applications)
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/firelens-using-fluentbit.html"
[{:keys [level msg_ instant ?ns-str ?file ?line] :as data}]
(let [event (force msg_)
ns-name (or ?ns-str ?file "?")
line-num (or ?line "?")]
(json/generate-string {:timestamp instant
:level level
:ns ns-name
:request-id mw/*request-id*
:line (format "%s:%s" ns-name line-num)
:message (string/replace event #"\n" " ")})))
(defn disable-json-logging?
"Useful when running locally to avoid JSON structured logs."
[env]
(parse-boolean (get env "DISABLE_JSON_LOGGING")))
(defn initialize-logging!
[env]
(log/merge-config!
(cond-> {:min-level :info
:output-fn json-log-output
:appenders {:spit (appenders/spit-appender {:fname "log.txt"})}}
(disable-json-logging? env) (dissoc :output-fn))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Running the server
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn start-application!
[env]
(let [system-components (env/start-system! env)
port 5000]
(log/infof "Hello! Starting andrewslai on port %s" port)
(initialize-logging! env)
(-> system-components
(env/prepare-for-virtual-hosting)
(env/make-http-handler)
(http/start-server {:port port}))))
(defn -main
"Start a server and run the application"
[& args]
(start-application! (into {} (System/getenv))) ;; b/c getenv returns java.util.Collections$UnmodifiableMap
)
(comment
(log/with-merged-config
{:output-fn json-log-output}
(log/info (->> {:a "bbig long bits of stuff and more and more"
:c {:d :foo :e "bar"}
:f {:a "b" :c ({:d :foo :e "bar"})}}
clojure.pprint/pprint
with-out-str)))
(def x
(env/start-system! {"ANDREWSLAI_DB_TYPE" "embedded-h2"
"ANDREWSLAI_AUTH_TYPE" "custom-authenticated-user"
"ANDREWSLAI_AUTHORIZATION_TYPE" "public-access"
"ANDREWSLAI_STATIC_CONTENT_TYPE" "none"
"ANDREWSLAI_WEDDING_AUTH_TYPE" "custom-authenticated-user"
"ANDREWSLAI_WEDDING_AUTHORIZATION_TYPE" "public-access"
"ANDREWSLAI_WEDDING_STATIC_CONTENT_TYPE" "none"
}
))
(env/prepare-for-virtual-hosting x)
)