View Source Flex
A lightweight, flexible web framework for Elixir
Table of Contents
Introduction
Flex is a lightweight and flexible web framework for Elixir, designed to make web development a breeze. Inspired by the simplicity of Flask and powered by the robustness of Elixir, Flex provides developers with an intuitive API for building scalable web applications quickly and efficiently.
Features
- Intuitive Routing: Define your routes with a clean, expressive syntax
- Blazing Fast: Leverages the power of the BEAM VM for high performance
- Hot Code Reloading: Enjoy seamless development with automatic code reloading
- Flexible Structure: Organize your code the way that makes sense for your project
- Templating: Built-in support for EEx templates with layouts
- Static File Serving: Easily serve static assets
- Plug Integration: Seamless integration with the Plug ecosystem
- Extensible: Easy to extend with custom modules and plugins
Installation
Install Flex with:
mix archive.install hex flex_web
Quick Start
Create a new Flex project:
mix flex.new my_app
cd my_app
mix deps.get
Start your Flex server:
mix flex.server
Visit http://localhost:4000
in your browser to see your Flex application in action!
Usage
Defining Routes
defmodule MyApp.Controllers.HomeController do
use Flex.Controller
defroute :index, "/" do
html_response(conn, "home.html.eex", %{message: "Welcome to Flex!"})
end
defroute :about, "/about" do
html_response(conn, "about.html.eex", %{})
end
defroute :api_example, "/api/example", methods: [:get] do
json_response(conn, %{message: "This is a JSON response"})
end
end
Templates
Flex uses EEx for templating. Templates are located in the lib/templates
directory.
You can use base html templates like the app.html.eex
generated in the lib/templates/base
folder.
For the content from other templates to be placed inside the base template, ensure the @main_content
exists in the base, like:
<main>
<%= @main_content %>
</main>
Example template (lib/templates/home.html.eex
):
<h1>Welcome to Flex</h1>
<p><%= @message %></p>
Configuration
Configure Flex in your config/config.exs
:
config :flex_web,
port: 4000,
static_path: Path.join(File.cwd!, "priv/static"),
templates_path: Path.join(File.cwd!, "lib/templates")
Contributing
We welcome contributions to Flex! Please see our CONTRIBUTING.md for details on how to get started.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -am 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE for more information.
##
Made with ❤️ by the Flex Web Team