Skip to content

Instantly share code, notes, and snippets.

@haywoood
Created January 14, 2020 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haywoood/5cc8e7f2744bee4aadf7cd25f98470f2 to your computer and use it in GitHub Desktop.
Save haywoood/5cc8e7f2744bee4aadf7cd25f98470f2 to your computer and use it in GitHub Desktop.
(ns cloud-billing.api
(:require [cheshire.core :as json]
[clojure.java.io :refer [reader resource]]
[cloud-billing.config :as config]
[io.pedestal.http :as server]
[io.pedestal.http.route :as route]))
(defn parse-stream
“A helper that turns a body-stream into a map by consuming it and parsing it as
json.”
[body-stream]
(json/parse-stream (reader body-stream) true))
(defn respond-hello
“A way to just check if it’s listening, useful for stuff like k8s health”
[request]
{:status 200 :body “cloud-billing api is up”})
(def routes
#{[“/ping” :get respond-hello :route-name :hello]})
(def service-default
{::server/routes routes
;; resource-path doesn’t work and I’m not figuring out why on this PR
;; because we don’t need to serve UI yet
::server/resource-path “/static”
::server/port 8890
::server/type :jetty
::server/container-options {:h2c? true
:h2? false
:ssl? false
;; let’s not ssl yet, for now
;; :ssl? true
;; :ssl-port 8443
;; :keystore “keystore.jks”
;; :key-password “password”
}
:env :prod})
(defonce server-atom (atom nil))
(defn start-dev []
(reset! server-atom
(-> service-default
(merge {:env :dev
;; if config overrides the port use that instead
::server/port (get-in (config/config)
[:api :port]
(::server/port service-default))
;; do not block starting thread
::server/join? false
;; reloadable routes
::server/routes #(route/expand-routes (deref #’routes))
;; in dev we don’t care about origins
::server/allowed-origins {:creds true :allowed-origins
(constantly true)}
;; in dev we don’t care about content security policies
::server/secure-headers {:content-security-policy-settings
{:object-src “‘none’“}}})
server/default-interceptors
server/dev-interceptors
server/create-server
server/start)))
(defn stop-dev []
(server/stop @server-atom))
(defn restart []
(stop-dev)
(start-dev))
;; Note that prod uses the full, CLI’d, vaulted, etc’d, config passed in from
;; the command-fn as an argument.
(defn start-prod [conf]
(reset! server-atom
(-> service-default
(merge {:env :prod
;; if config overrides the port use that instead
::server/port (get-in conf
[:api :port]
(::server/port service-default))
;; prod blocks starting thread
::server/join? true
;; reloadable routes
::server/routes #(route/expand-routes (deref #’routes))
;; TODO: decide if we want these in prod mode?
;; in dev we don’t care about origins
::server/allowed-origins {:creds true :allowed-origins
(constantly true)}
;; in dev we don’t care about content security policies
::server/secure-headers {:content-security-policy-settings
{:object-src “‘none’“}}})
server/default-interceptors
server/create-server
server/start)))
;; stopping prod is essentially synonymous with ending the entire process so
;; there is no explicit stop fn for it.
(comment
(start-dev)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment