• Weber:
  • MVC Web framework for Elixir language
  • Flexible routing
  • Websocket support
  • Sessions
  • Grunt integration
  • Html Helpers
  • And many more...

Quick start

There are some simple steps for running weber based application:

  • Get and install Elixir from master.
  • Clone weber repo with git clone https://github.com/elixir-web/weber
  • Execute make && make test in the weber directory
  • Create new project with: mix weber.new /home/user/testWebApp or mix weber.new /home/user/testWebApp --grunt for Grunt integration.
  • go to the /home/user/testWebApp and execute there: mix deps.get && mix compile --all --force && ./start.sh
  • Open browser at http://localhost:8080/

Documentation


Weber Configuration

Weber's configuration is in config.ex configuration file

There are the following configuration parameters:

  • http_host - Web server host
  • http_port - Weber server port
  • acceptors - Acceptors count
  • ssl - Use ssl or not
  • ws_port - Websocket port
  • ws_mod - Websocket handler module
  • user_internationalization - Use internationalization or not
  • default_locale - Defualt locatization
  • use_locales - Which locales to use
  • use_sessions - Enable/Disable sessions
  • max_age - Session life time

If you need to access to the configuration, you can get it with Config.config

Routing

When you've created a new Weber web application, you will have a route.ex file in the /home/user/testWeberApplication/lib directory. This is the app's routing declaration.

It consists of a route macro whose value is a chain of functions with 3 parameters:

  • Http method;
  • Route path, can be binding (starts with ':' symbol);
  • Module name of controller;
  • Function name from this controller.

Http method can be:

  • "GET"
  • "POST"
  • "PUT"
  • "DELETE"
  • "ANY"

Weber.Route API:

  • Weber.Route.link(:controller, :action, [binding: "value"]) - Creates url from Elixir code

Web controllers

All weber's controllers are in the /project/lib/controllers directory. They can be in any sub-directories.

All weber's controllers must use Weber.Controller

All weber's controllers have an action, which are usually Elixir functions that take two parameters. They are executed when new requests are routed to them.

Action parameter:

  • [binding_keyword_list] - if there is request: /username/0xAX to the url /username/:username, you will get [username: username] binding value
  • connection - use it for getting information about request (cookie, headers, ...)

Responses

Weber supports different responses:

Render views with bindings

Or:

Render inline template

Render any view in controller.

File

JSON

Redirect

Plain text

Nothing

Views

Weber uses EEx templates for views. It's just html code with embedded Elixir code. There are the following types of tags:

  • <% Elixir expression - inline with output %>
  • <%= Elixir expression - replace with result %>
  • <%% EEx quotation - returns the contents inside %>
  • <%# Comments - they are discarded from source %>
Will generate:

Models

Weber uses the Ecto library for building data models. Weber's model is a just Elixir an module which uses Ecto API and is located in your_project_name/lib/models directory.


Session

Weber's session manager is automatically started with any web application. Weber has simple API for managing sessions:

  • get_session(conn) -> [] | [{atom, binary()}]
  • get_session(conn, key :: atom()) -> [] | binary()
  • set_session_val(conn, key :: atom(), val :: binary()) -> :ok.

Weber's sessions backend:

  • ETS

Helpers

There are many helpers in Weber which can help you to develop your web application:

Html helpers

Resource helpers

include_view helpers

WebSocket

Weber has WebSockets support and you can handle websocket connection and incoming/outgoing websocket messages in your actions. First of all you need to designate websocket controller in your config.ex file in webserver: section, like:

ws_mod: :Simplechat.Main.Chat

After that you must implement three callbacks:

  • websocket_init(pid, conn) - initialization of new websocket connection
  • websocket_message(pid, message, conn) - incoming messages handler
  • websocket_terminate(pid, conn) - connection terminated

Note that all websocket connections must start with prefix /_ws/.