summaryrefslogtreecommitdiff
path: root/src/ruuter/core.clj
diff options
context:
space:
mode:
authorAsko Nõmm <asko@bien.ee>2021-10-02 19:58:15 -0300
committerAsko Nõmm <asko@bien.ee>2021-10-02 19:58:15 -0300
commita4d6e488859f14db1547975b011cc3559e5c47ef (patch)
tree30d6c427be1a82d268700a3e11a099da6ada014d /src/ruuter/core.clj
parentb020883897f50c35af3a8774a911fe7c69631bce (diff)
Make Ruuter HTTP server agnostic
Diffstat (limited to 'src/ruuter/core.clj')
-rw-r--r--src/ruuter/core.clj25
1 files changed, 8 insertions, 17 deletions
diff --git a/src/ruuter/core.clj b/src/ruuter/core.clj
index b441b1f..f811802 100644
--- a/src/ruuter/core.clj
+++ b/src/ruuter/core.clj
@@ -1,6 +1,5 @@
(ns ruuter.core
- (:require [clojure.string :as string]
- [org.httpkit.server :as http])
+ (:require [clojure.string :as string])
(:gen-class))
@@ -75,29 +74,21 @@
:body "Not found."}))
-(defn- router
+(defn route
"For a given collection of `routes` and the current HTTP request as
`req`, will attempt to match a route with the HTTP request, which it
- will then try to return a response for.
+ will then try to return a response for. The only requirement for `req`
+ is to contain both a `uri` and `request-method` key. First should match
+ the request path (like the paths defined in routes) and the second
+ should match the request method used by the HTTP server you pass this fn to.
If no route matched for a given HTTP request it will try to find a
route with `:not-found` as its `:path` instead, and return the response
- for that."
+ for that, and if that route was also not found, will return a built-in
+ 404 response instead."
[routes {:keys [uri request-method] :as req}]
(if-let [route (match-route routes uri request-method)]
(route+req->response route req)
(route+req->response (->> routes
(filter #(= :not-found (:path %)))
first) req)))
-
-
-(defn route!
- "Starts an HTTP server which will then try to find a matching route for
- each request from within the given collection of `routes`. Takes an
- optional `opts` map, which corresponds directly to http-kit's config,
- allowing you to specify things like `{:port 8080}` and so on."
- ([routes]
- (route! routes {:port 9600}))
- ([routes opts]
- (println "Starting HTTP server on port " (:port opts))
- (http/run-server #(router routes %) opts))) \ No newline at end of file