Amber Framework
WebsiteBlogGithubDiscord
  • Introduction
  • Getting Started
  • Guides
    • Overview
    • Installation
    • Create New App
    • Directory Structure
    • Configuration
    • Docker
    • Controllers
      • Sessions
      • Request & Response Objects
      • Halt!
      • Respond With
      • Params
      • Cookies
      • Filters
      • Flash
      • Redirection
      • CSRF
    • Views
      • Basic View Helpers
      • Form Builder
    • Models
      • Granite
        • Granite's README
        • Migrations
        • Validations
        • Callbacks
        • Associations
        • Querying
        • Bulk Insertions
      • Jennifer
        • Jennifer Docs
        • Migrations
        • Models
    • Routing
      • Pipelines
      • Routes
    • Websockets
      • Channels
      • Sockets
      • JavaScript Client
    • Mailers
    • Testing
      • System Tests
  • Deployment
    • Manual Deploy
    • Digital Ocean
    • Heroku
    • Dokku
  • CLI
    • New
    • Recipes
    • Plugins
    • Generate
    • Database
    • Watch
    • Routes
    • Exec
    • Encrypt
  • Examples
    • Amber Auth
    • Crystal Debug
    • Minimal Configuration
  • Cookbook
    • From Scratch
    • Hello World
    • CORS
    • File Download
    • File Upload
    • Cookies
    • Authenticate
    • JSON API
    • JSON Mapping
    • WebSocket Chat
  • Troubleshooting
  • In Production
  • Contributing
  • Code of Conduct
  • HAVE A QUESTION?
    • Join the Discord
    • Follow on Twitter
    • Submit an issue
Powered by GitBook
On this page
  • Add Amber dependency
  • Create project structure
  • Build and run your project
  • Local CLI
  1. Cookbook

From Scratch

PreviousCookbookNextHello World

Last updated 2 years ago

You can create an Amber project from scratch using crystal init app.

Also see .

$ crystal init app myapp
      create  myapp/.gitignore
      create  myapp/.editorconfig
      create  myapp/LICENSE
      create  myapp/README.md
      create  myapp/.travis.yml
      create  myapp/shard.yml
      create  myapp/src/myapp.cr
      create  myapp/src/myapp/version.cr
      create  myapp/spec/spec_helper.cr
      create  myapp/spec/myapp_spec.cr
Initialized empty Git repository in /home/main/myapp/.git/

Add Amber dependency

First you require to add the amber shard dependency in your shard.yml file:

name: myapp
version: 0.1.0

authors:
  - Foo Bar <foo@bar.baz>

targets:
  myapp:
    main: src/myapp.cr

crystal: 0.24.2

license: MIT

dependencies:
  amber:
    github: amberframework/amber
    version: 0.7.2

Then execute shards install :

$ shards install
Fetching https://github.com/amberframework/amber.git
Fetching https://github.com/amberframework/router.git
Fetching https://github.com/amberframework/cli.git
Fetching https://github.com/mosop/optarg.git
Fetching https://github.com/mosop/callback.git
Fetching https://github.com/mosop/string_inflection.git
Fetching https://github.com/elorest/compiled_license.git
Fetching https://github.com/jeromegn/kilt.git
Fetching https://github.com/amberframework/micrate.git
Fetching https://github.com/crystal-lang/crystal-db.git
Fetching https://github.com/crystal-lang/crystal-mysql.git
Fetching https://github.com/will/crystal-pg.git
Fetching https://github.com/stefanwille/crystal-redis.git
Fetching https://github.com/jwaldrip/shell-table.cr.git
Fetching https://github.com/jeromegn/slang.git
Fetching https://github.com/crystal-lang/crystal-sqlite3.git
Fetching https://github.com/phoffer/inflector.cr.git
Fetching https://github.com/amberframework/teeplate.git
Installing amber (0.7.2)
Installing amber_router (0.0.3)
Installing cli (0.7.0)
Installing optarg (0.5.8)
Installing callback (0.6.3)
Installing string_inflection (0.2.1)
Installing compiled_license (0.1.3)
Installing kilt (0.4.0)
Installing micrate (0.3.1)
Installing db (0.5.0)
Installing mysql (0.4.0)
Installing pg (0.14.1)
Installing redis (1.9.0)
Installing shell-table (0.9.2)
Installing slang (1.7.1)
Installing sqlite3 (0.9.0)
Installing inflector (0.1.8)
Installing teeplate (0.5.0)

Create project structure

.
├── config
│   ├── application.cr
│   └── routes.cr
└── src
    ├── controllers
    │   └── application_controller.cr
    └── myapp.cr

Then a basic setup (without views nor models) requires an application_controller.cr file inside src/controllers directory.

Finally you need to call Amber::Server and all your project in your main myapp.crfile.

Build and run your project

To compile your project use shards build myappand run the executable with bin/myapp.

$ shards build myapp
Dependencies are satisfied
Building: myapp
$ bin/myapp 
06:52:37 Server     | (INFO) Amber 0.7.2 serving application "Amber_app" at http://localhost:3000
06:52:37 Server     | (INFO) Server started in development.
06:52:37 Server     | (INFO) Startup Time 00:00:00.000273000

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:

crystal build lib/amber/src/amber/cli.cr -o bin/amber -s

All Amber's projects have a . Basically you need a common file tree (unless you use a ).

First you need a config/folder with an application.cr file, also see .

Secondly a routes.cr file with pipelines and routes blocks, also see: .

Then use bin/amber to do scaffolding, run migrations and .

creating a project or library
directory structure
Minimal Configuration
configuration
routing
more