воскресенье, 7 января 2018 г.

Getting started with N2O Erlang web framework

Synrc.com guys did a great job with creating N2O framework. Let's try to make some app with it.
But before we start we need to install MAD. Mad is a deps management & build kind of tool.
Let's download it. Open this link and press Download button.  What we've just got is a file with compiled erlang bytecode inside and it's self executable.
Place it somewere in your $PATH (for example "/usr/local/bin") just to be able to execute it anywhere in your console.
Then you can type "mad app helloworld" which is going to create a new directory called helloworld with a bunch of files in it.
helloworld/rebar.config will have something like this in it:
{sub_dirs,["apps"]}.
{deps_dir,"deps"}.
{deps, [
    {erlydtl,".*", {git, "git://github.com/evanmiller/erlydtl", {tag, "0.8.0"}  }},
    {cowboy, ".*", {git, "git://github.com/extend/cowboy",      {tag, "1.0.1"}  }},
    {gproc,  ".*", {git, "git://github.com/uwiger/gproc.git",   {tag, "0.3"}    }},
    {fs,     ".*", {git, "git://github.com/synrc/fs",           {tag, "1.9"}    }},
    {sh,     ".*", {git, "git://github.com/synrc/sh",           {tag, "1.9"}    }},
    {mad,    ".*", {git, "git://github.com/synrc/mad",          {tag, "1.9"} }},
    {active, ".*", {git, "git://github.com/synrc/active",       {tag, "1.9"} }},
    {nitro,  ".*", {git, "git://github.com/synrc/nitro",        {tag, "0.9"} }},
    {n2o,    ".*", {git, "git://github.com/synrc/n2o",          {tag, "4.4"} }},
]}.
These are dependencies, which MAD is going to download and place in a deps_dir, which happens to be "deps" as it says on line 2.   As you can see n2o is in the list already.

So now we can just try to run the template app. Type this commands:
cd helloworld
mad dep com pla rep
If everythings runs smoothly you are going to see some console output. And then you can press Enter and there's "1> " line at the bottom, which means Erlang virtual machine started successfully and opened REPL for you.
First argument of  "mad dep com pla rep" command is "dep" which is "fetch all my dependencies from rebar.config". After that comes "com" which is "compile all the files in sub_dirs, and deps_dir".  And then comes "pla" which is "search over the project files and build the plan of applications structure so I can be sure of right start order of my apps"
And finally there's "rep" which is "open the repl".

And now if you look into sys.config you're going to see something like this
[{n2o, [{port,8001},{route,routes},{log_modules,sample}]}].
Aha, the auto-generated template says to start n2o on port 8001. Since we already started our project with MAD let's open "localhost:8001" in the browser.




And here it is, our first app on N2O.

Комментариев нет:

Отправить комментарий