Getting Started With BeepBeep
erlang(20), beepbeep (6)一位同事(需要弄清楚他的隐藏博客,所以我可以把它链接回他...... :)从erlang邮件列表中转发了我的方式BeepBeep似乎是一个相当令人兴奋的项目,因为它结合了我正在工作的东西,但需要更多的练习 - erlang - 并用熟悉的香料味道:django。
This tutorial will walk through installation, creation and (kind-of, sort-of, but not really) deployment of an extremely simple BeepBeep application. I can't promise you'll be immediately overwhelmed with excitement, but this将要pave the way for more exciting tutorials down the line.
Please note that this tutorial owes a heavy debt to theBeepBeep documentation, and wouldn't have been possible without it.
在本教程中,我们将构建一个简单的Web应用程序,该应用程序将显示随机素数(目前我的WebApps的标准似乎已经下降了一点),同时显示Beepbeep应用程序的基本组件。没有进一步的ado,让我们开始。
Installing BeepBeep
First, install Erlang. For OS X withPort这可能是
sudo port install erlang
, for Debian systems it could beapt-get安装erlang
.接下来,从GIT存储库下载源代码。
cd ~/git/git clone git://github.com/dmitriid/beepbeep.git
(At the time of this tutorial's writing, the latest commit was
96FB57A58322AA9ABD0340755F0C94D6501D223D
从2009年7月2日起。如果您真的希望本教程在没有修改的情况下工作,我建议使用该版本。)Third, we need to build the BeepBeep source code. (The Git repository is kind enough to come with its dependencies--Mochiweb.和ErlyDTL- 包括。)
CD beepap制作
Somewhat unusually, building the code worked on the first try, and didn't involve chasing down any hidden dependencies.
Now you've completed the rather painless BeepBeep installation.
Creating a BeepBeep Project
现在我们已经安装了框架,我们的下一步是为我们的新项目创建基础。
首先,创建项目脚手架
BB_Primes
, the exceedingly unclever name for the project we're building../script/new_beep.erl Primes ../BB_Primes
现在启动新项目的WebServer。
cd ../BB_Primes/primes/制作./stat_server.sh
If you forget to
制作
the code you'll get a fairly generic crash file, so double-check there first if something goes wrong.After successfully running the server, go tohttp:// localhost:8000to view the default BeepBeep application page.
现在让我们花几分钟时间来看看由此创建的各种文件和文件夹
new_beep.erl
script.deps
包含BeepBeep,MochiWeb和和erlydtl来源,这意味着您不需要确保这些都在您的Erlang路径中。在这里没有太多,虽然您可能想要添加其他依赖关系您的项目使用。ebin
stores the compiled files generated by theMakefile
. Not much to do here either.support
containsinclude.mk
,辅助文件制作
.万维网
contains static files to be served by MochiWeb. (I must admit, I assumed a production environment using BeepBeep would involve hiding it behind Nginx/Lighttpd and only using it to serve dynamic content, but it seems thatMochiWeb也是一个合理的静态文件服务器。)views
包含用于呈现给您的应用程序页面的模板。这些模板是用Django模板语言编写的,编译使用erlydlt.SRC.
contains all the code for the application, including several pieces that will be unfamiliar to those without previous Erlang experiences.home_controller.erl.
is where your controller logic is defined.对于简单的应用程序,这是唯一的文件SRC.你需要编辑。primes.app.
定义OTP应用程序。primes.erl
定义启动和停止功能primes.app.
.primes_deps.erl
defines and locates dependencies (I haven't actually seen aX_deps.erl
之前的文件,是重用应用程序的一个相当不错的想法。)primes_app.erl.
starts and stops theprimes_sup.erl
主管申请。primes_sup.erl
defines the supervisor forprimes.app.
.primes_web.erl.
defines the MochiWeb server for the app.
Customizing a BeepBeep Project
有三个主要位置,您将为您的BeepBeep项目进行更改。
SRC./home_controller.erl
is where URL patterns and behavior are defined. In Django terms, it serves as both theurls.py
和views.py
files for your project.views/
包含所有模板,该模板由控制器使用以向用户显示内容。在Django,这将是你的模板/
folder.万维网/
contains all static media, akin to the媒体/
folder for most Django projects.
In general, I would recommend starting with a simple controller, writing a simple view to match, and then iterating between the two until reaching your desired ending point. Finally add static files at the very end.
Following my own advice, first let's open upSRC./home_controller.erl
. Find the index handle which looks like this:
handle_request(“指数”,[])- >{使成为,"home/index.html",[{数据,"Hello There From BeepBeep!"}]};
和修改它的样子:
random_prime.(N)- >% skipping the logic for finding% nth prime to keep things concise ;(101..handle_request(“指数”,[])- >N=random:制服(100.),SomePrime=random_prime.(N),{使成为,"home/primes.html",[{prime,SomePrime}]};
Next it is time to work on our views. First open up视图/ base.html.
.
“http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitionAl.dtd”>XMLNS ="http://www.w3.org/1999/xhtml"XML:Lang =“en”lang=“en”>HTTP-EQUIV =“内容类型”content="text/html;charset=UTF-8"/> Welcome to Primes
<链接href ="/stylesheets/style.css"rel=“样式表”类型="text/css"/>{%堵塞content%}{%终止%}
Thebase.html.
模板用作其他模板的脚手架,并且是一种简单的方式来支持,不要从Django重复自己的哲学。然后我们将把它延伸到home / primes.html.
template.
{%extends“../base.html”%}{%堵塞content%}Prime is :{ {prime}}{%终止%}
Now finally制作
申请再次。
Will-Larsons-MacBook:Primes将$制作(CD SRC;制造)erlc -w -i ../include -i ../deps/beepbeep-src/include -pa ../deps/beepbeep-src/ebin -pa ../deps/erlydtl-src/ebin -pa ../deps / mochiweb-src / ebin + debug_info -o ../ebin主页_controller.erl/ home_controller.erl:8: Warning: variable 'N' is unused
Woops,在那里有一个警告,因为我回到实施了主要代码,但没有什么太重要了。
Now start the server again:
./start-server.sh
If you had (hypothetically, of course) typoedhome/prime.html
instead ofhome / primes.html.
在您的控制器文件中,那么您会看到这样的错误:
It isn't quite as pretty or as informative as the Django error pages, but it's servicable nonetheless. A successful run looks like this:
现在它是使用默认的beepbeep样式表www / styleshs / style.css
, but let's change things up ever so slightly.
emacs.万维网/样式表/primes.css
并添加以下样式:
身体{填充:25px.;背景:#333;color:#bbf.;}
Then edit视图/ base.html.
to replace
<关联HREF.="/stylesheets/style.css"rel=“样式表”类型="text/css"/>
with
<关联HREF.=“/stylesheets/primes.css”rel=“样式表”类型="text/css"/>
Now restart the server,
./start-server.sh
reload the page, and bask in the glory of the very simple application you have created.
Deployment
当前的启动脚本在当前shell会话中运行,这对于开发非常重要,但对长时间运行的流程不太方便。您可以在改进它方面走得太远(在备用节点中启动它,运行它心, etc), but keeping with the simple tone of this tutorial it is easy to simply run it in detached mode which allows you to retain your shell sessions.
cp start-server.sh start-detached-server.shEmacs start-detached-server.sh.
Then modifystart-detached-server.sh.
to look like this:
#/ / bin / shCD`舞蹈0 $ 0`EXEC ERL -PA $ PWD / EBIN $ PWD / DEPS / * / EBIN -BOOT Start_sasl span>-s重新加载器-s primes -detached
如果您希望在注销后持续过程,则可以使用sudo -u
将进程作为持久用户运行(运行为root
有一些如此轻微的安全后果,但有人可能会使用万维网-data
或自定义用户运行BeepBeep应用程序)。
所以你有它,一个非常简单的Beepap应用程序。本教程不可否认给你Blog in 5 Minutesfeel that you've probably come to expect from frameworks, but hang around and I promise the next BeepBeep tutorial will be much more exciting.
(本教程中的代码可用为BB_Primes
在里面BeepBeep Examples git repository.
As always, I am grateful for any feedback or corrections!