实战MochiWeb

把手头的东西整理的过程中,有些内容始终不想轻易的关掉,就让它这么流逝在我的时间线里。比如说有关ErLang的几个内容。

虽然ErLang离我的实际应用较远,但是它的几个特性还是深深吸引了我。
erlang当初是为电信系统设计的,电信系统的要求,按照Dacker给出的说法是10个(见参考2):
* 系统必须处理极大数目的并发活动;
* 动作必须及时在某一个点得到处理,或在一定的时间内得到处理;
* 系统可能分布在数台计算机上
* 系统用于控制硬件
* 软件系统非常庞大
* 系统具备复杂的功能
* 系统应该在很多年间持续运行
* 在不停止系统的前提下进行软件维护(例如重新配置)
* 严格的质量和可靠性需求
* 必须同时对硬件失败和软件错误容错

根据实战MochiWeb这篇文章,在10.04的虚拟机上做了测试,感觉还是很不错的。这結果很好,算是做为暂时告别对Erlang向往的一个小结吧。它够好、够巧、够实用、够强大。我不大习惯做我不视焉,故不在焉的评价。

1. 安装erlang
sudo apt-get install erlang

2. check erlang mochiweb
svn checkout http://mochiweb.googlecode.com/svn/trunk/ mochiweb-read-only
好象已经迁移到这里了。git最近比较火。
http://github.com/mochi/mochiweb
[code language=”bash”]
lanhui@lanhui-desktop:~/mochiweb-read-only$ ls
deps ebin include Makefile README src
doc examples LICENSE priv scripts support
[/code]

3. make
[code language=”bash”]
lanhui@lanhui-desktop:~/mochiweb-read-only$ make
(cd src;make all)
make[1]: Entering directory `/home/lanhui/mochiweb-read-only/src’
erlc -W -I ../include +debug_info -o ../ebin mochifmt.erl
erlc -W -I ../include +debug_info -o ../ebin mochifmt_records.erl
erlc -W -I ../include +debug_info -o ../ebin mochifmt_std.erl
erlc -W -I ../include +debug_info -o ../ebin mochiglobal.erl
erlc -W -I ../include +debug_info -o ../ebin mochihex.erl
erlc -W -I ../include +debug_info -o ../ebin mochijson2.erl
erlc -W -I ../include +debug_info -o ../ebin mochijson.erl
erlc -W -I ../include +debug_info -o ../ebin mochilists.erl
erlc -W -I ../include +debug_info -o ../ebin mochilogfile2.erl
erlc -W -I ../include +debug_info -o ../ebin mochinum.erl
erlc -W -I ../include +debug_info -o ../ebin mochitemp.erl
erlc -W -I ../include +debug_info -o ../ebin mochiutf8.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_acceptor.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_app.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_charref.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_cookies.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_cover.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_echo.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_headers.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_html.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_http.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_io.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_mime.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_multipart.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_request.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_response.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_skel.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_socket.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_socket_server.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_sup.erl
erlc -W -I ../include +debug_info -o ../ebin mochiweb_util.erl
erlc -W -I ../include +debug_info -o ../ebin reloader.erl
../support/make_app.escript mochiweb.app.src ../ebin/mochiweb.app “” “mochifmt mochifmt_records mochifmt_std mochiglobal mochihex mochijson2 mochijson mochilists mochilogfile2 mochinum mochitemp mochiutf8 mochiweb_acceptor mochiweb_app mochiweb_charref mochiweb_cookies mochiweb_cover mochiweb_echo mochiweb mochiweb_headers mochiweb_html mochiweb_http mochiweb_io mochiweb_mime mochiweb_multipart mochiweb_request mochiweb_response mochiweb_skel mochiweb_socket mochiweb_socket_server mochiweb_sup mochiweb_util reloader”
make[1]: Leaving directory `/home/lanhui/mochiweb-read-only/src’
[/code]

4. 生成骨架
[code language=”bash”]
lanhui@lanhui-desktop:~/mochiweb-read-only$ escript scripts/new_mochiweb.erl test
./test/
./test/src/
skel.app
Makefile
skel_app.erl
skel_sup.erl
skel.hrl
skel_deps.erl
skel_web.erl
skel.erl
./test/include/
./test/support/
include.mk
run_tests.escript
Makefile
./test/priv/
./test/priv/www/
index.html
./test/doc/
start-dev.sh
start.sh
./test/deps/
[/code]
看看生成的内容吧:
[code language=”bash”]
lanhui@lanhui-desktop:~/mochiweb-read-only$ cd test/
lanhui@lanhui-desktop:~/mochiweb-read-only/test$ ls
deps doc ebin include Makefile priv src start-dev.sh start.sh support
[/code]

这个还是要make一下:
[code language=”bash”]
lanhui@lanhui-desktop:~/mochiweb-read-only/test$ make
(cd src;make all)
make[1]: Entering directory `/home/lanhui/mochiweb-read-only/test/src’
erlc -W -I ../include -I ../deps/mochiweb-src/include -pa ../deps/mochiweb-src/ebin +debug_info -o ../ebin test_app.erl
erlc -W -I ../include -I ../deps/mochiweb-src/include -pa ../deps/mochiweb-src/ebin +debug_info -o ../ebin test_deps.erl
erlc -W -I ../include -I ../deps/mochiweb-src/include -pa ../deps/mochiweb-src/ebin +debug_info -o ../ebin test.erl
erlc -W -I ../include -I ../deps/mochiweb-src/include -pa ../deps/mochiweb-src/ebin +debug_info -o ../ebin test_sup.erl
erlc -W -I ../include -I ../deps/mochiweb-src/include -pa ../deps/mochiweb-src/ebin +debug_info -o ../ebin test_web.erl
cp test.app ../ebin/test.app
make[1]: Leaving directory `/home/lanhui/mochiweb-read-only/test/src’
[/code]

5. 测试一下:
[code language=”bash”]
./start-dev.sh
(cd src;make all)
make[1]: Entering directory `/home/lanhui/mochiweb-read-only/test/src’
make[1]: Nothing to be done for `all’.
make[1]: Leaving directory `/home/lanhui/mochiweb-read-only/test/src’
Erlang R13B03 (erts-5.7.4) [source] [64-bit] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]

=PROGRESS REPORT==== 23-Aug-2010::11:57:10 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.36.0>},
{name,alarm_handler},
{mfa,{alarm_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]

=PROGRESS REPORT==== 23-Aug-2010::11:57:10 ===
supervisor: {local,sasl_safe_sup}
started: [{pid,<0.37.0>},
{name,overload},
{mfa,{overload,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]

=PROGRESS REPORT==== 23-Aug-2010::11:57:10 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.35.0>},
{name,sasl_safe_sup},
{mfa,
{supervisor,start_link,
[{local,sasl_safe_sup},sasl,safe]}},
{restart_type,permanent},
{shutdown,infinity},
{child_type,supervisor}]

=PROGRESS REPORT==== 23-Aug-2010::11:57:10 ===
supervisor: {local,sasl_sup}
started: [{pid,<0.38.0>},
{name,release_handler},
{mfa,{release_handler,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]

=PROGRESS REPORT==== 23-Aug-2010::11:57:10 ===
application: sasl
started_at: nonode@nohost

=PROGRESS REPORT==== 23-Aug-2010::11:57:10 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.43.0>},
{name,timer_server},
{mfa,{timer,start_link,[]}},
{restart_type,permanent},
{shutdown,1000},
{child_type,worker}]
** Found 0 name clashes in code paths
Eshell V5.7.4 (abort with ^G)
1>
=PROGRESS REPORT==== 23-Aug-2010::11:57:10 ===
supervisor: {local,crypto_sup}
started: [{pid,<0.52.0>},
{name,crypto_server},
{mfa,{crypto_server,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]

=PROGRESS REPORT==== 23-Aug-2010::11:57:10 ===
application: crypto
started_at: nonode@nohost
** Found 0 name clashes in code paths

=PROGRESS REPORT==== 23-Aug-2010::11:57:10 ===
supervisor: {local,test_sup}
started: [{pid,<0.57.0>},
{name,test_web},
{mfa,
{test_web,start,
[[{ip,”0.0.0.0″},
{port,8000},
{docroot,
“/home/lanhui/mochiweb-read-only/test/priv/www”}]]}},
{restart_type,permanent},
{shutdown,5000},
{child_type,worker}]

=PROGRESS REPORT==== 23-Aug-2010::11:57:10 ===
application: test
started_at: nonode@nohost
[/code]
結果如图:

6. 小定制:
[code language=”erlang”]
case Path of
_ ->
[/code]
代码中加入timer分支:
[code language=”erlang”]
case Path of
“timer” ->
Response = Req:ok({“text/plain”, chunked}), timer(Response);
_ ->
Req:serve_file(Path, DocRoot)
[/code]

get_option函数后增加
[code language=”erlang”]
timer(Response) ->
Response:write_chunk(io_lib:format(“This time is: ~p~n”,
[calendar:local_time()])),
timer:sleep(1000),
timer(Response).
[/code]
結果:

参考:
1. 实战MochiWeb
2. Erlang能否震撼Ruby界
3. 主题:用Mochiweb打造百万级Comet应用,第一部分

打赏

发表评论

电子邮件地址不会被公开。 必填项已用*标注