Skip to content

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.

config/routes.am
Rocks.routes do
root "posts#index"
resources :posts do
resources :comments, only: [:create]
end
end
app/controllers/posts_controller.am
class 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
end
end
app/models/post.am
class Post < Strata::Record
@[Required]
property title: String
property body: String
end

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.

Install the rocks command, create an app, and serve it:

Terminal window
dotnet tool install --global Amethyst.Rocks.Cli
rocks new blog
cd blog
rocks generate scaffold Post title:String body:Text
rocks db migrate
rocks serve

Your app is now running at http://localhost:5000, served chilled.

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 is plain Amethyst, using a receiver block:

config/database.am
Rocks.configure do
database adapter: :postgres, url: env("DATABASE_URL")
environment :test do
database adapter: :sqlite, url: "db/test.sqlite3"
end
end
  1. Convention over configuration. Sensible defaults, so you only configure what’s actually unusual about your app.
  2. 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.
  3. It’s just .NET. Any ASP.NET Core middleware, NuGet package or C# library works. Rocks is a dialect, not a walled garden.
  4. Stay sober. No magic you can’t find with Go to Definition.