This guide walks through creating a minimal Amber V2 application from scratch. By the end, you will have a working web server that responds to HTTP requests with a rendered HTML page.
- Crystal >= 1.0.0
- A text editor
Create a new Crystal project using crystal init:
crystal init app my_app
cd my_appEdit shard.yml to add the Amber framework:
name: my_app
version: 0.1.0
dependencies:
amber:
github: amberframework/amber
branch: v2-dev
crystal: ">= 1.0.0"Install dependencies:
shards installCreate the following directory structure:
my_app/
src/
my_app.cr
controllers/
home_controller.cr
views/
home/
index.ecr
layouts/
application.ecr
Create the directories:
mkdir -p src/controllers src/views/home src/views/layoutsCreate src/controllers/home_controller.cr:
require "amber/controller/base"
class HomeController < Amber::Controller::Base
def index
@title = "Welcome"
render("index.ecr")
end
endThe render macro looks for the template at src/views/home/index.ecr based on the controller name. It wraps the template in the default layout at src/views/layouts/application.ecr.
Create the layout at src/views/layouts/application.ecr:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My App</title>
</head>
<body>
<%= content %>
</body>
</html>The <%= content %> placeholder is where the action template is inserted.
Create the index template at src/views/home/index.ecr:
<h1><%= @title %></h1>
<p>Hello from Amber V2!</p>Edit src/my_app.cr to configure routing and start the server:
require "amber"
require "./controllers/*"
Amber::Server.configure do
pipeline :web do
plug Amber::Pipe::Error.new
plug Amber::Pipe::Logger.new
end
routes :web do
get "/", HomeController, :index
end
end
Amber::Server.startThis configures a :web pipeline with error handling and request logging, maps GET / to HomeController#index, and starts the HTTP server.
crystal run src/my_app.crVisit http://localhost:3000 in your browser to see your page.
By default, Amber listens on localhost:3000. To change the host or port, create a YAML configuration file or use environment variables.
AMBER_SERVER_HOST=0.0.0.0 AMBER_SERVER_PORT=8080 crystal run src/my_app.crCreate config/environments/development.yml:
name: "my_app"
server:
host: "localhost"
port: 3000
secret_key_base: "a_random_string_at_least_32_characters_long"
logging:
severity: "debug"
colorize: trueSee the Configuration Guide for all available settings.
Add additional routes and controllers as your application grows:
Amber::Server.configure do
pipeline :web do
plug Amber::Pipe::Error.new
plug Amber::Pipe::Logger.new
plug Amber::Pipe::Session.new
plug Amber::Pipe::Flash.new
plug Amber::Pipe::CSRF.new
end
routes :web do
get "/", HomeController, :index
resources "users", UsersController
resources "posts", PostsController
end
endThe resources macro generates all seven RESTful routes (index, new, create, show, edit, update, destroy) for a given controller.
- Routing -- Route definitions, resources, namespaces, constraints, and API versioning
- Configuration -- Environment YAML, environment variables, and custom config sections
- Action Helpers -- Form helpers, URL helpers, asset tags, and text formatting
- Schema API -- Type-safe, validated parameter handling
- WebSockets -- Real-time communication with channels and presence tracking
- Background Jobs -- Asynchronous job processing
- Mailer -- Email delivery with SMTP and memory adapters
- Testing -- Request helpers, assertions, and controller testing
- Markdown -- Markdown rendering with GFM support
- Migration Guide -- Migrating from Amber V1 to V2