[Erlang 0082] Erlang Web Servers & Frameworks


   Erlang應用程序需要暴露出來Web Service接口,你該如何處理?需要做復雜點的頁面展現你又將如何選擇?不同規模解決方案解決不同規模的問題,這里的問題其實是你要選擇哪個Web Server,你要選擇哪個Web Framework?

 

Erlang Web Servers 

   

 現在(2012-10-27)還處於活躍開發狀態的Web Server有:Mochiweb Yaws Cowboy

 

Mochiweb 

 

   一句話介紹: MochiWeb is an Erlang library for building lightweight HTTP servers.

   

    Mochiweb在Erlang項目中被當做一個Web組件被廣泛使用(比如RabbitMQ的plug-in),它的設計相當收斂,除了基礎的Web請求處理沒有提供特別復雜的功能集(后面會提到其它Web Server).目前我已經在兩個項目中使用了Mochiweb,得心應手.

   Mochiweb項目代碼有很多值得學習的地方,比如 mochiglobal [ 鏈接 ],Parameterized module [鏈接]今年夏天我把Mochiweb代碼打印了一份,看得很是過癮.這里有一篇實戰風格的入門文章:A practical introduction to MochiWeb - Alex Marandon[鏈接]

   使用Rebar很容易編譯運行,里面自帶一個簡單的Echo demo,你可以使用Rebar快速建立起來自己的站點框架,動手試試吧

   

  項目地址:https://github.com/mochi/mochiweb

 

Cowboy

   

   一句話介紹: Cowboy is a small, fast and modular HTTP server written in Erlang.

   使用Cowboy需要通過編寫Handler來定制如何處理Web請求.這是它設計上的一大特色.項目源碼自帶了N種版本的hello_world,也是可以非常快上手.編譯運行依然是rebar搞定,這個項目現在保持更新,每天都能收到github推送的代碼變更郵件. 

  項目地址:https://github.com/extend/cowboy

 

YAWS 

 

  一句話介紹:Yaws is a HTTP high perfomance 1.1 webserver particularly well suited for dynamic-content web applications.

  這個略顯怪異的名字是Yet another Webserver的縮寫.其提供的已經不是簡單的腳手架了,而是支持動態內容輸出,REST,文件上傳SOAP等等.在其官網上有豐富的文檔和樣例代碼.O'Reilly在2012年6月出版的 Building Web Applications with Erlang 一書中使用的就是YAWS.這本不足150頁的小冊子是很好的YAWS入門教程.估計國內出版社不會引進這樣一本冷冷的書,自己找電子版讀吧,很容易找到. 

 項目地址:http://yaws.hyber.org/

 

Misultin [停止更新]

 

  一句話介紹: Misultin development has been discontinued. 

  是的,這個項目已經不再繼續更新了;停止更新的原因是作者認為各個Web server項目有"too much duplication of efforts".作者在項目介紹中倒是簡單評價了Mochiweb和Cowboy:


  Mochiweb has been around the block for a while and it's proven solid in production, I can only recommend it for all basic webserver needs you might have. Cowboy has a very interesting approach since it allows to use multiple TCP and UDP protocols on top of a common acceptor pool. It is a very modern approach, is very actively maintained and many projects are starting to be built around it.  

 

項目地址:https://github.com/ostinelli/misultin

 

 

  看過Misultin作者的一番評論,其實可以回答不少人關於"用Mochiweb做Web項目很痛苦"的問題:如果你要做一個豐富多彩的Web站點,在Mochiweb提供的腳手架上,你要完成很多工作,顯然你需要其它選擇,呃,或許你需要選擇一個Web Framework了.對於大多數框架來說,其設計的總要目標就是開發效率和常見應用場景的支持.我們看看有哪些Erlang Web Framework可用吧!

 

Elrang Web Frameworks

  

   除了Asp.net MVC,RoR之外,其實還有很多Web Framework可用,看看Erlang世界里面的解決方案吧:

 

ChicagoBoss

 

  一句話介紹:Chicago Boss is a server framework inspired by Rails and written in Erlang.

 

  ChicagoBoss是現在非常活躍的Erlang Web Framework,在各種細節上都為新手准備的相當周到比如60秒快速上手教程什么的.甚至它說自己區別於其它Erlang Web Framework的就是" it is easy to set up and use."它之間Web Server是選擇的Misultin現在已經遷移到Cowboy.前端MVC架構,內置消息隊列BOSSMQ,數據存儲方面選擇性也比較多:Mnesia MongoDB Mysql PostgreSQL Riak ,Tokyo Tyrant;模板方案依然是使用ErlyDTL. 

  另外,我覺得ChicagoBoss的文案是這些項目里面寫的最棒的,直接命中你最想知道的兩個問題:

  "Chicago Boss is a server framework inspired by Rails and written in Erlang. It offers all the conveniences of modern web development, including Comet. What sets Chicago Boss apart from other non-Erlang frameworks is that it can handle large amounts of traffic without any drop in performance. What sets Chicago Boss apart from other Erlang frameworks is that it is easy to set up and use."

 

 項目地址:https://github.com/evanmiller/ChicagoBoss

 入門PDF: http://www.evanmiller.org/chicago-boss-tutorial.pdf

 

Nitrogen 
 

   一句話介紹:Nitrogen Web Framework is the fastest way to develop interactive web applications in full-stack Erlang.

   看群里面討論,采用Nitrogen的項目也不在少數,這個項目動態模板方案不是采用ErlyDTL,而是自己有一套解決方案Nitrogen records [鏈接].網站上的例程和Step by Step教程足夠詳細,入門應該比較快.

 

   項目地址:http://nitrogenproject.com/

 

Zotontic

  一句話介紹:Zotonic is the open source, high speed, real-time web framework  and content management system, built with Erlang.

   應該說Zotontic和其它框架的差異更多是在業務定位上了,提供了很多CMS直接可用的功能,查看其features列表能夠看到詳細介紹,注意:"Typically 10 times (and much more) faster than PHP content management systems." [鏈接]

   Zotontic構建在Mochiweb和PostgreSQL之上.

 

 項目地址:http://zotonic.com/

 

BeepBeep

 

  一句話介紹: BeepBeep is a simple web application framework for Mochiweb inspired by Rails and Merb

  BeepBeep 構建在 MochiWeb 和 ErlyDTL (后面會介紹) 基礎之上. 沿襲了mochiweb的優良傳統一鍵建站,基於ErlyDTL提供Django 模板的視圖展現.

   這個項目已經09年之后就沒有實質性的更新,最近一次更新是2010年更新了一下README,慎重選擇吧.

   還有一個項目ErlyWeb同樣是

項目地址:https://github.com/davebryson/beepbeep

 

 

ErlyWeb

 

  一句話介紹: ErlyWeb is a web development framework for Erlang. It lets you quickly and easily build database-driven webapps following the MVC pattern.

   這個項目也不多說了,已經5,6年沒有更新了,可惜了這個霸氣外露的名字了.

 

 項目地址: https://github.com/yariv/erlyweb

 

 

更多資料

 

  ChicagoBoss 項目組為上面的Web Framework做了一個詳細的比較,技術選型的時候,這個太有用了:

https://github.com/evanmiller/ChicagoBoss/wiki/Comparison-of-Erlang-Web-Frameworks 

還有一篇比較的文章:

A comparison between Misultin, Mochiweb, Cowboy, NodeJS and Tornadoweb
http://www.ostinelli.net/a-comparison-between-misultin-mochiweb-cowboy-nodejs-and-tornadoweb/

Stackoverflow上還有一篇討論:

http://stackoverflow.com/questions/1822518/current-state-of-erlang-web-development-frameworks-template-languages

 

Erlang Boss
 
http://www.evanmiller.org/chicago-boss-guide.html
 
 
Video Tutorial To Start Developing Web Applications on Erlang
 

 

番外篇 - ErlyDTL

 

 上面反復提到ErlyDTL,必須介紹一下,這也是一個基礎項目被廣泛使用,延續上面的模式:

 

 一句話介紹:ErlyDTL is an Erlang implementation of the Django Template Language.

 

 

Template file welcome.html:

  Welcome back, {{ name }}!

   You have {{ friends|length }} friends: {{ friends|join:", " }}

   Have some primes:
   {# this is exciting #}
   {% for i in primes %}
      {{ i }}
   {% endfor %}

Erlang code:

 erlydtl:compile("/path/to/welcome.html", welcome_template),
   welcome_template:render([
       {name, "Johnny"},
       {friends, [<<"Frankie Lee">>, <<"Judas Priest">>]},
       {primes, [1, 2, "3", <<"5">>]}
   ]).

Result:

 Welcome back, Johnny!

   You have 2 friends: Frankie Lee, Judas Priest

   Have some primes:
   1
   2
   3
   5

  項目地址:http://github.com/evanmiller/erlydtl 這個項目給我的啟示很多,其實有更多可能性,不僅僅是用在Web項目里面,后面有時間專門說它.

 

 各位,晚安! 送上小圖一張,周末愉快!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM