Skip to content

Commit

Permalink
Require virtual threads
Browse files Browse the repository at this point in the history
Update microhttp_ring_adapter.clj

Update microhttp_ring_adapter.clj
  • Loading branch information
bowbahdoe committed May 15, 2022
1 parent 6920350 commit d7af6cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Microhttp Ring Adapter

# THIS REQUIRES VIRTUAL THREADS. JDK 19 + --enable-preview
## What
Adapter for using [microhttp](https://github.com/ebarlas/microhttp)
as a ring server.
Expand All @@ -15,7 +16,7 @@ I am confident it is as production ready as microhttp.
## deps.edn

```clojure
io.github.bowbahdoe/microhttp-ring-adapter {:git/tag "v0.0.1"}
io.github.bowbahdoe/microhttp-ring-adapter {:git/tag "v0.0.2"}
```

## Usage
Expand Down
21 changes: 13 additions & 8 deletions src/dev/mccue/microhttp_ring_adapter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,25 @@
out)
(Response. (:status ring-response)
(or (status->reason (:status ring-response)) "IDK")
(reduce (fn [header-list [k v]]
(conj header-list (Header. k v)))
[]
(:headers ring-response))
(mapv #(Header. (key %) (val %))
(:headers ring-response))
(.toByteArray out))))

(defn- ring-handler->Handler
[{:keys [host port]} ring-handler]
(reify Handler
(handle [_ microhttp-request callback]
(let [ring-request (->req {:host host
:port port} microhttp-request)
microhttp-response (<-res (ring-handler ring-request))]
(.accept callback microhttp-response)))))
(let [ring-request (->req {:host host
:port port} microhttp-request)]
(Thread/startVirtualThread
(fn []
(.accept callback
(<-res (try
(ring-handler ring-request)
(catch Exception e
{:status 500
:headers {}
:body (.getMessage e)}))))))))))

(defn create-microhttp-event-loop
([handler]
Expand Down

0 comments on commit d7af6cd

Please sign in to comment.