Elixir app with Plug and Cowboy

Mahabub Islam Prio
Aug 31, 2018 · 2 min read
Taken from Unsplash

This week I am going to demonstrate about how you can create an Elixir app with the power of Plug and Cowboy. And after reading this post you can create an application that can provide you with better understanding of these tools and its also works as a template for creating small web applications if you want to start.

So, to start we are going to create an empty project.

mix new app_name --module=ModuleName # in my case I named it 'tarragon'

after creating the project we will have to cd app_folder and add some dependencies over mix.exs

defp deps do                           
[
{:cowboy, "~> 2.0"},
{:plug, "~> 1.0"}
]
end

and after saving the file, run mix deps.get

Now to get the application on its feet, I started to add routers for endpoint accesses. I created lib/router/main_router.ex and lib/router/sites_router.ex

and the another router file,

In main_router.ex file I am forwarding the routes that starts with sites to the specific sites_router.ex

Here we also can add eex template for the view or json like in this case

I created app/views/file_name.html.eex and app/static/css/file_name.css.eex

and used them as response over the router.

Now its time to boot up the Cowboy Server and see how does it really looks.

> iex -S mix> {:ok, _} = Plug.Adapters.Cowboy2.http Router.MainRouter, []
> # will return a process id

Now visit localhost:4000 and visit routes.


If you want to add up Ecto with this project you will just have to add up it to mix file.

Add a supervisor over file lib/tarragon/application.ex and add repo.ex and change config.ex

and please remember to start Repo with supervisor .

This is all now about Elixir app with Plug and Cowboy.

For the codes please visit: https://github.com/prio101/tarragon

 by the author.

Mahabub Islam Prio

Written by

Ruby On Rails, Elixir, Javascript developer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade