📜 ⬆️ ⬇️

CowboyD: demonizing Cowboy, the embedded web server for Erlang

What am I talking about?


Where does almost every web-application on Erlang begin? I do not know who has it, but I, as a rule, have lines of code responsible for launching Cowboy and serving requests. With the function of updating the rules of routing. And it is always the same thing - only the routes are different, the port and the number of acceptors, maybe. And it can be made easier. Already guessed, what am I getting at? No, this is not just another framework. This is the transformation of the embedded cowboy into a separate running application. That is, we write our application, Cowboy Handlers, but we don’t touch Cowboy. At all. We do not specify any dependencies, or launch the application. Curious? Welcome under cat.

Instead, when the application is ready, we execute the command in the console.

cowboyd start myapp /path/to/myapp 8080 

and miraculously, our application is running and you can admire it on port 8080. It is also easy to stop:

 cowboyd stop myapp 

At the same time, of course, you can run as many applications as you want - the main thing to remember is to give them different ports;)
The routing rules are written in the routes.config file in the root directory of your application. Erlang syntax, just do not need to declare a module, export functions and, in fact, write them. It's easier to look at the routes.config example. If you need to update the routing rules, just run the next simple command:
')
 cowboyd routes-update myapp 

The code after the update is reloaded automatically thanks to the Sync utility.

So, we finished with the introduction, now the main and, at the same time, the shortest part.

Installation


We go somewhere where we will put a cowboyd
 cd ~/github_projects 

Clone project repository
 git clone https://github.com/chvanikoff/cowboyd 

We give the rights to performances for cowboyd
 chmod +x cowboyd/cowboyd 

We link cowboyd somewhere in the executable directory - for example, in / usr / bin
 sudo ln -s cowboyd/cowboyd /usr/bin/cowboyd 

That's all, now you can run the test application from the repository

 cowboyd start webapp ~/github_projects/cowboyd/examples/webapp 8008 

The application can say that it is running at http: // localhost: 8008 , serve static css from ~ / github_projects / cowboyd / examples / webapp / priv / css and give 404 for all other pages. As soon as it gets boring for you, it's time to write something of your own and try cowboyd in action;)

Repository link: github.com/chvanikoff/cowboyd

Thanks for attention.

Source: https://habr.com/ru/post/187844/


All Articles