fudo-service/src/clj/fudo/server.clj

63 lines
2.4 KiB
Clojure

(ns fudo.server
(:require [fudo.server.config :as config]
[fudo.db.auth-db :as auth-db]
[fudo.server.service :as service]
[fudo.server.services :as services]
[fudo.server.middleware :as middleware]
[ring.adapter.jetty :refer [run-jetty]]
[slingshot.slingshot :refer [try+ throw+]]
[com.stuartsierra.component :as component]
[clojure.spec.alpha :as s]))
(defn- get-file-contents [filename]
(when (not (.exists (clojure.java.io/as-file filename)))
(throw+ {:type :server/file-error
:class :server-error
:msg (format "File doesn't exist: %s"
filename)}))
(clojure.string/trim (slurp filename)))
(s/fdef get-file-contents
:args (s/cat :filename string?)
:ret (s/nilable string?))
(defn start-server [debug config fudo-services]
(let [api-handler (service/generate-handler fudo-services)
this-host (config/get-hostname)
service-config (config/get-path config (format "services::%s::service" this-host))
server-port (config/get-env-or config
"FUDO_SERVICE_PORT"
(:port service-config))
keystore (config/get-env-or config
"FUDO_SERVER_KEYSTORE"
(:keystore service-config))
key-password (config/get-env-or config
"FUDO_SERVER_KEYSTORE_PASSWD"
(get-file-contents
(:keystore_passwd_file service-config)))]
(println (format "Starting server on port %s" server-port))
(run-jetty api-handler
{:ssl-port server-port
:http? false
:ssl? true
:keystore keystore
:key-password key-password})))
(defrecord FudoServer [debug config fudo-services server]
component/Lifecycle
(start [this]
(->FudoServer (:debug this)
(:config this)
(:fudo-services this)
(start-server (:debug this)
(:config this)
(:fudo-services this))))
(stop [this]
(.stop (:server this))))
(defn init [debug]
(->FudoServer debug nil nil nil))