Skip to content

Commit

Permalink
expose res and effect solution; alpha release
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenYong committed Mar 24, 2020
1 parent 3b52554 commit bdf2f61
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 140 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,33 @@ Skir - an over-simplified HTTP Node.js server toolkit
[![Clojars Project](https://img.shields.io/clojars/v/mvc-works/skir.svg)](https://clojars.org/mvc-works/skir)

```edn
[mvc-works/skir "0.0.7-a3"]
[mvc-works/skir "0.0.8-a1"]
```

WIP...

```clojure
(require '[skir.core :as skir])

(defn on-request! [req]
(defn on-request! [req-edn res]
{:code 200
:message "OK"
:headers {:Content-Type "application/edn"}
:body {:message "Hello World!"}})

(skir/create-server! #(on-request! %))
(skir/create-server! #(on-request! %1 %2))
```

Core logic:

```clojure
(cond
(map? response) (write-response! res response)
(fn? response) (response (fn [response-data] (write-response! res response-data)))
(promise? response) (.then response (fn [result] (write-response! res result)))
(chan? response) (go (write-response! res (<! response)) (close! response))
(= response :effect) (comment "Done with effect")
:else (do (println "Response:" response) (throw (js/Error. "Unrecognized response!"))))
```

### License
Expand Down
46 changes: 41 additions & 5 deletions calcit.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1534560244378) (:text "|#()") (:id |Kdo_cNCi5H)
|j $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1534560244378) (:text |render!) (:id |SgJv90nvio)
|r $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1534560244378) (:text |%) (:id |ZuuD5Zld9k)
|r $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585018631649) (:text |%1) (:id |ZuuD5Zld9k)
|v $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585018628962) (:text |%2) (:id |g-u_98pxE)
|r $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1572594319088) (:id |oT55UfAcE5)
:data $ {}
|D $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1572594356315) (:text |;) (:id |eEX5lEamKD)
Expand Down Expand Up @@ -157,6 +158,7 @@
|r $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1534218175967) (:id |QqMIlRcsml)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1534218184482) (:text |req) (:id |veJG-OOzH)
|j $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585018639561) (:text |res) (:id |Je0pcesFCz)
|v $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1534219157791) (:id |TS2BNAIAdr)
:data $ {}
|D $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1534219159969) (:text |do) (:id |MvSTjdzvRS)
Expand Down Expand Up @@ -371,6 +373,20 @@
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1534227679519) (:text |:body) (:id |FA2qu8Wi0pleaf)
|j $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1534227889370) (:text "|\"slow response finished!") (:id |5WEGKVyjY7)
|mt $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1585019091096)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585019092881) (:text "|\"effect") (:id |ghL4CmuJVleaf)
|j $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1585019093122)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585019096637) (:text |do) (:id |EXeFv0UrP)
|j $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585019098845) (:text |:effect) (:id |tjUNDp2wt)
|b $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1585019099532)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585019104991) (:text |.end) (:id |n3kIqGYn1)
|j $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585019105894) (:text "|\"Res") (:id |1O_bsQtLj)
:id |UzuHvBROJi
:id |gdZNcGbjK
:id |ghL4CmuJV
|mr $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1535651382993) (:id |6oAsy-GJhL)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1535651386188) (:text "|\"channel") (:id |6oAsy-GJhLleaf)
Expand Down Expand Up @@ -516,11 +532,16 @@
|Q $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1583508068065)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1583508983668) (:text |js/console.log) (:id |xabg-6hCleaf)
|j $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1583509105622)
|j $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1585018608781)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1583508968307) (:text |:original-request) (:id |qBg4PkKe)
|j $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1583508071022) (:text |req) (:id |DwgGjM0ve)
:id |eddWz2qCc
|T $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1583509105622)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1583508968307) (:text |:original-request) (:id |qBg4PkKe)
|j $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1583508071022) (:text |req) (:id |DwgGjM0ve)
:id |eddWz2qCc
|D $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585018609296) (:text |;) (:id |wsiQzjmgcdleaf)
:id |OEy1kwCVoZ
|r $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585018606985) (:text |res) (:id |P_lyTuP6L)
:id |xabg-6hC
|router-rules $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1534227420812) (:id |HWQg132JKE)
:data $ {}
Expand Down Expand Up @@ -1161,6 +1182,7 @@
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1535652004344) (:text |handler) (:id |w5VTlQd5gK)
|j $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1535652004344) (:text |edn-req) (:id |HUNoLb8Zji3)
|r $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585018584997) (:text |res) (:id |OrZjkiBn-0)
|v $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1535652004344) (:id |8D_JrBozxIN)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1535652004344) (:text |cond) (:id |AtOMaCMppen)
Expand Down Expand Up @@ -1255,6 +1277,20 @@
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1535652004344) (:text |js/Error.) (:id |CcXr0JivzJp)
|j $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1535652004344) (:text "|\"Unrecognized response!") (:id |W6rKbSqAQoM)
|w $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1585019044402)
:data $ {}
|T $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1585019019258)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585019020239) (:text |=) (:id |6638oK3G5leaf)
|j $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585019024344) (:text |response) (:id |303DW4dzUU)
|r $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585019038156) (:text |:effect) (:id |vmHn8wncNm)
:id |6638oK3G5
|j $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1585019045073)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585019049437) (:text |;) (:id |Y8xn2Z6dyleaf)
|j $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1585019425779) (:text "|\"Done with effect") (:id |-RtwPuYQ_8)
:id |Y8xn2Z6dy
:id |0sSnW_dZy
|req->edn $ {} (:type :expr) (:by |-OxUkFUX3) (:at 1534220571911) (:id |5uBAvESUub)
:data $ {}
|T $ {} (:type :leaf) (:by |-OxUkFUX3) (:at 1534220577440) (:text |defn) (:id |APNMz8SePC)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"license": "MIT",
"devDependencies": {
"cheerio": "^1.0.0-rc.3",
"shadow-cljs": "^2.8.90",
"shadow-cljs": "^2.8.93",
"source-map-support": "^0.5.16",
"ws": "^7.2.1"
"ws": "^7.2.3"
},
"dependencies": {}
}
2 changes: 1 addition & 1 deletion release.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:version "0.0.7"
{:version "0.0.8-a1"
:group-id "mvc-works"
:artifact-id "skir"
:skip-tag true
Expand Down
7 changes: 4 additions & 3 deletions src/skir/app/main.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
(def router-rules
{"home" [], "callback" [], "html" [], "json" [], "edn" [], "promise" [], "channel" []})

(defn render! [req]
(defn render! [req res]
(do
(println)
(comment println "Requests:" (pr-str req))
(comment println "Url:" (:url req))
(js/console.log (:original-request req))
(js/console.log (comment (:original-request req)) res)
(let [router (parse-address (:url req) router-rules)
page (get-in router [:path 0])
parse-result (match-path (:url req) "a/:b")]
Expand Down Expand Up @@ -52,6 +52,7 @@
(<! (timeout 4000))
(>! delayed-message {:code 200, :headers {}, :body "message from channel"}))
delayed-message)
"effect" (do (.end "Res") :effect)
nil {:code 200, :message "OK, default page", :headers {}, :body "Home page"}
{:code 404,
:message "Page not found",
Expand All @@ -76,7 +77,7 @@

(defn main! []
(skir/create-server!
#(render! %)
#(render! %1 %2)
(comment
{}
(:after-start (fn [options] (println "options" options) (comment run-task!))))))
Expand Down
3 changes: 2 additions & 1 deletion src/skir/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@
:else (.stringify js/JSON body)))))

(defn handle-request! [req res handler]
(let [edn-req (req->edn req), response (handler edn-req)]
(let [edn-req (req->edn req), response (handler edn-req res)]
(cond
(map? response) (write-response! res response)
(fn? response) (response (fn [response-data] (write-response! res response-data)))
(promise? response) (.then response (fn [result] (write-response! res result)))
(chan? response) (go (write-response! res (<! response)) (close! response))
(= response :effect) (comment "Done with effect")
:else (do (println "Response:" response) (throw (js/Error. "Unrecognized response!"))))))

(defn create-server!
Expand Down

0 comments on commit bdf2f61

Please sign in to comment.