Skip to main content

ErlyWeb Overview

ErlyWeb is a lightweight web framework for building web applications using the Erlang programming language.

It follows the MVC (Model-View-Controller) architectural pattern and provides a set of tools and conventions to simplify the development of web applications.

History

ErlyWeb was initially released in 2006 by Claes Wikstrom, who developed it as an alternative to existing web frameworks for Erlang. It was designed to be simple, efficient, and easy to use, allowing developers to quickly build scalable and robust web applications.

Features

  1. Routing: ErlyWeb provides a powerful routing mechanism that allows developers to define URL patterns and map them to specific controller actions. This enables clean and intuitive URLs for the application.
-module(my_controller).

-export([index/1, show/2]).

index(Req) ->
%% Handle HTTP GET request for the index page
{ok, "Hello, World!"}.

show(Req, Id) ->
%% Handle HTTP GET request to show a specific record
%% with the given Id
{ok, "Showing record with Id " ++ Id}.
  1. Controllers: ErlyWeb uses controllers to handle HTTP requests and generate responses. Controllers are Erlang modules that define functions corresponding to different actions, such as handling GET or POST requests.

  2. Views: Views in ErlyWeb are responsible for generating the HTML content to be sent as a response. They are typically implemented as Erlang modules that define functions for rendering specific HTML templates.

-module(my_view).

-export([render_index/1, render_show/2]).

render_index(Data) ->
%% Render the index page using the provided data
{ok, "<html><body>Welcome to the index page</body></html>"}.

render_show(Data, Id) ->
%% Render the show page for the record with the given Id
{ok, "<html><body>Showing record with Id " ++ Id ++ "</body></html>"}.
  1. Models: ErlyWeb supports integration with databases through its built-in model layer. Models represent the data structure and provide an interface for interacting with the database.

  2. Session Management: ErlyWeb includes session management capabilities, allowing developers to store and retrieve user-specific data across multiple requests.

  3. Error Handling: ErlyWeb provides a robust error handling mechanism, which allows developers to gracefully handle exceptions and display appropriate error messages to the users.

  4. Testing Framework: ErlyWeb includes a testing framework that simplifies the process of writing and executing tests for the web application.

Examples

Example 1: Routing

-module(my_app).

-export([handle_request/1]).

handle_request(Req) ->
%% Route the request to the appropriate controller action
case ew_router:route(Req) of
{Controller, Action, Params} ->
Controller:Action(Req, Params);
_ ->
{error, <<"404 Not Found">>}
end.

Example 2: Controller

-module(my_controller).

-export([index/1, show/2]).

index(Req) ->
%% Handle HTTP GET request for the index page
{ok, "Hello, World!"}.

show(Req, Id) ->
%% Handle HTTP GET request to show a specific record
%% with the given Id
{ok, "Showing record with Id " ++ Id}.

Example 3: View

-module(my_view).

-export([render_index/1, render_show/2]).

render_index(Data) ->
%% Render the index page using the provided data
{ok, "<html><body>Welcome to the index page</body></html>"}.

render_show(Data, Id) ->
%% Render the show page for the record with the given Id
{ok, "<html><body>Showing record with Id " ++ Id ++ "</body></html>"}.

Conclusion

ErlyWeb is a powerful and lightweight web framework for building web applications with Erlang. It provides a range of features and conventions to simplify the development process and allows developers to focus on writing clean and efficient code. With its robust routing, controller, view, and model layers, ErlyWeb enables the creation of scalable and maintainable web applications.

For more information, refer to the official ErlyWeb documentation: ErlyWeb Official Site