summaryrefslogtreecommitdiff
path: root/examples/jank-test-project/src/example/main.jank
diff options
context:
space:
mode:
Diffstat (limited to 'examples/jank-test-project/src/example/main.jank')
-rw-r--r--examples/jank-test-project/src/example/main.jank56
1 files changed, 56 insertions, 0 deletions
diff --git a/examples/jank-test-project/src/example/main.jank b/examples/jank-test-project/src/example/main.jank
new file mode 100644
index 0000000..6744b02
--- /dev/null
+++ b/examples/jank-test-project/src/example/main.jank
@@ -0,0 +1,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!")