最近研究了下clojure语言,和该语言的一些web库框架,ring,compujure做了一个简单的框架Road:
(defn render-test [ret tmt]
(-> (resp/response "------render----test------")
(#(resp/content-type %1 "text/plain"))))
(defn foo
"I don't do a whole lot."
[x]
(str "来自源码目录的参数:" x))
(defn handler [^Integer x]
{:$r render-test :text (str "hello world, road goes sucess!" (foo x))})
(defn home [req content ^Integer num]
{:hiccup "home.clj" :content (str "home" content) :num num})
(defroad road (GET "/web-test-0.1.0-SNAPSHOT-standalone/main" handler)
(GET "/web-test-0.1.0-SNAPSHOT-standalone/home/:num{\\d+}" home))
(defn -main [& args]
(log/info "---------log4j test-------")
(jetty/run-jetty road {:port 3000}))
https://github.com/zhujinxian/road
框架可以把URI直接映射为一个函数,函数的参数名字对应http传过来的参数的key对应的值,类型用tag修饰。默认string, 函数返回一个map该map被渲染器渲染后返回给浏览器。
可以直接达成jar包独立运行或放在nginx-clojure里跑,或者打包为war放在tomcat里。
这个框架是否足够简单,足够快速,给些建议和改进措施?
Judging from your title, the juxtaposition of ring and compojure does not seem to fully understand the design intent of the frameworks ring and compojure:
The design idea of ring is to only deal with the core request and response mapping issues (its session, cookie and other implementations can be replaced at will). URL parsing frameworks such as compojure are areas that ring intentionally leaves blank. In other words, the design idea of ring is to have other complementary libraries to make it easier to use. Because of this, it has become the basis for all clojure web frameworks (currently the manifest intends to supplement ring semantics for asynchronous processing, but the general idea is still compatible with ring). In the words of the ring author: one ring to bind them all. This Lord of the Rings commands other libraries at various levels such as URL parsing, encryption and decryption, and web server compatibility.
A very prosperous ecosystem can be grown based on this idea. For example, for url parsing, there are many libraries such as compojure, bidi, silk, mastache, etc., with similar goals but different methods; for display layer generation, there are various generation technologies such as enlive, laser, hiccup, selmar and so on. This kind of creativity often seems difficult to master for beginners, but it is similar to the unix command line. The power lies in the combination of each other, but there is a steep learning curve.
The road designed by the subject seems to be hoping to combine the above-mentioned capabilities, and this is exactly what the ring-based clojure web ecosystem hopes to accomplish by division of labor. This is the reason why you posted many promotions but received very few responses.
Feel like you need this RESTful API design best practice