---
url: /runcobo/guide/project-structure.md
description: >-
  How a Runcobo application is organized — business areas with actions,
  contracts and templates side by side.
---

# 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`](/cli/#cobo-init) creates this layout for a new project. The examples throughout this guide come from this blog application:

```tree
blog/
  src/
    app.cr                       # Loads the application
    main.cr                      # Opens the store and starts the server
    actions/
      base_action.cr             # Blog::BaseAction, the base of every action
      posts/                     # One business area
        create_post.cr           # Posts::CreatePost
        create_post_contract.cr  # Its input
        create_post.jbuilder     # Its response
        show_post.cr
        show_post_contract.cr
        show_post.jbuilder
        support.cr               # Code shared by this area's actions
        partials/
          post.jbuilder          # Templates shared by this area's actions
      authors/
        ...
    web/                         # The request boundary
      handlers.cr                # Puts the store on each request
    persistence/
      generated/
        queries.cr               # Written by cobo generate
    shared/                      # Helpers used across areas
    integrations/                # Clients for outside services
  db/
    migrations/                  # SQL migrations
    queries/                     # Named SQL queries, one file per table
      authors.sql
      posts.sql
  public/                        # Static files
  spec/
  runcobo.yml                    # Settings for cobo
  sqlc.yaml                      # Settings for code generation
  shard.yml
```

## One endpoint, three files

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

```crystal [src/actions/posts/show_post_contract.cr]
class Posts::ShowPostContract < RequestValidation::Contract
  schema do
    required :id, Int64
  end
end
```

```crystal [src/actions/posts/show_post.cr]
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
```

```crystal [src/actions/posts/show_post.jbuilder]
json.partial! "posts/partials/post", post: post
```

```crystal [src/actions/posts/partials/post.jbuilder]
# 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](/guide/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

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

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

```crystal [src/main.cr]
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:

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

```crystal [src/actions/base_action.cr]
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:

```crystal [src/web/handlers.cr]
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](/guide/database#_4-give-actions-a-store) explains `Blog::Store`, and [Dependency injection](/guide/server#dependency-injection) why dependencies travel on the context.

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

```text [.gitignore]
/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.
