From Scratch
You can create an Amber project from scratch using crystal init app.
1
$ crystal init app myapp
2
create myapp/.gitignore
3
create myapp/.editorconfig
4
create myapp/LICENSE
5
create myapp/README.md
6
create myapp/.travis.yml
7
create myapp/shard.yml
8
create myapp/src/myapp.cr
9
create myapp/src/myapp/version.cr
10
create myapp/spec/spec_helper.cr
11
create myapp/spec/myapp_spec.cr
12
Initialized empty Git repository in /home/main/myapp/.git/
Copied!

Add Amber dependency

First you require to add the amber shard dependency in your shard.yml file:
1
name: myapp
2
version: 0.1.0
3
4
authors:
5
- Foo Bar <[email protected]>
6
7
targets:
8
myapp:
9
main: src/myapp.cr
10
11
crystal: 0.24.2
12
13
license: MIT
14
15
dependencies:
16
amber:
17
github: amberframework/amber
18
version: 0.7.2
Copied!
Then execute shards install :
1
$ shards install
2
Fetching https://github.com/amberframework/amber.git
3
Fetching https://github.com/amberframework/router.git
4
Fetching https://github.com/amberframework/cli.git
5
Fetching https://github.com/mosop/optarg.git
6
Fetching https://github.com/mosop/callback.git
7
Fetching https://github.com/mosop/string_inflection.git
8
Fetching https://github.com/elorest/compiled_license.git
9
Fetching https://github.com/jeromegn/kilt.git
10
Fetching https://github.com/amberframework/micrate.git
11
Fetching https://github.com/crystal-lang/crystal-db.git
12
Fetching https://github.com/crystal-lang/crystal-mysql.git
13
Fetching https://github.com/will/crystal-pg.git
14
Fetching https://github.com/stefanwille/crystal-redis.git
15
Fetching https://github.com/jwaldrip/shell-table.cr.git
16
Fetching https://github.com/jeromegn/slang.git
17
Fetching https://github.com/crystal-lang/crystal-sqlite3.git
18
Fetching https://github.com/phoffer/inflector.cr.git
19
Fetching https://github.com/amberframework/teeplate.git
20
Installing amber (0.7.2)
21
Installing amber_router (0.0.3)
22
Installing cli (0.7.0)
23
Installing optarg (0.5.8)
24
Installing callback (0.6.3)
25
Installing string_inflection (0.2.1)
26
Installing compiled_license (0.1.3)
27
Installing kilt (0.4.0)
28
Installing micrate (0.3.1)
29
Installing db (0.5.0)
30
Installing mysql (0.4.0)
31
Installing pg (0.14.1)
32
Installing redis (1.9.0)
33
Installing shell-table (0.9.2)
34
Installing slang (1.7.1)
35
Installing sqlite3 (0.9.0)
36
Installing inflector (0.1.8)
37
Installing teeplate (0.5.0)
Copied!

Create project structure

All Amber's projects have a directory structure. Basically you need a common file tree (unless you use a Minimal Configuration).
1
.
2
├── config
3
│ ├── application.cr
4
│ └── routes.cr
5
└── src
6
├── controllers
7
│ └── application_controller.cr
8
└── myapp.cr
Copied!
First you need a config/folder with an application.cr file, also see configuration.
config/application.cr
1
require "amber"
2
3
require "../src/controllers/**"
Copied!
Secondly a routes.cr file with pipelines and routes blocks, also see: routing.
config/routes.cr
1
Amber::Server.configure do |app|
2
pipeline :web do
3
# plug PipeName.new
4
end
5
6
routes :web do
7
# get "/", SomeController, :some_action
8
end
9
end
Copied!
Then a basic setup (without views nor models) requires an application_controller.cr file inside src/controllers directory.
src/controllers/application_controller.cr
1
class ApplicationController < Amber::Controller::Base
2
end
Copied!
Finally you need to call Amber::Server and all your project in your main myapp.crfile.
src/myapp.cr
1
require "../config/*"
2
3
Amber::Server.start
Copied!

Build and run your project

To compile your project use shards build myappand run the executable with bin/myapp.
1
$ shards build myapp
2
Dependencies are satisfied
3
Building: myapp
4
$ bin/myapp
5
06:52:37 Server | (INFO) Amber 0.7.2 serving application "Amber_app" at http://localhost:3000
6
06:52:37 Server | (INFO) Server started in development.
7
06:52:37 Server | (INFO) Startup Time 00:00:00.000273000
Copied!
That's it!, now you're ready to create any basic project from scratch without Amber CLI.

Local CLI

As additional step you can compile an amber CLI inside your project using:
1
crystal build lib/amber/src/amber/cli.cr -o bin/amber -s
Copied!
Then use bin/amber to do scaffolding, run migrations and more.
Last modified 3yr ago