Amethyst on the Rocks
Amethyst on the Rocks (just Rocks to its friends) is a web framework for Amethyst in the spirit of Ruby on Rails: conventions over configuration, generators for the boring parts, and one obvious place for everything.
Underneath it is ASP.NET Core, so Rocks gets Kestrel, dependency injection and middleware for free. Rocks supplies the conventions and the good vibes. ASP.NET does the heavy lifting, as it has for years, mostly without complaint.
A taste
Section titled “A taste”Rocks.routes do root "posts#index"
resources :posts do resources :comments, only: [:create] endendclass PostsController < Rocks::Controller def index: Response render :index, posts: Post.order(:created_at, :desc).all end
def show(id: Int): Response post = Post.find(id) return not_found unless post render :show, post: post endendclass Post < Strata::Record @[Required] property title: String
property body: StringendThe parts
Section titled “The parts”Every good rock has layers. Rocks has five:
| Part | Does | Rails equivalent | |
|---|---|---|---|
| Veins | Routing | Routes | Veins are how things travel through rock. |
| Controllers | Handle requests | Action Controller | No pun. We tried. |
| Strata | Models, queries, migrations | Active Record | Your data, settled in layers. |
| Facet | Templates | Action View / ERB | Every page has many facets. |
| Tumbler | Background jobs | Active Job | Polishes things slowly, out of sight. |
Getting started
Section titled “Getting started”Install the rocks command, create an app, and serve it:
dotnet tool install --global Amethyst.Rocks.Clirocks new blogcd blogrocks generate scaffold Post title:String body:Textrocks db migraterocks serveYour app is now running at http://localhost:5000, served chilled.
Project layout
Section titled “Project layout”blog/├── blog.amethystproj├── project.am # dependencies, including Amethyst.Rocks├── app/│ ├── controllers/│ ├── models/│ ├── views/│ └── jobs/├── config/│ ├── routes.am│ └── database.am└── db/ └── migrations/Rocks finds things by convention: PostsController lives in
app/controllers/posts_controller.am, and its index action renders
app/views/posts/index.facet. Put things where Rocks expects them and you’ll rarely
write configuration. Put them anywhere else and you’ll write configuration, and also an
apology.
Configuration
Section titled “Configuration”Configuration is plain Amethyst, using a receiver block:
Rocks.configure do database adapter: :postgres, url: env("DATABASE_URL")
environment :test do database adapter: :sqlite, url: "db/test.sqlite3" endendThe Rocks doctrine
Section titled “The Rocks doctrine”- Convention over configuration. Sensible defaults, so you only configure what’s actually unusual about your app.
- Types all the way down. Routes, parameters, models and templates are all checked by the compiler. A typo in a view is a build error, not a support ticket.
- It’s just .NET. Any ASP.NET Core middleware, NuGet package or C# library works. Rocks is a dialect, not a walled garden.
- Stay sober. No magic you can’t find with Go to Definition.