chyabrung-cli 0.2.2

The `chy` command line tool for Chyabrung: scaffolding, dev server with hot reload, builds, generators, migrations and one-command deploys
# {{app_name}}


A JSON API built with [Chyabrung](https://github.com/Chyabrung/chyabrung) 🥁.

## Run it


```sh
chy dev          # http://127.0.0.1:2007, rebuilds and restarts on every save
                 # API docs (Swagger UI): http://127.0.0.1:2007/docs
cargo test       # run the tests in tests/
chy build        # optimized release build
```

## Where things go


```
{{app_name}}/
├── src/
│   ├── main.rs              starts the app (rarely edited)
│   ├── lib.rs               wires everything together: state, middleware, routes
│   ├── routes.rs            ★ every URL of the app: start here
│   ├── controllers/         handlers that answer requests, one file per feature
│   │   ├── auth_controller.rs     register, login, me
│   │   ├── post_controller.rs     #[controller("/posts")]: index, store, show, update, destroy

│   │   ├── stats_controller.rs
│   │   ├── upload_controller.rs
│   │   └── realtime_controller.rs Server-Sent Events and WebSocket
│   ├── models/              one struct per database table (User, Post)
│   ├── requests/            incoming data and its validation rules
│   └── middleware/          code that runs before/after handlers (empty at first)
├── migrations/              SQL that creates and changes tables
├── database/seeds/          sample data for development
├── public/                  static files, served at /assets/...
├── storage/public/          user uploads (created automatically, not in git)
├── tests/                   automated tests
├── .env                     secrets and local settings (not in git)
└── chyabrung.toml           app settings: name, port, database, deploy
```

Every folder explains itself: open its `mod.rs` or `README.md`.

## How a request flows


```
browser / client
   │  GET /api/posts/1
   ▼
routes.rs           finds the handler for this URL
   ▼
middleware          logging, CORS, auth... (src/lib.rs)
   ▼
requests/           checks the input (422 with messages if invalid)
   ▼
controllers/        PostController::show
   ▼
models/             Post::find(1)  →  database
   ▼
JSON response
```

## Adding a feature: comments


One command writes the whole feature and connects it:

```sh
chy make:resource Comment --fields "body:text,post_id:int,approved:bool"
chy migrate                            # create the table
cargo test                             # includes the new tests/comment.rs
```

It creates:

| File | What it is |
|---|---|
| `src/models/comment.rs` | the `Comment` model |
| `migrations/..._create_comments_table.*.sql` | the table |
| `src/requests/comment.rs` | `CommentRequest` with validation rules |
| `src/controllers/comment_controller.rs` | `#[controller("/comments")]` with index, store, show, update, destroy |
| `src/routes.rs` | `.mount(CommentController)` added inside `/api` |
| `tests/comment.rs` | a create/read/update/delete test |

Field types: `string`, `text`, `int`, `float`, `bool`; add `?` for an optional
column (`bio:text?`). Without `--fields` you get one `name` column. Then edit
the files as you like: for example add an `Auth(user): Auth` argument to a
handler to require a logged-in user.

Smaller generators exist for single pieces: `chy make:model Comment -m`,
`chy make:controller CommentController`, `chy make:migration add_bio_to_users`.

## API docs


`/docs` shows Swagger UI and `/openapi.json` the OpenAPI 3.1 spec, generated
from the routes when the app starts. For `#[controller]` and `#[get]`-style
routes, the handler's doc comment becomes the summary, and its arguments
document the request body (`Json<PostRequest>`), query (`Query<PageQuery>`),
path parameters, login (`Auth`) and validation (`422`). Types in `requests/`
and `models/` describe themselves; for other types add `#[derive(ToSchema)]`.

## Endpoints


| Method | URL | What it does |
|---|---|---|
| `POST` | `/api/auth/register` | create an account, returns a token |
| `POST` | `/api/auth/login` | log in, returns a token |
| `GET` | `/api/me` | the logged-in user (send `Authorization: Bearer <token>`) |
| `GET` | `/api/posts` | published posts (`?page=1&per_page=10`) |
| `POST` | `/api/posts` | create a post (logged in) |
| `GET` | `/api/posts/:id` | one post |
| `PUT` | `/api/posts/:id` | edit your post |
| `DELETE` | `/api/posts/:id` | delete your post |
| `GET` | `/api/stats` | counts, cached for a minute |
| `POST` | `/api/upload` | upload a file (logged in, multipart) |
| `GET` | `/api/events` | live events (Server-Sent Events) |
| `GET` | `/ws` | WebSocket echo |
| `GET` | `/docs` | API docs (Swagger UI), from `/openapi.json` |