setup.tex 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. \section{Setup}
  2. \subsection{Prerequisites}
  3. To run N2O websites you need to install Erlang version 18 or higher.
  4. N2O works on Windows, Mac and Linux.
  5. \subsection{Kickstart Bootstrap}
  6. To try N2O you only need to fetch it from Github and build. We don't use
  7. fancy scripts, so building process is OTP compatible: bootstrap site
  8. is bundled as an Erlang release.
  9. \vspace{1\baselineskip}
  10. \begin{lstlisting}
  11. $ git clone git://github.com/synrc/n2o
  12. $ cd n2o/samples
  13. $ ./mad deps compile plan repl
  14. \end{lstlisting}
  15. \vspace{1\baselineskip}
  16. Now you can try: \footahref{http://localhost:8000}{http://localhost:8000}.
  17. On Linux you should do at first:
  18. \vspace{1\baselineskip}
  19. \begin{lstlisting}
  20. $ sudo apt-get install inotify-tools
  21. \end{lstlisting}
  22. \vspace{1\baselineskip}
  23. \newpage
  24. \subsection{Application Template}
  25. If you want to start using N2O inside your application, you can use Cowboy dispatch parameter
  26. for passing HTTP, REST, WebSocket and Static N2O endpoints:
  27. \vspace{1\baselineskip}
  28. \begin{lstlisting}[caption=sample.erl]
  29. -module(sample).
  30. -behaviour(supervisor).
  31. -behaviour(application).
  32. -export([init/1, start/2, stop/1, main/1]).
  33. main(A) -> mad:main(A).
  34. start(_,_) -> supervisor:start_link({local,review},review,[]).
  35. stop(_) -> ok.
  36. init([]) -> { ok, { { one_for_one, 5, 100 }, [spec()] } }.
  37. spec() -> ranch:child_spec(http, 100, ranch_tcp, port(), cowboy_protocol, env()).
  38. port() -> [ { port, wf:config(n2o,port,8000) } ].
  39. env() -> [ { env, [ { dispatch, points() } ] } ].
  40. static() -> { dir, "apps/sample/priv/static", mime() }.
  41. n2o() -> { dir, "deps/n2o/priv", mime() }.
  42. mime() -> [ { mimetypes, cow_mimetypes, all } ].
  43. points() -> cowboy_router:compile([{'_', [
  44. { "/static/[...]", n2o_static, static()},
  45. { "/n2o/[...]", n2o_static, n2o()},
  46. { "/ws/[...]", n2o_stream, []},
  47. { '_', n2o_cowboy, []} ]}]).
  48. \end{lstlisting}
  49. \vspace{1\baselineskip}
  50. \newpage
  51. While Listing 1 is a little bit criptic we want to say that N2O
  52. intentionally not introduced here any syntax sugar.
  53. For any Erlang application you need to create application
  54. and supervisor behavior modules which we combined in the
  55. same Erlang file for simplicity.
  56. \paragraph{}
  57. Cowboy routing rules also leaved as is.
  58. We'd better to leave our efforts for making N2O protocol
  59. and N2O internals simplier. Here we can't fix a much. Just use this
  60. as template for bootstrapping N2O based applications.
  61. \subsection{Companion Dependencies}
  62. For raw N2O use with BERT message formatter you need only
  63. one N2O dependecy, but if you want to use DTL templates,
  64. JSON message formatter, SHEN JavaScript Compiler or NITRO
  65. Nitrogen DSL you can plug all of them separately.
  66. \vspace{1\baselineskip}
  67. \begin{lstlisting}
  68. https://github.com/synrc/n2o 2.9
  69. https://github.com/synrc/nitro 2.9
  70. https://github.com/synrc/kvs 2.9
  71. https://github.com/synrc/active 2.9
  72. https://github.com/synrc/shen 1.5
  73. https://github.com/synrc/rest 1.5
  74. https://github.com/spawnproc/bpe 1.5
  75. https://github.com/spawnproc/forms 1.5
  76. \end{lstlisting}
  77. \vspace{1\baselineskip}
  78. \newpage
  79. \subsection{Configuration}
  80. \vspace{1\baselineskip}
  81. \begin{lstlisting}[caption=sys.config]
  82. [{n2o, [{port,8000},
  83. {app,review},
  84. {route,routes},
  85. {mq,n2o_mq},
  86. {json,jsone},
  87. {log_modules,config},
  88. {log_level,config},
  89. {log_backend,n2o_log},
  90. {session,n2o_session},
  91. {origin,<<"*">>},
  92. {bridge,n2o_cowboy},
  93. {pickler,n2o_pickle},
  94. {erroring,n2o_error}]},
  95. {kvs, [{dba,store_mnesia},
  96. {schema, [kvs_user,
  97. kvs_acl,
  98. kvs_feed,
  99. kvs_subscription ]} ]} ].
  100. \end{lstlisting}
  101. \vspace{1\baselineskip}
  102. \subsection*{Ports}
  103. N2O uses two ports for WebSocket and HTTP connections.
  104. \vspace{1\baselineskip}
  105. \begin{lstlisting}
  106. wf:config(n2o,port,443)
  107. wf:config(n2o,websocket_port,443)
  108. \end{lstlisting}
  109. \vspace{1\baselineskip}
  110. If you use server pages mode N2O will render HTML with nessesary ports values.
  111. For single page application mode you should redefine these ports inside the template:
  112. \vspace{1\baselineskip}
  113. \begin{lstlisting}
  114. <script> var transition = { pid: '',
  115. host: 'localhost',
  116. port: '443' }; </script>
  117. \end{lstlisting}
  118. \vspace{1\baselineskip}
  119. \newpage
  120. \subsection*{Application}
  121. In {\bf app} setting you should place the name of your OTP application
  122. that will be treated by N2O and NITRO as a source for templates and
  123. other static data with {\bf code:priv\_dir}.
  124. \subsection*{Routes}
  125. Setting {\bf route} is a place for the name of Erlang module where
  126. resides mappings from URL to page modules.
  127. \subsection*{Logging}
  128. N2O supports logging API and you can plug different logging module.
  129. It ships with {\bf n2o\_io} and {\bf n2o\_log} modules which you can set in the
  130. {\bf log\_backend} option. This is how logging looks like in N2O:
  131. \vspace{1\baselineskip}
  132. \begin{lstlisting}
  133. wf:info(index,"Message: ~p",[Message]),
  134. \end{lstlisting}
  135. \vspace{1\baselineskip}
  136. First argument is a module from which function is being called. By using this
  137. N2O can filter all log messages with special filter settled with {\bf log\_modules} option.
  138. It says in which Erlang module function {\bf log\_modules/0} exists that
  139. returns allowed Erlang modules to log. Option {\bf log\_level} which specified
  140. in a similar way, it specifies the module with function {\bf log\_level/0} that could
  141. return one of {\bf none}, {\bf error}, {\bf warning} or {\bf info} atom values which
  142. means different error log levels.
  143. \vspace{1\baselineskip}
  144. \begin{lstlisting}
  145. -module(config).
  146. -compile(export_all).
  147. log_level() -> info.
  148. log_modules() -> [ login, index ].
  149. \end{lstlisting}
  150. \vspace{1\baselineskip}
  151. \newpage
  152. \subsection*{Message Queue}
  153. In {\bf mq} settings you should place the name of Erlang module which supports
  154. message queue API. By default N2O provides {\bf n2o\_mq} module.
  155. \subsection*{Formatter}
  156. With {\bf formatter} option you may set the WebSocket channel
  157. termination formatter such as {\bf bert} or {\bf json}. If you will select json as formatter
  158. you may want also to chose a json encoder in {\bf json} option. By default in n2o enabled
  159. {\bf json} formatter and {\bf jsone} encoder. The main reason is that {\bf jsone} is
  160. written in pure erlang idiomatic code and is ready to run on LING without C NIF linkage.
  161. But you may want to switch to {\bf jsonx} on BEAM or whatever.
  162. \subsection*{Minimal Page}
  163. And then add a minimal {\bf index.erl} page:
  164. \vspace{1\baselineskip}
  165. \begin{lstlisting}[caption=index.erl]
  166. -module(index).
  167. -compile(export_all).
  168. -include_lib("nitro/include/nitro.hrl").
  169. main() -> #span{body="Hello"}.
  170. \end{lstlisting}
  171. \vspace{1\baselineskip}