knot-server 0.1.9

Distributed REST API server for knot codebase indexing. Manages Git repositories across a cluster with shared workspace coordination.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# OpenAPI / Swagger UI Integration via `utoipa`

## Goal

Add auto-generated, interactive API documentation to `knot-server` by integrating
the [`utoipa`](https://crates.io/crates/utoipa) ecosystem. After this change,
navigating to `/docs` in a browser will render a Swagger UI page where users can
browse every endpoint, inspect request/response schemas, and execute live "Try it
out" requests — the same experience as `springdoc-openapi` in Spring Boot.

Additionally, the raw OpenAPI 3.1 JSON spec will be available at
`/api-docs/openapi.json`, enabling Postman imports, client generation, and CI
contract testing.

---

## Dependencies

Add the following crates to `Cargo.toml`:

```toml
utoipa = { version = "5", features = ["axum_extras"] }
utoipa-axum = "0.2"
utoipa-swagger-ui = { version = "9", features = ["axum"] }
```

| Crate | Purpose |
|-------|---------|
| `utoipa` | Proc macros (`#[utoipa::path]`, `#[derive(ToSchema)]`) for generating the OpenAPI spec at compile time |
| `utoipa-axum` | `OpenApiRouter` — collects routes and builds the spec automatically |
| `utoipa-swagger-ui` | Embeds the Swagger UI static assets and serves them as an Axum router |

> **Note:** `utoipa-swagger-ui` embeds the Swagger UI assets at compile time via
> `rust-embed`. In debug builds, it reads them from disk (via `debug-embed`
> feature, which is enabled by default in v9). No external CDN or download needed.

---

## Schemas — `#[derive(ToSchema)]`

Every struct/enum that appears in a request body, response body, or query parameter
needs `#[derive(utoipa::ToSchema)]`.

### File: `src/models.rs`

| Struct/Enum | Role | Notes |
|-------------|------|-------|
| `AuthType` | Enum (`ssh`, `https`) | Add `#[derive(ToSchema)]` |
| `RepoStatus` | Enum (`idle`, `cloning`, `pulling`, `indexing`, `error`) | Add `#[derive(ToSchema)]` |
| `RepoEntry` | Response body for `GET /api/repos/:id` and items in list | Add `#[derive(ToSchema)]`. Mark `webhook_secret` with `#[schema(write_only)]` so it doesn't leak in docs. |
| `RegisterRepoRequest` | Request body for `POST /api/repos` | Add `#[derive(ToSchema)]`. Add `#[schema(example = ...)]` for `url` and `branch`. |
| `RegisterRepoResponse` | Response body for `POST /api/repos` | Add `#[derive(ToSchema)]` |
| `RepoListResponse` | Response body for `GET /api/repos` | Add `#[derive(ToSchema)]` |

#### Example diff for `AuthType`:

```diff
+use utoipa::ToSchema;
+
-#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
 #[serde(rename_all = "lowercase")]
 pub enum AuthType {
     Ssh,
     Https,
 }
```

#### Example diff for `RegisterRepoRequest`:

```diff
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Deserialize, ToSchema)]
 pub struct RegisterRepoRequest {
+    /// Git repository URL (HTTPS, SSH, or local absolute path)
+    #[schema(example = "https://github.com/raultov/knot.git")]
     pub url: String,
+    /// Authentication method
     #[serde(default = "default_auth_type")]
     pub auth_type: AuthType,
+    /// Branch to clone
+    #[schema(example = "main")]
     #[serde(default = "default_branch")]
     pub branch: String,
+    /// Shared secret for webhook signature validation (HMAC-SHA256)
     #[serde(skip_serializing_if = "Option::is_none")]
     pub webhook_secret: Option<String>,
 }
```

### File: `src/handlers.rs`

| Struct | Role | Notes |
|--------|------|-------|
| `ErrorResponse` | Standard error body `{ "error": "..." }` | Add `#[derive(ToSchema)]` |
| `SearchParams` | Query params for `/search` | Add `#[derive(ToSchema, IntoParams)]` |
| `CallersParams` | Query params for `/callers` | Add `#[derive(ToSchema, IntoParams)]` |
| `ExploreParams` | Query params for `/explore` | Add `#[derive(ToSchema, IntoParams)]` |
| `DepsParams` | Query params for `/deps` | Add `#[derive(ToSchema, IntoParams)]` |
| `GraphParams` | Query params for `/graph` | Add `#[derive(ToSchema, IntoParams)]` |
| `GraphExpandParams` | Query params for `/graph/expand` | Add `#[derive(ToSchema, IntoParams)]` |
| `GraphNodeResponse` | Part of graph JSON response | Add `#[derive(ToSchema)]` |
| `GraphEdgeResponse` | Part of graph JSON response | Add `#[derive(ToSchema)]` |
| `GraphResponse` | Response body for `/graph` | Add `#[derive(ToSchema)]` |

#### Query parameter structs — `IntoParams`

For query-parameter structs, use `#[derive(IntoParams)]` in addition to
`ToSchema`. This generates the OpenAPI `parameters` array automatically:

```diff
+use utoipa::{ToSchema, IntoParams};
+
-#[derive(Debug, Deserialize)]
+#[derive(Debug, Deserialize, IntoParams)]
 pub struct SearchParams {
+    /// The search query string
+    #[param(example = "authentication logic")]
     pub q: Option<String>,
+    /// Maximum number of results to return
+    #[param(example = 5)]
     pub max_results: Option<usize>,
 }
```

---

## Handler Annotations — `#[utoipa::path]`

Every public handler function gets a `#[utoipa::path(...)]` attribute. This is
the core of the OpenAPI spec generation.

### Complete handler annotation table

| # | Handler | Method | Path | Tag | Request Body | Responses |
|---|---------|--------|------|-----|--------------|-----------|
| 1 | `list_repos_handler` | GET | `/api/repos` | Repositories || 200→`RepoListResponse` |
| 2 | `register_repo_handler` | POST | `/api/repos` | Repositories | `RegisterRepoRequest` | 202→`RegisterRepoResponse`, 409→`ErrorResponse`, 429→`ErrorResponse` |
| 3 | `get_repo_handler` | GET | `/api/repos/{id}` | Repositories || 200→`RepoEntry`, 404→`ErrorResponse` |
| 4 | `delete_repo_handler` | DELETE | `/api/repos/{id}` | Repositories || 200→`json`, 404→`ErrorResponse` |
| 5 | `sync_repo_handler` | POST | `/api/repos/{id}/sync` | Indexing || 202→`json`, 404→`ErrorResponse`, 429→`ErrorResponse` |
| 6 | `search_handler` | GET | `/api/repos/{id}/search` | Search | — (query: `SearchParams`) | 200→`json`, 400→`ErrorResponse` |
| 7 | `callers_handler` | GET | `/api/repos/{id}/callers` | Search | — (query: `CallersParams`) | 200→`json`, 400→`ErrorResponse` |
| 8 | `explore_handler` | GET | `/api/repos/{id}/explore` | Search | — (query: `ExploreParams`) | 200→`json`, 400→`ErrorResponse`, 404→`ErrorResponse` |
| 9 | `deps_handler` | GET | `/api/repos/{id}/deps` | Search | — (query: `DepsParams`) | 200→`json`, 400→`ErrorResponse` |
| 10 | `graph_handler` | GET | `/api/repos/{id}/graph` | Graph | — (query: `GraphParams`) | 200→`GraphResponse`, 400→`ErrorResponse`, 404→`ErrorResponse` |
| 11 | `graph_expand_handler` | GET | `/api/repos/{id}/graph/expand` | Graph | — (query: `GraphExpandParams`) | 200→`GraphResponse`, 400→`ErrorResponse`, 404→`ErrorResponse` |
| 12 | `webhook_handler` | POST | `/api/webhook/{id}` | Webhooks | raw bytes | 202→`json`, 401→`ErrorResponse`, 404→`ErrorResponse` |
| 13 | `health_handler` | GET | `/api/health` | Health || 200→`json` |

### Example annotation for `search_handler`:

```rust
#[utoipa::path(
    get,
    path = "/api/repos/{id}/search",
    tag = "Search",
    params(
        ("id" = String, Path, description = "Repository ID"),
        SearchParams,
    ),
    responses(
        (status = 200, description = "Search results", body = serde_json::Value),
        (status = 400, description = "Missing or invalid query parameter", body = ErrorResponse),
        (status = 500, description = "Internal server error", body = ErrorResponse),
    ),
    description = "Semantic + structural search. Find code by meaning, class name, method signature, or docstrings.",
)]
pub async fn search_handler(...) -> Response { ... }
```

### Example annotation for `register_repo_handler`:

```rust
#[utoipa::path(
    post,
    path = "/api/repos",
    tag = "Repositories",
    request_body = RegisterRepoRequest,
    responses(
        (status = 202, description = "Repository registered and clone job enqueued", body = RegisterRepoResponse),
        (status = 409, description = "Repository already exists", body = ErrorResponse),
        (status = 429, description = "Indexing queue is full", body = ErrorResponse),
    ),
    description = "Register a new Git repository. The server clones it and queues it for indexing.",
)]
pub async fn register_repo_handler(...) -> Response { ... }
```

### Example annotation for `webhook_handler`:

```rust
#[utoipa::path(
    post,
    path = "/api/webhook/{id}",
    tag = "Webhooks",
    params(
        ("id" = String, Path, description = "Repository ID"),
    ),
    responses(
        (status = 202, description = "Webhook accepted, indexing job enqueued"),
        (status = 401, description = "Invalid or missing webhook signature", body = ErrorResponse),
        (status = 404, description = "Repository not found", body = ErrorResponse),
    ),
    description = "Endpoint for Git provider webhooks (GitHub, GitLab, Bitbucket). Validates payload signatures and triggers incremental re-indexing on push events.",
)]
pub async fn webhook_handler(...) -> Response { ... }
```

### Example annotation for `health_handler`:

```rust
#[utoipa::path(
    get,
    path = "/api/health",
    tag = "Health",
    responses(
        (status = 200, description = "Server health status"),
    ),
    description = "Check server health, uptime, queue capacity, and repository statistics.",
)]
pub async fn health_handler(...) -> Response { ... }
```

---

## Router Integration — `OpenApiRouter`

### File: `src/main.rs`

Replace the current `Router::new()` chain with `OpenApiRouter` from `utoipa-axum`,
which automatically collects the `#[utoipa::path]` metadata from each handler.

#### Current code (lines 147–170):

```rust
let app = Router::new()
    .route("/api/repos", get(handlers::list_repos_handler).post(handlers::register_repo_handler))
    .route("/api/repos/{id}", get(handlers::get_repo_handler).delete(handlers::delete_repo_handler))
    // ... 10 more routes ...
    .with_state(state);
```

#### Proposed code:

```rust
use utoipa::OpenApi;
use utoipa_axum::router::OpenApiRouter;
use utoipa_axum::routes;
use utoipa_swagger_ui::SwaggerUi;

#[derive(OpenApi)]
#[openapi(
    info(
        title = "knot-server",
        version = env!("CARGO_PKG_VERSION"),
        description = "REST API for managing and indexing Git repositories. Provides semantic search, caller analysis, file exploration, dependency graphs, and interactive 3D visualization.",
        license(name = "MIT", url = "https://github.com/raultov/knot-server/blob/master/LICENSE"),
    ),
    tags(
        (name = "Repositories", description = "Register, list, inspect, and delete Git repositories"),
        (name = "Search", description = "Semantic search, caller analysis, file exploration, and dependency lookup"),
        (name = "Graph", description = "Entity relationship subgraph queries"),
        (name = "Indexing", description = "Trigger manual sync and re-indexing"),
        (name = "Webhooks", description = "Git provider webhook endpoints (GitHub, GitLab, Bitbucket)"),
        (name = "Health", description = "Server health and statistics"),
    ),
)]
struct ApiDoc;

// Build API routes with automatic OpenAPI spec collection
let (api_router, api) = OpenApiRouter::with_openapi(ApiDoc::openapi())
    .routes(routes!(handlers::list_repos_handler, handlers::register_repo_handler))
    .routes(routes!(handlers::get_repo_handler, handlers::delete_repo_handler))
    .routes(routes!(handlers::sync_repo_handler))
    .routes(routes!(handlers::search_handler))
    .routes(routes!(handlers::callers_handler))
    .routes(routes!(handlers::explore_handler))
    .routes(routes!(handlers::deps_handler))
    .routes(routes!(handlers::graph_handler))
    .routes(routes!(handlers::graph_expand_handler))
    .routes(routes!(handlers::webhook_handler))
    .routes(routes!(handlers::health_handler))
    .split_for_parts();

// Merge the generated API routes with non-API routes and Swagger UI
let app = api_router
    .route("/favicon.ico", get(handlers::favicon_handler))
    .route("/graph", get(handlers::graph_viewer_handler))
    .merge(SwaggerUi::new("/docs").url("/api-docs/openapi.json", api))
    .with_state(state);
```

#### Key points:
- The `routes!()` macro reads the `#[utoipa::path]` annotations from each handler
  and registers both the Axum route and the OpenAPI path entry.
- Handlers that share the same path (e.g., `list_repos_handler` + `register_repo_handler`
  on `/api/repos`) must go in the **same** `routes!()` call.
- Non-API routes (`/favicon.ico`, `/graph`) stay as regular `.route()` calls —
  they don't need OpenAPI documentation.
- `SwaggerUi::new("/docs")` serves the interactive UI at `http://localhost:3000/docs`.
- The raw spec is served at `http://localhost:3000/api-docs/openapi.json`.

---

## Test Router

### File: `src/handlers.rs``build_test_app()` (line ~1182)

The test helper `build_test_app()` constructs a `Router` without Swagger UI. This
function does **not** need `OpenApiRouter` — it can stay as plain `Router::new()`.
The `#[utoipa::path]` attributes on the handlers are inert when used with a plain
`Router`, so no test changes are needed.

---

## Graph Viewer — Discreet Link to API Docs

### File: `assets/graph-viewer.html`

Add a subtle link to `/docs` in the **status bar** at the bottom of the graph
viewer. The status bar (line ~263) is the thin strip at the very bottom of the
screen — low-profile, not competing with the graph for attention.

#### Current HTML (line ~263):

```html
<div id="status-bar">Ready</div>
```

#### Proposed HTML:

```html
<div id="status-bar">
  <span id="status-text">Ready</span>
  <a href="/docs" target="_blank" style="float: right; color: #555; text-decoration: none; font-size: 10px;" title="Open API documentation (Swagger UI)">API Docs ↗</a>
</div>
```

The link floats to the far right of the status bar, uses a muted `#555` color
and `10px` font to stay out of the way. On hover, it should lighten slightly.
Add this CSS rule inside the existing `<style>` block:

```css
#status-bar a:hover { color: #aaa; }
```

The `setStatus()` function (line ~557) needs a one-line update to target the
inner `<span>` instead of the whole `<div>`:

```diff
 function setStatus(msg) {
-  document.getElementById('status-bar').textContent = msg;
+  document.getElementById('status-text').textContent = msg;
 }
```

---

## Files Changed — Summary

| File | Changes |
|------|---------|
| `Cargo.toml` | Add `utoipa`, `utoipa-axum`, `utoipa-swagger-ui` dependencies |
| `src/models.rs` | Add `#[derive(ToSchema)]` to `AuthType`, `RepoStatus`, `RepoEntry`, `RegisterRepoRequest`, `RegisterRepoResponse`, `RepoListResponse` (6 types) |
| `src/handlers.rs` | Add `#[derive(ToSchema)]` to `ErrorResponse`, `GraphNodeResponse`, `GraphEdgeResponse`, `GraphResponse` (4 types). Add `#[derive(IntoParams)]` to `SearchParams`, `CallersParams`, `ExploreParams`, `DepsParams`, `GraphParams`, `GraphExpandParams` (6 types). Add `#[utoipa::path(...)]` to 13 handler functions. |
| `src/main.rs` | Replace `Router::new()` with `OpenApiRouter`, add `SwaggerUi` merge, add `ApiDoc` struct |
| `assets/graph-viewer.html` | Add discreet "API Docs ↗" link in the status bar, update `setStatus()` to target inner `<span>` |
| `README.md` | Document `/docs` endpoint in the API section |

No new source files are created. No existing tests should break.

---

## Binary Size Impact

`utoipa-swagger-ui` embeds the Swagger UI static assets (~3.5 MB gzipped) into
the binary via `rust-embed`. This increases the release binary size by
approximately **3–4 MB**. This is acceptable for a server binary.

If binary size becomes a concern in the future, the Swagger UI can be served from
a CDN instead by using the `url` feature of `utoipa-swagger-ui` (not recommended
for self-hosted/offline deployments).

---

## Verification Plan

### Automated
1. `cargo build` — confirms all macros expand correctly.
2. `cargo test` — confirms existing tests still pass (no handler signatures changed).
3. `cargo clippy` — no new warnings.

### Manual
1. Start the server: `cargo run`
2. Navigate to `http://localhost:3000/docs` — Swagger UI should render with all 13 endpoints.
3. Verify tags: endpoints should be grouped under "Repositories", "Search", "Graph", "Indexing", "Webhooks", "Health".
4. Click "Try it out" on `GET /api/health` — should return 200 with JSON body.
5. Click "Try it out" on `POST /api/repos` — fill in request body, verify 202 response.
6. Fetch `http://localhost:3000/api-docs/openapi.json` — verify valid OpenAPI 3.1 JSON.
7. Import `openapi.json` into Postman — verify all endpoints appear.
8. Navigate to `http://localhost:3000/graph` — verify the "API Docs ↗" link appears at the bottom-right of the status bar and opens `/docs` in a new tab.

### Docker
1. `docker compose -f docker-compose.yml -f docker-compose.dev.yml up --build`
2. Navigate to `http://localhost:3000/docs` — verify Swagger UI works in the container.

---

## Migration Path for the Postman Collection

After this feature is implemented:

1. The Postman collection (`knot-server.postman_collection.json`) can still be kept
   in the repo as a convenience for users who prefer Postman.
2. The `openapi.json` served by the running server becomes the **canonical** API
   contract. Users can import it directly into Postman, Insomnia, or any OpenAPI
   tool.
3. Long-term, the Postman collection can be deprecated in favor of the OpenAPI spec.