CowboyD: demonizing Cowboy, an embedded web server for Erlang
What am I talking about?
Where does almost every Erlang web application begin? I don’t know how anyone has it, but I, as a rule, have lines of code responsible for launching Cowboy and servicing requests. With the function of updating routing rules. Moreover, it is always the same thing - only the routes differ, the port and the number of acceptors, maybe. And it can be made easier. Guessed already, what am I getting at? No, this is not another framework. This is turning an embedded cowboy into a stand-alone application. That is, we are writing our application, cowboy handlers, but we don’t touch the cowboy himself. At all. We don’t specify any dependencies, nor do we launch them when the application starts. Curious? Welcome to 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. Stopping is just as easy:
cowboyd stop myapp
At the same time, of course, you can run as many applications as you like - the main thing is not to forget to give them different ports;) Routing
rules are written in the routes.config file in the root directory of your application. Erlang syntax, you just don’t 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
After updating, the code restarts automatically thanks to the Sync utility .
So, with the introductory part finished, now the main and, at the same time, the shortest part.
Installation
Go somewhere where we put cowboyd
cd ~/github_projects
Clone the project repository
git clone https://github.com/chvanikoff/cowboyd
We give performance rights for cowboyd
chmod +x cowboyd/cowboyd
Link cowboyd somewhere to an 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 bores you - it's time to write something of your own and try cowboyd in business;)
Link to the repository: github.com/chvanikoff/cowboyd
Thank you for your attention.