Skip to content

Instantly share code, notes, and snippets.

@clonekim
Created August 12, 2020 01:14
Show Gist options
  • Save clonekim/d21d04db1b1c7549177c665167e4249b to your computer and use it in GitHub Desktop.
Save clonekim/d21d04db1b1c7549177c665167e4249b to your computer and use it in GitHub Desktop.
Boot httpkit using integrant
{:http {:port 9090 :handler #ig/ref :handler/app}
:handler/app {}}
(ns devtools.core
(:gen-class)
(:require [clojure.tools.logging :as log]
[integrant.core :as ig]
[org.httpkit.server :as http]
[compojure.core :refer :all]
[compojure.route :as route]
[ring.util.response :refer [response status]]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[ring.middleware.json :refer [wrap-json-body wrap-json-params wrap-json-response]]))
(def config
(ig/read-string (slurp (clojure.java.io/resource "config.edn"))))
(defn myip [req]
(log/debug req)
(response {:name :ring :ip (:remote-addr req)}))
(defroutes app
(GET "/" [] "Hello World !!")
(route/not-found "Not Found"))
(defn wrap-internal-error [handler]
(fn [req]
(try
(handler req)
(catch Throwable t
(log/error t)
(-> (response {:errors (or (.getMessage t) "Internal Server Error")})
(status 500))))))
(defmethod ig/init-key :http [_ {:keys [handler port]}]
; server를 생성하고 돌려줘야 함
(let [server (http/run-server handler {:port port})]
(log/info "Starting HTTP server on port" port)
server))
(defmethod ig/halt-key! :http [_ server]
; 첫번째는 :http
; 두번째 는 init-key에서 돌려준 값으로 server가 들어온다
(do
(server :timeout 100)
(log/info "HTTP server stopped")))
(defmethod ig/init-key :handler/app [_ _]
; 파라미터가 없어도 반드시 두개의 인자값을 넣어줘야 함
(wrap-defaults
(-> app
(wrap-json-body {:keywords true :bigdecimals? true})
wrap-json-params
wrap-internal-error
wrap-json-response)
site-defaults))
(defonce system (atom nil))
(defn start-system! []
(reset! system (ig/init config)))
(defn stop-system! []
(when-not (nil? @system)
(ig/halt! @system)
(reset! system nil)))
(defn -main []
(start-system!))
(defproject devtools "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.10.0"]
[org.clojure/tools.cli "1.0.194"]
[org.clojure/tools.logging "1.1.0"]
[ch.qos.logback/logback-classic "1.2.3"]
[cheshire "5.10.0"]
[integrant "0.8.0"]
[http-kit "2.4.0"]
[compojure "1.6.2"]
[ring/ring-core "1.8.1"]
[ring/ring-defaults "0.3.2"]
[ring/ring-json "0.5.0" :exclusions [cheshire]]]
:repositories {"maven" "http://10.46.44.5:8080/repository/maven-releases"}
:repl-options {:init-ns devtools.core}
:main devtools.core
:profiles
{:uberjar
{:omit-source true
:uberjar-name "apm.jar"
:uberjar-exclusions [#"META-INF/(leiningen|maven)"]
:aot :all}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment