{"id":173595,"date":"2013-03-20T21:38:03","date_gmt":"2013-03-20T17:38:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=173595"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=173595","title":{"rendered":"<span class=\"post_title\">Web-\u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 \u0431\u0430\u0437\u0435 Cowboy<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u041f\u0440\u0438\u0432\u0435\u0442!<br \/>  \u0412 \u044d\u0442\u043e\u043c \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0435 \u044f \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u044e \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0442\u0435\u043c, \u043a\u0442\u043e \u0435\u0449\u0435 \u043d\u0435 \u0437\u043d\u0430\u043a\u043e\u043c \u0441 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c Cowboy, \u043a\u0430\u043a \u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f. \u0414\u043b\u044f \u043b\u044e\u0434\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u043c\u0435\u044e\u0442 \u043e\u043f\u044b\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c, \u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b \u0432\u0440\u044f\u0434\u043b\u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d, \u0430 \u0432\u043e\u0442 \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u0437\u043d\u0430\u0435\u0442 \u043e \u041a\u043e\u0432\u0431\u043e\u0435 \u043b\u0438\u0448\u044c \u043f\u043e \u043d\u0430\u0441\u043b\u044b\u0448\u043a\u0435 \u2014 welcome!<\/p>\n<p>  \u0427\u0442\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c:  <\/p>\n<ol>\n<li>\u041f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0430\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/li>\n<li>\u041a\u0440\u0430\u0442\u043a\u0438\u0439 \u043e\u0431\u0437\u043e\u0440 \u0440\u043e\u0443\u0442\u0438\u043d\u0433\u0430, \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u0438\u043a\u0438<\/li>\n<li>\u0428\u0430\u0431\u043b\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e ErlyDTL (Django Template Language \u0434\u043b\u044f Erlang)<\/li>\n<\/ol>\n<p>  <a name=\"habracut\"><\/a><br \/>  \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f rebar, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043d\u0435\u0445\u0438\u0442\u0440\u0430\u044f:  <\/p>\n<pre><code class=\"bash\">&gt; git clone git:\/\/github.com\/basho\/rebar.git && cd rebar && .\/bootstrap <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u0444\u0430\u0439\u043b rebar \u2014 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c (\u0430 \u043b\u0443\u0447\u0448\u0435 \u043b\u0438\u043d\u043a\u0443\u0435\u043c) \u0435\u0433\u043e \u043a\u0443\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u0432 $PATH. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:  <\/p>\n<pre><code class=\"bash\">&gt; sudo ln -s `pwd`\/rebar \/usr\/bin\/rebar <\/code><\/pre>\n<p>  And here we go!<\/p>\n<h4>\u041f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0430\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/h4>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e \u0438 \u0441\u043a\u0435\u043b\u0435\u0442 \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u0443\u0434\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0432 \u044d\u0442\u043e\u043c \u043d\u0430\u043c \u043f\u043e\u043c\u043e\u0436\u0435\u0442 rebar. \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a\u0443\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443:  <\/p>\n<pre><code class=\"bash\">&gt; mkdir webserver && cd webserver && rebar create-app appid=webserver <\/code><\/pre>\n<p>  \u041a\u043e\u043c\u0430\u043d\u0434\u0430 <code>rebar create-app appid=webserver<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u043a\u0435\u043b\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e Erlang-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0448\u0430 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044f webserver \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/c76\/cb0\/845\/c76cb0845dbe0ec0cbf9d422fc5a5738.png\"\/><br \/>  \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435, \u0447\u0442\u043e \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u2014 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0442 Cowboy, Sync, Mimetypes \u0438 Erlydtl. Cowboy \u2014 \u043d\u0430\u0448 web-\u0441\u0435\u0440\u0432\u0435\u0440, Sync \u2014 \u0443\u0442\u0438\u043b\u0438\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043d\u0430\u043c \u043d\u0435 \u043f\u0435\u0440\u0435\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0438 \u0431\u0443\u0434\u0435\u0442 \u0441\u0430\u043c\u0430 \u043f\u0435\u0440\u0435\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438 \u043f\u0440\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0438, Mimetypes \u2014 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0441 mimetype (\u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c\u0441\u044f \u043e\u0442\u0434\u0430\u0447\u0435\u0439 \u0441\u0442\u0430\u0442\u0438\u043a\u0438), \u0430 Erlydtl \u2014 \u0448\u0430\u0431\u043b\u043e\u043d\u0438\u0437\u0430\u0442\u043e\u0440. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u0434\u043b\u044f rebar \u043f\u043e\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c rebar.config:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 rebar.config<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">{deps, [ \t{cowboy, &quot;.*&quot;, {git, &quot;https:\/\/github.com\/extend\/cowboy.git&quot;, {branch, &quot;master&quot;}}}, \t{sync, &quot;.*&quot;, {git, &quot;git:\/\/github.com\/rustyio\/sync.git&quot;, {branch, &quot;master&quot;}}}, \t{mimetypes, &quot;.*&quot;, {git, &quot;git:\/\/github.com\/spawngrid\/mimetypes.git&quot;, {branch, &quot;master&quot;}}}, \t{erlydtl, &quot;.*&quot;, {git, &quot;git:\/\/github.com\/evanmiller\/erlydtl.git&quot;, {branch, &quot;master&quot;}}} ]}. <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b src\/webserver.erl, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043f\u043e\u043a\u0430 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0438 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0435\u0440:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 src\/webserver.erl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">-module(webserver).  %% API -export([ \tstart\/0, \tstop\/0 ]).  -define(APPS, [crypto, ranch, cowboy, webserver]).  %% =================================================================== %% API functions %% ===================================================================  start() -&gt; \tok = ensure_started(?APPS), \tok = sync:go().  stop() -&gt; \tsync:stop(), \tok = stop_apps(lists:reverse(?APPS)).  %% =================================================================== %% Internal functions %% ===================================================================  ensure_started([]) -&gt; ok; ensure_started([App | Apps]) -&gt; \tcase application:start(App) of \t\tok -&gt; ensure_started(Apps); \t\t{error, {already_started, App}} -&gt; ensure_started(Apps) \tend.  stop_apps([]) -&gt; ok; stop_apps([App | Apps]) -&gt; \tapplication:stop(App), \tstop_apps(Apps). <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b\u0437\u043e\u0432 webserver:start() \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u043f\u043e-\u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f crypto, ranch, cowboy, webserver \u0438 \u0430\u0432\u0442\u043e\u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Sync, \u0430 webserver:stop \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442 \u0432\u0441\u0435 \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u043e\u0435 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.<br \/>  \u041a\u0430\u0441\u043a\u0430\u0434 \u0433\u043e\u0442\u043e\u0432, \u043f\u043e\u0440\u0430 \u0443\u0436\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043a \u041a\u043e\u0432\u0431\u043e\u044e. \u041e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c webserver_app.erl \u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e start\/2:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0424\u0443\u043d\u043a\u0446\u0438\u044f webserver_app:start\/2<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">start(_StartType, _StartArgs) -&gt; \tDispatch = cowboy_router:compile([ \t\t{'_', [ \t\t\t{&quot;\/&quot;, index_handler, []}, \t\t\t{'_', notfound_handler, []} \t\t]} \t]), \tPort = 8008, \t{ok, _} = cowboy:start_http(http_listener, 100, \t\t[{port, Port}], \t\t[{env, [{dispatch, Dispatch}]}] \t), \twebserver_sup:start_link(). <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412 \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u0445 \u0434\u0438\u0441\u043f\u0430\u0442\u0447\u0438\u043d\u0433\u0430 \u043c\u044b \u0443\u043a\u0430\u0437\u0430\u043b\u0438, \u0447\u0442\u043e \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a\u0440\u043e\u043c\u0435 &quot;\/&quot;, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440, \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e notfound_handler (\u0431\u0443\u0434\u0435\u043c \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c 404 \u043e\u0448\u0438\u0431\u043a\u0443), \u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a &quot;\/&quot; \u0431\u0443\u0434\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e index_handler. \u0417\u043d\u0430\u0447\u0438\u0442, \u0441\u0442\u043e\u0438\u0442 \u0438\u0445 \u0441\u043e\u0437\u0434\u0430\u0442\u044c:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 src\/index_handler.erl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">-module(index_handler). -behaviour(cowboy_http_handler). %% Cowboy_http_handler callbacks -export([ \tinit\/3, \thandle\/2, \tterminate\/3 ]).  init({tcp, http}, Req, _Opts) -&gt; \t{ok, Req, undefined_state}.  handle(Req, State) -&gt; \tBody = &lt;&lt;&quot;&lt;h1&gt;It works!&lt;\/h1&gt;&quot;&gt;&gt;, \t{ok, Req2} = cowboy_req:reply(200, [], Body, Req), \t{ok, Req2, State}.  terminate(_Reason, _Req, _State) -&gt; \tok. <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 src\/notfound_handler.erl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">-module(notfound_handler). -behaviour(cowboy_http_handler). %% Cowboy_http_handler callbacks -export([ \tinit\/3, \thandle\/2, \tterminate\/3 ]).  init({tcp, http}, Req, _Opts) -&gt; \t{ok, Req, undefined_state}.  handle(Req, State) -&gt; \tBody = &lt;&lt;&quot;&lt;h1&gt;404 Page Not Found&lt;\/h1&gt;&quot;&gt;&gt;, \t{ok, Req2} = cowboy_req:reply(404, [], Body, Req), \t{ok, Req2, State}.  terminate(_Reason, _Req, _State) -&gt; \tok. <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0441\u0435 \u2014 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u043c\u0435\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430 <a href=\"http:\/\/localhost\">localhost<\/a>:8008 \u0438 <a href=\"http:\/\/localhost\">localhost<\/a>:8008\/WHATEVER. \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440:  <\/p>\n<pre><code class=\"bash\">&gt; rebar get-deps &gt; rebar compile &gt; erl -pa ebin deps\/*\/ebin -s webserver <\/code><\/pre>\n<p>  <code>rebar get-deps<\/code> \u043f\u043e\u0434\u0442\u044f\u043d\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u0437 \u043a\u043e\u043d\u0444\u0438\u0433\u0430, <code>rebar compile<\/code> \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u0434, \u0430 <code>erl -pa ebin deps\/*\/ebin -s webserver<\/code> \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u0441\u0430\u043c \u0441\u0435\u0440\u0432\u0435\u0440. \u041a\u0441\u0442\u0430\u0442\u0438, \u0441\u0430\u043c\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 Makefile \u0434\u043b\u044f \u043e\u0431\u043b\u0435\u0433\u0447\u0435\u043d\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u044b\u0448\u0435\u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 Makefile<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre> REBAR = `which rebar`  all: deps compile  deps: \t@( $(REBAR) get-deps )  compile: clean \t@( $(REBAR) compile )  clean: \t@( $(REBAR) clean )  run: \t@( erl -pa ebin deps\/*\/ebin -s webserver )  .PHONY: all deps compile clean run <\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u043e\u043c <code>make<\/code>, \u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u043e\u043c <code>make run<\/code><br \/>  \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0441\u0435\u0440\u0432\u0435\u0440 \u0431\u044b\u043b \u0437\u0430\u043f\u0443\u0449\u0435\u043d, \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430 <a href=\"http:\/\/localhost\">localhost<\/a>:8008, \u0430 \u0437\u0430\u0442\u0435\u043c \u043d\u0430 <a href=\"http:\/\/localhost\">localhost<\/a>:8008\/whatever \u0438 \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0441\u0435\u0440\u0432\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e, \u043e\u0442\u0434\u0430\u0432\u0430\u044f \u00abIt works\u00bb \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u00ab404 Page Not Found\u00bb \u043d\u0430 \u0432\u0442\u043e\u0440\u043e\u0439<\/p>\n<h4>\u041a\u0440\u0430\u0442\u043a\u0438\u0439 \u043e\u0431\u0437\u043e\u0440 \u0440\u043e\u0443\u0442\u0438\u043d\u0433\u0430, \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u0438\u043a\u0438<\/h4>\n<p>  \u0420\u043e\u0443\u0442\u0438\u043d\u0433 \u0432 \u041a\u043e\u0432\u0431\u043e\u0435 \u043d\u0435 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u0443\u0434\u043e\u0431\u043d\u044b\u0439, \u043e\u0434\u043d\u0430\u043a\u043e \u0432\u043f\u043e\u043b\u043d\u0435 \u0441\u043d\u043e\u0441\u043d\u044b\u0439 \u2014 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u0438\u0448\u043a\u0438 \u0432\u0440\u043e\u0434\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 URL \u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u044d\u0442\u0438\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b. \u041f\u043e\u043a\u0430 \u0443 \u043d\u0430\u0441 \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u0445 \u0434\u0438\u0441\u043f\u0430\u0442\u0447\u0438\u043d\u0433\u0430 \u0435\u0441\u0442\u044c \u043b\u0438\u0448\u044c \u0434\u0432\u0430 \u0440\u043e\u0443\u0442\u0430:  <\/p>\n<pre><code class=\"erlang\">{&quot;\/&quot;, index_handler, []}, {'_', notfound_handler, []} <\/code><\/pre>\n<p>  \u041a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0434\u0440\u0443\u0433\u043e\u0433\u043e, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0435\u0433\u043e, \u0434\u043b\u044f \u043a\u0430\u043a\u043e\u0433\u043e \u0445\u043e\u0441\u0442\u0430 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c \u0438 \u043e \u0440\u043e\u0443\u0442\u0438\u043d\u0433\u0435 \u0432 \u0446\u0435\u043b\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0437\u0434\u0435\u0441\u044c: <a href=\"https:\/\/github.com\/extend\/cowboy\/blob\/master\/guide\/routing.md\">github.com\/extend\/cowboy\/blob\/master\/guide\/routing.md<\/a> \u0430 \u0437\u0434\u0435\u0441\u044c \u044f \u0443\u0442\u043e\u0447\u043d\u044e \u043b\u0438\u0448\u044c \u0447\u0442\u043e \u0430\u0442\u043e\u043c &#8216;_&#8217; \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0440\u043e\u0443\u0442 \u0431\u0443\u0434\u0435\u0442 \u043c\u0430\u0442\u0447\u0438\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0432\u0441\u0435\u043c \u0430\u0434\u0440\u0435\u0441\u0430\u043c, notfound_handler \u2014 \u0438\u043c\u044f \u043c\u043e\u0434\u0443\u043b\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u043c\u0430\u0442\u0447\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u0430 [] \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043e\u043f. \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u044e<br \/>  \u0425\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u043a\u0443 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0432 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 priv \u0432 \u043f\u043e\u0434\u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044f\u0445 priv\/css priv\/js, priv\/img \u0438 \u043c\u0430\u0442\u0447\u0438\u0442\u044c \u0435\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c:  <\/p>\n<pre><code class=\"bash\">\/css\/WHATEVER -&gt; \/priv\/css\/WHATEVER \/js\/WHATEVER  -&gt; \/priv\/js\/WHATEVER \/img\/WHATEVER -&gt; priv\/img\/WHATEVER <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c 3 \u0440\u043e\u0443\u0442\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e:  <\/p>\n<pre><code class=\"erlang\">Dispatch = cowboy_router:compile([ \t{'_', [ \t\t{&quot;\/css\/[...]&quot;, cowboy_static, [ \t\t\t{directory, {priv_dir, webserver, [&lt;&lt;&quot;css&quot;&gt;&gt;]}}, \t\t\t{mimetypes, {fun mimetypes:path_to_mimes\/2, default}} \t\t]}, \t\t{&quot;\/js\/[...]&quot;, cowboy_static, [ \t\t\t{directory, {priv_dir, webserver, [&lt;&lt;&quot;js&quot;&gt;&gt;]}}, \t\t\t{mimetypes, {fun mimetypes:path_to_mimes\/2, default}} \t\t]}, \t\t{&quot;\/img\/[...]&quot;, cowboy_static, [ \t\t\t{directory, {priv_dir, webserver, [&lt;&lt;&quot;img&quot;&gt;&gt;]}}, \t\t\t{mimetypes, {fun mimetypes:path_to_mimes\/2, default}} \t\t]}, \t\t{&quot;\/&quot;, index_handler, []}, \t\t{'_', notfound_handler, []} \t]} ]). <\/code><\/pre>\n<p>  \u0444\u0443\u043d\u043a\u0446\u0438\u044f mimetypes:path_to_mimes\/2 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u043e\u0442\u0434\u0430\u0447\u0443 \u0432\u0435\u0440\u043d\u043e\u0433\u043e mimetype \u043f\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044e \u0444\u0430\u0439\u043b\u0430.<br \/>  \u041b\u0435\u0433\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 3 \u0440\u043e\u0443\u0442\u0430 \u043f\u043e\u0447\u0442\u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043a\u043e\u043f\u0438\u0440\u0443\u044e\u0442 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430 \u0437\u0430 \u043c\u0435\u043b\u043a\u0438\u043c\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f\u043c\u0438, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u044b\u043d\u0435\u0441\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u0440\u043e\u0443\u0442\u0430 \u0434\u043b\u044f \u0441\u0442\u0430\u0442\u0438\u043a\u0438 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u0435\u0439 \u0440\u043e\u0443\u0442\u044b:  <\/p>\n<pre><code class=\"erlang\">Static = fun(Filetype) -&gt; \t{lists:append([&quot;\/&quot;, Filetype, &quot;\/[...]&quot;]), cowboy_static, [ \t\t{directory, {priv_dir, webserver, [list_to_binary(Filetype)]}}, \t\t{mimetypes, {fun mimetypes:path_to_mimes\/2, default}} \t]} end, Dispatch = cowboy_router:compile([ \t{'_', [ \t\tStatic(&quot;css&quot;), \t\tStatic(&quot;js&quot;), \t\tStatic(&quot;img&quot;), \t\t{&quot;\/&quot;, index_handler, []}, \t\t{'_', notfound_handler, []} \t]} ]). <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u0447\u0442\u043e\u0431\u044b \u043d\u043e\u0432\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0434\u0438\u0441\u043f\u0430\u0442\u0447\u0438\u043d\u0433\u0430 \u0432\u0441\u0442\u0443\u043f\u0438\u043b\u0438 \u0432 \u0441\u0438\u043b\u0443, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0431\u043e \u043f\u0435\u0440\u0435\u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440, \u043b\u0438\u0431\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 cowboy:set_env\/3<br \/>  \u041f\u0435\u0440\u0432\u043e\u0435 \u2014 \u043d\u0435\u0441\u043f\u043e\u0440\u0442\u0438\u0432\u043d\u043e, \u0434\u0430 \u0438 \u043f\u0435\u0440\u0435\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0447\u0438\u0445 \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u0445 \u0440\u043e\u0443\u0442\u0438\u043d\u0433\u0430 \u0437\u0430\u043c\u0443\u0447\u0430\u0435\u0448\u044c\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u043e\u0443\u0442\u0438\u043d\u0433\u0430 \u0432 \u043d\u0430\u0448\u0435\u043c \u0444\u0430\u0439\u043b\u0435 webserver, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u0442\u044c webserver:update_routing(). \u0418, \u0447\u0442\u043e\u0431\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u044f webserver:update_routing\/0 \u0437\u043d\u0430\u043b\u0430 \u043e \u043d\u043e\u0432\u044b\u0445 \u0440\u043e\u0443\u0442\u0430\u0445 \u2014 \u0432\u044b\u043d\u0435\u0441\u0435\u043c \u0438\u0445 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e. \u0412 \u0438\u0442\u043e\u0433\u0435 \u0444\u0430\u0439\u043b webserver_app.erl \u043f\u0440\u0438\u043c\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0438\u0434:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 src\/webserver_app.erl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">-module(webserver_app). -behaviour(application).  %% Application callbacks -export([ \tstart\/2, \tstop\/1 ]).  %% API -export([dispatch_rules\/0]).  %% =================================================================== %% API functions %% ===================================================================  dispatch_rules() -&gt; \tStatic = fun(Filetype) -&gt; \t\t{lists:append(&quot;\/&quot;, Filetype, &quot;\/[...]&quot;), cowboy_static, [ \t\t\t{directory, {priv_dir, webserver, [list_to_binary(Filetype)]}}, \t\t\t{mimetypes, {fun mimetypes:path_to_mimes\/2, default}} \t\t]} \tend, \tcowboy_router:compile([ \t\t{'_', [ \t\t\tStatic(&quot;css&quot;), \t\t\tStatic(&quot;js&quot;), \t\t\tStatic(&quot;img&quot;), \t\t\t{&quot;\/&quot;, index_handler, []}, \t\t\t{'_', notfound_handler, []} \t\t]} \t]).  %% =================================================================== %% Application callbacks %% ===================================================================  start(_StartType, _StartArgs) -&gt; \tDispatch = dispatch_rules(), \tPort = 8008, \t{ok, _} = cowboy:start_http(http_listener, 100, \t\t[{port, Port}], \t\t[{env, [{dispatch, Dispatch}]}] \t), \twebserver_sup:start_link().  stop(_State) -&gt; \tok. <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e update_routing \u0432 \u043c\u043e\u0434\u0443\u043b\u044c webserver.erl:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0424\u0443\u043d\u043a\u0446\u0438\u044f webserver:update_routes\/0<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">update_routes() -&gt; \tRoutes = webserver_app:dispatch_rules(), \tcowboy:set_env(http_listener, dispatch, Routes). <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0418 \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432 \u0430\u0442\u0442\u0440\u0438\u0431\u0443\u0442 -export(), \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043e\u043d \u0441\u0442\u0430\u043d\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"erlang\">%% API -export([ \tstart\/0, \tstop\/0, \tupdate_routes\/0 ]). <\/code><\/pre>\n<p>  \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 <code>webserver:update_routes().<\/code>, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u0434\u043b\u044f \u0441\u0442\u0430\u0442\u0438\u043a\u0438  <\/p>\n<pre><code class=\"bash\">&gt; mkdir priv && cd priv && mkdir css js img <\/code><\/pre>\n<p>  \u0438 \u043a\u043b\u0430\u0434\u0435\u043c \u0442\u0443\u0434\u0430 \u043a\u0430\u043a\u0438\u0435-\u043d\u0438\u0431\u0443\u0434\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0444\u0430\u0439\u043b\u044b, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e \u043e\u043d\u0438 \u043e\u0442\u0434\u0430\u044e\u0442\u0441\u044f, \u043a\u0430\u043a \u0438 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u043e\u0441\u044c, \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <a href=\"http:\/\/localhost\">localhost<\/a>:8008\/PATH\/FILE<\/p>\n<h4>\u0428\u0430\u0431\u043b\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e ErlyDTL (Django Template Language \u0434\u043b\u044f Erlang)<\/h4>\n<p>  Evan Miller, \u0430\u0432\u0442\u043e\u0440 \u043d\u0435\u0431\u0435\u0437\u044b\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0433\u043e web-\u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 Chicago Boss \u043f\u043e\u0434 Erlang, \u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043b Django Template Language (https:\/\/docs.djangoproject.com\/en\/dev\/topics\/templates\/) \u043d\u0430 Erlang \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u044d\u0442\u043e, \u043e\u0442\u043a\u0440\u043e\u0432\u0435\u043d\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043a\u0440\u0443\u0442\u043e. \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e\u0442 \u0448\u0430\u0431\u043b\u043e\u043d\u0438\u0437\u0430\u0442\u043e\u0440 \u044f \u0431\u044b \u0438 \u043f\u043e\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u043b \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0432 \u0432\u0430\u0448\u0438\u0445 \u0431\u0443\u0434\u0443\u0449\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u2014 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432 \u043b\u0443\u0447\u0448\u0435 \u044f \u043f\u043e\u043a\u0430 \u043d\u0435 \u0432\u0438\u0434\u0435\u043b.<br \/>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e webserver\/tpl \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0442\u0443\u0434\u0430 \u0442\u0440\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u0430:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 tpl\/layout.dtl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"django\">&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; \t&lt;title&gt;Webserver&lt;\/title&gt; &lt;\/head&gt; &lt;body&gt; {% block content %}{% endblock %} &lt;\/body&gt; &lt;\/html&gt; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 tpl\/index.dtl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"django\">{% extends &quot;layout.dtl&quot; %} {% block content %} &lt;h1&gt;Hello, {{ username | default : &quot;stranger&quot; }}!&lt;\/h1&gt; {% endblock %} <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 tpl\/404.dtl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"django\">{% extends &quot;layout.dtl&quot; %} {% block content %} &lt;h1&gt;URL &lt;span style=&quot;color:red;&quot;&gt;{{ url }}&lt;\/span&gt; does not exists.&lt;\/h1&gt; {% endblock %} <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d\u044b, \u0438\u0445 \u043d\u0443\u0436\u043d\u043e \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0414\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e erlydtl:compile\/3 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:  <\/p>\n<pre><code class=\"erlang\">ok = erlydtl:compile(&quot;tpl\/layout.dtl&quot;, &quot;layout_tpl&quot;, []), ok = erlydtl:compile(&quot;tpl\/index.dtl&quot;, &quot;index_tpl&quot;, []), ok = erlydtl:compile(&quot;tpl\/404.dtl&quot;, &quot;404_tpl&quot;, []). <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u2014 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u043f\u0446\u0438\u0439 \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u0430, \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0434\u0435\u0441\u044c: <a href=\"https:\/\/github.com\/evanmiller\/erlydtl\">github.com\/evanmiller\/erlydtl<\/a><br \/>  \u0427\u0442\u043e\u0431\u044b \u0440\u0443\u043a\u0430\u043c\u0438 \u043d\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 webserver, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c\u0441\u044f \u043f\u0435\u0440\u0435\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0435\u0439:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">c_tpl() -&gt; \tc_tpl([]).  c_tpl(Opts) -&gt; \tc_tpl(filelib:wildcard(&quot;tpl\/*.dtl&quot;), Opts).  c_tpl([], _Opts) -&gt; ok; c_tpl([File | Files], Opts) -&gt; \tok = erlydtl:compile(File, re:replace(filename:basename(File), &quot;.dtl&quot;, &quot;_tpl&quot;, [global, {return, list}]), Opts), \tc_tpl(Files, Opts). <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0438 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0438\u0445:  <\/p>\n<pre><code class=\"erlang\">%% API -export([ \tstart\/0, \tstop\/0, \tupdate_routes\/0, \tc_tpl\/0, c_tpl\/1, c_tpl\/2 ]). <\/code><\/pre>\n<p>  c_tpl\/0 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u044b \u0438\u0437 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 tpl \u0431\u0435\u0437 \u043e\u043f\u0446\u0438\u0439, c_tpl\/1 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043e\u043f\u0446\u0438\u044f\u043c\u0438, \u0430 c_tpl\/2 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043e\u043f\u0446\u0438\u044f\u043c\u0438. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0432 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u042d\u0440\u043b\u0430\u043d\u0433\u0430 <code>webserver:c_tpl().<\/code><br \/>  \u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0438 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u044b, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u043e\u0442\u0434\u0430\u0432\u0430\u043b\u0438 \u043e\u0442\u0432\u0435\u0442\u043e\u043c \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u044b, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u044b \u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 src\/index_handler.erl<\/b><\/p>\n<div class=\"spoiler_text\">index_handler.erl  <\/p>\n<pre><code class=\"erlang\">-module(index_handler). -behaviour(cowboy_http_handler). %% Cowboy_http_handler callbacks -export([ \tinit\/3, \thandle\/2, \tterminate\/3 ]).  init({tcp, http}, Req, _Opts) -&gt; \t{ok, Req, undefined_state}.  handle(Req, State) -&gt; \t{Username, Req2} = cowboy_req:qs_val(&lt;&lt;&quot;username&quot;&gt;&gt;, Req, &quot;stranger&quot;), \t{ok, HTML} = index_tpl:render([{username, Username}]), \t{ok, Req3} = cowboy_req:reply(200, [], HTML, Req2), \t{ok, Req3, State}.  terminate(_Reason, _Req, _State) -&gt; \tok. <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 src\/notfound_handler.erl<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"erlang\">-module(notfound_handler). -behaviour(cowboy_http_handler). %% Cowboy_http_handler callbacks -export([ \tinit\/3, \thandle\/2, \tterminate\/3 ]).  init({tcp, http}, Req, _Opts) -&gt; \t{ok, Req, undefined_state}.  handle(Req, State) -&gt; \t{URL, Req2} = cowboy_req:url(Req), \t{ok, HTML} = '404_tpl':render([{url, URL}]), \t{ok, Req3} = cowboy_req:reply(404, [], HTML, Req2), \t{ok, Req3, State}.  terminate(_Reason, _Req, _State) -&gt; \tok. <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0412\u043e\u0442, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0438 \u0432\u0441\u0435. \u041e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c <a href=\"http:\/\/localhost\">localhost<\/a>:8008\/?username=world \u0438\u043b\u0438 <a href=\"http:\/\/localhost\">localhost<\/a>:8008\/qweqweasdasd \u0438 \u0440\u0430\u0434\u0443\u0435\u043c\u0441\u044f, \u0447\u0442\u043e \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043a, \u043a\u0430\u043a \u043c\u044b \u043e\u0436\u0438\u0434\u0430\u043b\u0438.<\/p>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u044e \u0441\u0432\u043e\u0439 \u0440\u0430\u0441\u0441\u043a\u0430\u0437, \u0430 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u043c\u0443\u043b\u044c\u0442\u0438\u044f\u0437\u044b\u0447\u043d\u043e\u0441\u0442\u0438 \u0432 \u043d\u0430\u0448\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0435 \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0412\u043e\u043f\u0440\u043e\u0441\u044b, \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438, \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442\u0441\u044f \ud83d\ude09    \t \t\t   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/173595\/\"> http:\/\/habrahabr.ru\/post\/173595\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u041f\u0440\u0438\u0432\u0435\u0442!<br \/>  \u0412 \u044d\u0442\u043e\u043c \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0435 \u044f \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u044e \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0442\u0435\u043c, \u043a\u0442\u043e \u0435\u0449\u0435 \u043d\u0435 \u0437\u043d\u0430\u043a\u043e\u043c \u0441 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c Cowboy, \u043a\u0430\u043a \u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f. \u0414\u043b\u044f \u043b\u044e\u0434\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u043c\u0435\u044e\u0442 \u043e\u043f\u044b\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c, \u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b \u0432\u0440\u044f\u0434\u043b\u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d, \u0430 \u0432\u043e\u0442 \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u0437\u043d\u0430\u0435\u0442 \u043e \u041a\u043e\u0432\u0431\u043e\u0435 \u043b\u0438\u0448\u044c \u043f\u043e \u043d\u0430\u0441\u043b\u044b\u0448\u043a\u0435 \u2014 welcome!<\/p>\n<p>  \u0427\u0442\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c:  <\/p>\n<ol>\n<li>\u041f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0430\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/li>\n<li>\u041a\u0440\u0430\u0442\u043a\u0438\u0439 \u043e\u0431\u0437\u043e\u0440 \u0440\u043e\u0443\u0442\u0438\u043d\u0433\u0430, \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u0438\u043a\u0438<\/li>\n<li>\u0428\u0430\u0431\u043b\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e ErlyDTL (Django Template Language \u0434\u043b\u044f Erlang)<\/li>\n<\/ol>\n<p>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-173595","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/173595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=173595"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/173595\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=173595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=173595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=173595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}