Skip to main content

Maru Web Framework

Maru is a lightweight and modular Elixir framework that allows developers to build robust and scalable web applications.

It follows the principles of simplicity, convention over configuration, and code expressiveness. Maru is designed to be fast and efficient, making it an excellent choice for building high-performance web applications.

History

Maru was initially released in 2013 by José Valim, the creator of the Elixir programming language. It was developed as a response to the need for a lightweight and flexible web framework in the Elixir ecosystem. Since its release, Maru has gained popularity and has become one of the go-to frameworks for building Elixir-based web applications.

Features

1. Routing

Maru provides a powerful routing system that allows developers to define routes and map them to specific actions. Here's an example of how routing works in Maru:

defmodule MyApp.Router do
use Maru.Router

get "/hello", MyApp.HelloController, :index
end

In the code snippet above, a GET request to "/hello" will be handled by the index function in the MyApp.HelloController module.

2. Controllers

Maru follows the convention of representing actions as functions in controller modules. Controllers handle requests and return responses. Here's an example of a controller in Maru:

defmodule MyApp.HelloController do
use Maru.Controller

def index(conn, _params) do
text conn, "Hello, World!"
end
end

The index function takes two arguments: conn, which represents the connection, and _params, which contains any parameters passed in the request. In this example, the controller returns a simple text response of "Hello, World!".

3. Serializers

Maru provides serializers to transform Elixir data structures into the desired response format, such as JSON or XML. Serializers make it easy to build APIs by defining the structure of the response. Here's an example of a serializer in Maru:

defmodule MyApp.UserSerializer do
use Maru.Serializer

attributes [:id, :name, :email]
end

In this example, the UserSerializer defines the attributes to be included in the response. When a user object is serialized using this serializer, only the specified attributes will be included in the response.

4. Middlewares

Maru supports middlewares, which are functions that can modify the request or response before and after it reaches the controller. Middlewares provide a way to add additional functionality to the application's request/response pipeline. Here's an example of a middleware in Maru:

defmodule MyApp.MyMiddleware do
def call(conn, _opts) do
conn
|> put_resp_header("X-Custom-Header", "Hello, Maru!")
|> Maru.Conn.next_middleware(_opts)
end
end

In this example, the call function adds a custom response header to the connection before passing it to the next middleware or controller.

5. Plug Integration

Maru seamlessly integrates with Plug, a specification and set of libraries for building web applications in Elixir. This integration allows developers to leverage the power of Plug middleware and adapt Maru to their specific needs.

Examples using Maru Elixir Framework

Now let's look at a few examples of how to use Maru to build web applications.

Example 1: Hello World

defmodule MyApp.HelloController do
use Maru.Controller

def index(conn, _params) do
text conn, "Hello, World!"
end
end

In this example, the HelloController module handles a GET request to the root path ("/") and returns a text response of "Hello, World!".

Example 2: User API

defmodule MyApp.UserController do
use Maru.Controller

def index(conn, _params) do
users = MyApp.Repo.all(MyApp.User)
json conn, users, serializer: MyApp.UserSerializer
end

def show(conn, %{"id" => id}) do
user = MyApp.Repo.get(MyApp.User, id)
json conn, user, serializer: MyApp.UserSerializer
end
end

In this example, the UserController module handles GET requests to retrieve a list of users (index) and a specific user (show). The response is serialized using the MyApp.UserSerializer.

Conclusion

Maru is a powerful and flexible Elixir framework that makes it easy to build web applications. With its intuitive routing system, convention-based controllers, and support for serializers and middlewares, Maru provides everything you need to develop robust and scalable applications. Try it out and experience the simplicity and expressiveness of Maru!

For more information, you can visit the official Maru website: https://maru.readme.io