blob: 6744b0214187c65520d3294542e252b70d479a52 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
;; Minimal Jank example using Ruuter for HTTP routing.
;;
;; Run with:
;; jank run --module-path ../../src:src src/example/main.jank
;;
;; (from the examples/jank-test-project/ directory)
(require '[ruuter.core :as ruuter])
(println "Ruuter on Jank - Example")
(println "========================")
(println "")
;; Define routes
(def routes
[{:path "/"
:method :get
:response {:status 200 :body "Welcome home!"}}
{:path "/hello/:name"
:method :get
:response (fn [req]
{:status 200
:body (str "Hello, " (get-in req [:params :name]) "!")})}
{:path "/api/users/:id"
:method :get
:response (fn [req]
{:status 200
:body (str "User #" (get-in req [:params :id]))})}
{:path "/files/:path*"
:method :get
:response (fn [req]
{:status 200
:body (str "File: " (get-in req [:params :path]))})}
{:path :not-found
:response {:status 404 :body "Page not found."}}])
;; Simulate HTTP requests
(defn simulate [method uri]
(let [resp (ruuter/route routes {:uri uri :request-method method})]
(println (str " " (name method) " " uri " -> " (:status resp) " " (:body resp)))))
(println "Simulating requests:")
(println "")
(simulate :get "/")
(simulate :get "/hello/jank")
(simulate :get "/api/users/42")
(simulate :get "/files/docs/readme.txt")
(simulate :get "/nonexistent")
(simulate :post "/hello/world")
(println "")
(println "Done!")
|