handler.clj 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. (ns hello.handler
  2. (:require [com.appsflyer.donkey.core :as donkey-core]
  3. [com.appsflyer.donkey.server :as donkey-server]
  4. [com.appsflyer.donkey.middleware.json :as json])
  5. (:gen-class)
  6. (:import (io.vertx.core.impl.cpu CpuCoreSensor)))
  7. (defn- json-handler [_ res _]
  8. (res {:status 200
  9. :headers {"content-type" "application/json"}
  10. :body {"message" "Hello, World!"}}))
  11. (def ^:private json-route {:methods [:get]
  12. :middleware [(json/make-serialize-middleware)]
  13. :path "/json"
  14. :handler json-handler})
  15. (def ^:private hello-world-body
  16. (bytes (byte-array (map (comp byte int) "Hello, World!"))))
  17. (def ^:private hello-world-response
  18. {:status 200
  19. :headers {"content-type" "text/plain"}
  20. :body hello-world-body})
  21. (defn- hello-world-handler [_ res _]
  22. (res hello-world-response))
  23. (def ^:private hello-world-route {:methods [:get]
  24. :path "/plaintext"
  25. :handler hello-world-handler})
  26. (defn -main [& _]
  27. (let [concurrency (max 1 (- (CpuCoreSensor/availableProcessors) 1))]
  28. (->
  29. (donkey-core/create-donkey
  30. {:event-loops concurrency})
  31. (donkey-core/create-server {:port 8080
  32. :routes [hello-world-route json-route]
  33. :instances concurrency
  34. :compression false
  35. :decompression false
  36. :accept-backlog 20000
  37. :date-header true
  38. :server-header true
  39. :keep-alive true})
  40. (donkey-server/start-sync))))