Project Structure
A Runcobo application is organized by business area. Each area is one flat folder under src/actions/, and every endpoint in it is three files with the same name: the action, its contract and its template. Everything else in src/ is grouped by responsibility.
bin/cobo init creates this layout for a new project. The examples throughout this guide come from this blog application:
One endpoint, three files
An endpoint's files sit next to each other and share a name:
class Posts::ShowPostContract < RequestValidation::Contract
schema do
required :id, Int64
end
endclass Posts::ShowPost < Blog::BaseAction
get "/posts/:id"
call do
input = validate(ShowPostContract) # path, query and body, checked together
if post = store.get_post?(id: input.id)
render # → src/actions/posts/show_post.jbuilder
else
render_json({error: "post_not_found"}, status_code: 404)
end
end
endjson.partial! "posts/partials/post", post: post# locals: post
json.id post.id
json.title post.title
json.status post.status- Names line up.
Posts::ShowPostlives inposts/show_post.cr, its contract isPosts::ShowPostContractinshow_post_contract.cr, andrenderfindsshow_post.jbuilderbeside it. InsidePosts::ShowPost, the contract can be written as justShowPostContract. - Input is read with
validate. The first line ofcallvalidates the request against the action's contract and returns typed input — see Request Contracts. - Qualified class names. Write
class Posts::ShowPost, not amodule Postswrapper in every file. - No per-endpoint folders. An area stays flat. A contract's nested contracts are inner classes in the same contract file.
- Shared code stays in the area. Functions several actions in an area need go in its
support.cr; shared templates go in itspartials/.
Changing an endpoint means opening one folder, and everything about it — route, input, logic, response — is in front of you.
Where everything else goes
| Folder | Holds |
|---|---|
actions/<area>/ | Actions, contracts, templates, support.cr, partials/ |
web/ | The request boundary: route handlers, middleware, authentication |
persistence/ | Database access code you write; generated queries in persistence/generated/ |
shared/ | Plain helpers used across areas |
integrations/ | Adapters for external services |
app.cr and main.cr
app.cr loads the application in dependency order — the generated queries and the base action first, then everything that uses them:
require "runcobo"
require "pg"
require "./persistence/generated/queries"
require "./actions/base_action"
require "./web/handlers"
require "./actions/**"require "./actions/**" loads every action and its contract — you never require a contract by hand. Within a folder, files load in alphabetical order, so create_post.cr comes before create_post_contract.cr. That's fine: the contract is only used inside call, and Crystal checks method bodies after every file is loaded. Templates aren't required at all; render compiles them in.
main.cr loads the app and starts the server:
require "./app"
Blog::Store.open(ENV["DATABASE_URL"]) do |store|
Runcobo.start(handlers: [
Runcobo::LogHandler.new,
Runcobo::ErrorHandler.new,
Blog::RouteHandler.new(store),
Runcobo::RouteNotFoundHandler.new,
] of HTTP::Handler)
endKeep the two separate. Several tools load your routes and classes without starting a server:
| Tool | Loads |
|---|---|
cobo routes | src/app.cr, to print the route table |
cobo console, cobo runner | src/app.cr, before your code |
| Specs | src/app.cr, to dispatch requests in-process |
cobo dev | src/main.cr, to run the server |
If app.cr started the server, each of these would block on Runcobo.start.
Blog::BaseAction
Every action inherits from one abstract base class, named after the application. It's the place for what all actions share — filters, helpers, and access to dependencies:
abstract class Blog::BaseAction < ::BaseAction
before read_input # reject oversized or mistyped bodies before any work
protected def store : Blog::Store::Queries
context.blog_store
end
endstore comes from the request context, where Blog::RouteHandler puts it for every request:
class HTTP::Server::Context
property! blog_store : Blog::Store::Queries
end
module Blog
class RouteHandler < Runcobo::RouteHandler
def initialize(@store : Store::Queries)
end
protected def build_action(action_class : Runcobo::Action.class, context : HTTP::Server::Context) : Runcobo::Action
context.blog_store = @store
super
end
end
endDatabase Access explains Blog::Store, and Dependency injection why dependencies travel on the context.
Require order
Crystal needs a class defined before another class inherits from it. require "./actions/**" loads a folder's own files (alphabetically) before its subfolders, so actions/base_action.cr would come first anyway — app.cr requires it explicitly so the order is visible and doesn't depend on file names.
Ignored files
/bin/
/lib/
/.local/ # cobo's caches and SQLite databases
/db/schema.snapshot.sql # generated by cobo db snapshotCommit src/persistence/generated/ — it's generated, but it's part of your source.