Skip to content

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:

blog
src
app.crLoads the application
main.crOpens the store and starts the server
actions
base_action.crBlog::BaseAction, the base of every action
postsOne business area
create_post.crPosts::CreatePost
create_post_contract.crIts input
create_post.jbuilderIts response
show_post.cr
show_post_contract.cr
show_post.jbuilder
support.crCode shared by this area's actions
partials
post.jbuilderTemplates shared by this area's actions
authors
...
webThe request boundary
handlers.crPuts the store on each request
persistence
generated
queries.crWritten by cobo generate
sharedHelpers used across areas
integrationsClients for outside services
db
migrationsSQL migrations
queriesNamed SQL queries, one file per table
authors.sql
posts.sql
publicStatic files
spec
runcobo.ymlSettings for cobo
sqlc.yamlSettings for code generation
shard.yml

One endpoint, three files ​

An endpoint's files sit next to each other and share a name:

src/actions/posts/show_post_contract.cr
crystal
class Posts::ShowPostContract < RequestValidation::Contract
  schema do
    required :id, Int64
  end
end
src/actions/posts/show_post.cr
crystal
class 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
end
src/actions/posts/show_post.jbuilder
crystal
json.partial! "posts/partials/post", post: post
src/actions/posts/partials/post.jbuilder
crystal
# locals: post
json.id post.id
json.title post.title
json.status post.status
  • Names line up. Posts::ShowPost lives in posts/show_post.cr, its contract is Posts::ShowPostContract in show_post_contract.cr, and render finds show_post.jbuilder beside it. Inside Posts::ShowPost, the contract can be written as just ShowPostContract.
  • Input is read with validate. The first line of call validates the request against the action's contract and returns typed input — see Request Contracts.
  • Qualified class names. Write class Posts::ShowPost, not a module Posts wrapper 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 its partials/.

Changing an endpoint means opening one folder, and everything about it — route, input, logic, response — is in front of you.

Where everything else goes ​

FolderHolds
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:

src/app.cr
crystal
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:

src/main.cr
crystal
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)
end

Keep the two separate. Several tools load your routes and classes without starting a server:

ToolLoads
cobo routessrc/app.cr, to print the route table
cobo console, cobo runnersrc/app.cr, before your code
Specssrc/app.cr, to dispatch requests in-process
cobo devsrc/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:

src/actions/base_action.cr
crystal
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
end

store comes from the request context, where Blog::RouteHandler puts it for every request:

src/web/handlers.cr
crystal
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
end

Database 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 ​

.gitignore
text
/bin/
/lib/
/.local/                   # cobo's caches and SQLite databases
/db/schema.snapshot.sql    # generated by cobo db snapshot

Commit src/persistence/generated/ — it's generated, but it's part of your source.

Released under the MIT License.