axtra 0.2.3

Axtra is a Rust library for building web applications with Axum and Astro providing utilities for error handling, notifications, and more.
Documentation
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# Axtra

###

🎨 Overview
===========

Opinionated helpers for Axum + Astro projects.

> **Warning:** This library is experimental, opinionated, and subject to breaking changes.  
> 🐉 Here be dragons 🐉

---

## Features

### Error Handling

- **AppError**
  - `AppError`: One error type for all your Axum APIs.
  - Error variants for authentication, authorization, validation, database, not found, and more.
  - Automatic handling of thrown WithRejection<Json<Struct>> and Validation using [`validator`]https://github.com/Keats/validator

- **Error Macros**
  - `app_error!` macro: Ergonomic error construction for all variants.
  - Captures error location automatically.
  - Supports underlying error mapping for easy use with `.map_err()`.

- **TypeScript Type Generation**
  - Rust error types (`ErrorCode`, validation errors, etc.) exported to TypeScript via [`ts-rs`]https://github.com/Aleph-Alpha/ts-rs.
  - Ensures your frontend and backend share error contracts.

- **Error Notifications**
  - Send critical errors to a variety of services/notifiers
    - Sentry integration (optional)
    - Slack integration (optional)
    - Discord integration (optional)
  
### Api Responses

- **Wrapped JSON Responses**
  - `WrappedJson<T>`: Automatically wraps responses with a key derived from the type name.
  - `ResponseKey` derive macro: Customize or auto-generate response keys for your types.
  - List responses are pluralized automatically.

### Axum Helpers

- **Health Check Endpoint**
  - Built-in Axum route for checking Postgres connectivity.
  - Returns status, DB health, and timestamp.

- **Static File Serving**
  - Helpers for serving SPA and static files with Axum.

### Bouncer

- **IP Banning and Malicious Path Filtering**
  - Simple IP banning and malicious path filtering middleware for Axum.

### Notifications
- **Notification Integration**
  - Slack and Discord error notifications

---

## ErrorHandling

### AppError

AppError is an enum type containing types for handling BadRequest, NotFound, Authorization, Authentication, Database and Exception errors.

AppErrors will be logged automatically with the following severity

| Error Type        | Severity (Log Level) |
|-------------------|---------------------|
| Authentication    | INFO                |
| Authorization     | INFO                |
| BadRequest        | WARN                |
| NotFound          | WARN                |
| Validation        | WARN                |
| Database          | ERROR               |
| Exception         | ERROR               |

- **INFO**: Expected authentication/authorization failures.
- **WARN**: Client errors, invalid input, not found, validation issues.
- **ERROR**: Server-side failures, database errors, exceptions, and triggers notifications (Slack/Discord/Sentry if enabled).

### WithRejection

AppError will automatically handle malformed JSON and render BadRequest errors when using WithRejection.

```rust
pub async fn create(
    auth_session: AuthSession,
    State(pool): State<PgPool>,
    WithRejection(Json(payload), _): WithRejection<Json<NewSubscription>, AppError>,
) -> Result<WrappedJson<CheckoutSession>, AppError>
```

### Validator

When using .validate()? in a handler, AppError will automatically catch and render the ValidationErrors.

```rust
#[derive(Debug, Validate, Deserialize)]
struct SignupData {
    #[validate(email)]
    mail: String,
}

handler() -> Result<impl IntoResponse, AppError> {
  let payload signup_data = SignupData { mail: "notemail" };
  payload.validate()?;
}
```

```json
{
    "status": "Bad Request",
    "message": "There was a validation error with your request.",
    "error": "Validation error",
    "code": "validation",
    "validationErrors": {
        "errors": [
            {
                "field": "mail",
                "code": "email",
                "message": "mail must be a valid email",
            }
        ]
    }
}
```

### Error Macro Usage

The `app_error!` macro makes error construction ergonomic and consistent.  
It automatically tracks error location and supports both direct and closure-based usage.

If no response type is passed, defaults to HTML.

### Standalone (Direct) Usage

```rust
// Basic error (HTML response)
Err(app_error!(bad_request, "Missing field"));

// JSON error
Err(app_error!(bad_request, json, "Invalid JSON payload"));

// HTML error
Err(app_error!(bad_request, html, "Form error"));

// Not found resource: &str
Err(app_error!(not_found, "User not found"));
Err(app_error!(not_found, json, "User not found"));

// Unauthorized resource: &str, action: &str
Err(app_error!(unauthorized, "users", "delete"));
Err(app_error!(unauthorized, json, "users", "delete"));

// Unauthenticated
Err(app_error!(unauthenticated));
Err(app_error!(unauthenticated, json));

// Validation error
Err(app_error!(validation, errors));
Err(app_error!(validation, json, errors));

// Thrown exceptons
Err(app_error!(throw, "You broke something"))
Err(app_error!(throw, json, "You broke something and we're responding with json"))
Err(app_error!(throw, html, "You broke something and we're responding with html"))
```

### Closure (For `.map_err()` and error mapping)

```rust
// Bad request with underlying error
let value: i32 = input.parse().map_err(app_error!(bad_request, with_error, "Invalid number"))?;

// With format args
let value: i32 = input.parse().map_err(app_error!(bad_request, with_error, "Invalid number: {}", input))?;

// JSON error with underlying error
let user: User = serde_json::from_str(&body).map_err(app_error!(bad_request, json, with_error, "Bad JSON: {}", body))?;

// Database error mapping
let user = sqlx::query!("SELECT * FROM users WHERE id = $1", id)
    .fetch_one(&pool)
    .await
    .map_err(app_error!(db, "Failed to fetch user"))?;

// Exception mapping
let result = do_something().map_err(app_error!(exception, "Unexpected error"))?;
```

### Typescript types

Axtra provides Ts-Rs bindings to output typed ErrorResponses.

To enable, add the export to your build.rs

```rust
use axtra::errors::ErrorResponse;
use std::fs;
use std::path::Path;
use ts_rs::TS;

fn main() {
    // Specify the path to the directory containing the TypeScript files
    let ts_dir = Path::new("types");
    fs::create_dir_all(ts_dir).unwrap();

    ErrorResponse::export_all_to(ts_dir).unwrap();
}
```

```typescript
/**
 * Enum of all possible error codes.
 */
export type ErrorCode = "authentication" | "authorization" | "badRequest" | "database" | "exception" | "notFound" | "validation";

export type ErrorResponse = { status: string, message: string, code: ErrorCode, validationErrors?: SerializableValidationErrors, };

/**
 * Represents all validation errors in a serializable form.
 */
export type SerializableValidationErrors = { errors: Array<ValidationFieldError>, };

/**
 * Represents a single field validation error.
 */
export type ValidationFieldError = { field: string, code: string, message: string, params: { [key in string]?: string }, };
```

### Error Notification Feature Flags

Axtra supports sending critical errors to external services for alerting and monitoring.  
Enable these features in your `Cargo.toml` as needed:

#### `sentry`

- **Purpose:**  
  Automatically sends `Database` and `Exception` errors to [Sentry](https://sentry.io/) for error tracking.
- **How to use:**  
  Enable the feature:
  ```
  toml
  features = ["sentry"]
  ```
  Configure Sentry in your app (see [sentry docs](https://docs.rs/sentry)).
- **Effect:**  
  When enabled, critical errors are reported to Sentry in addition to being logged.

#### `notify-error-slack`

- **Purpose:**  
  Sends critical errors (database, exception, throw) to a Slack channel via webhook.
- **How to use:**  
  Enable the feature:
  ```
  toml
  features = ["notify-error-slack"]
  ```
  Set your webhook URL:
  ```
  SLACK_ERROR_WEBHOOK_URL=your_webhook_url
  ```
- **Effect:**  
  When enabled, errors are posted to Slack using the configured webhook.

#### `notify-error-discord`

- **Purpose:**  
  Sends critical errors (database, exception, throw) to a Discord channel via webhook.
- **How to use:**  
  Enable the feature:
  ```
  toml
  features = ["notify-error-discord"]
  ```
  Set your webhook URL:
  ```
  DISCORD_ERROR_WEBHOOK_URL=your_webhook_url
  ```
- **Effect:**  
  When enabled, errors are posted to Discord using the configured webhook.

---

**Note:**  
All notification features are opt-in and only send alerts for server-side errors (`Database`, `Exception`, or `throw`).  
You can enable any combination of these features as needed for your project.

---

## Api Responses

### WrappedJson & ResponseKey

Axtra provides a convenient way to wrap API responses with a predictable key, using the `WrappedJson<T>` type and the `ResponseKey` derive macro.

#### Usage

```rust
use axtra::response::{WrappedJson, ResponseKey};
use serde::Serialize;

#[derive(Serialize, ResponseKey)]
struct User {
    id: i32,
    name: String,
}

// In your handler:
async fn get_user() -> Result<WrappedJson<User>, AppError> {
    let user = User { id: 1, name: "Alice".to_string() };
    Ok(WrappedJson(user))
}
```

**Produces JSON:**
```json
{
  "user": {
    "id": 1,
    "name": "Alice"
  }
}
```

#### Customizing the Response Key

You can override the default key by using the `#[response_key = "custom_name"]` attribute:

```rust
#[derive(Serialize, ResponseKey)]
#[response_key = "account"]
struct UserAccount {
    id: i32,
    email: String,
}
```

**Produces JSON:**
```json
{
  "account": {
    "id": 1,
    "email": "alice@example.com"
  }
}
```

#### List Responses

When returning a list, the key is automatically pluralized:

```rust
#[derive(Serialize, ResponseKey)]
struct User {
    id: i32,
    name: String,
}

async fn list_users() -> Result<WrappedJson<Vec<User>>, AppError> {
    let users = vec![
        User { id: 1, name: "Alice".to_string() },
        User { id: 2, name: "Bob".to_string() },
    ];
    Ok(WrappedJson(users))
}
```

**Produces JSON:**
```json
{
  "users": [
    { "id": 1, "name": "Alice" },
    { "id": 2, "name": "Bob" }
  ]
}
```

#### Macro Implementation

```rust
// #[derive(ResponseKey)] will auto-implement this trait:
pub trait ResponseKey {
    fn response_key() -> &'static str;
}
```

See [`axtra_macros::ResponseKey`](./axtra_macros/src/response_key.rs) for details.

---

## Axum Helpers

### Health Check Endpoint

Axtra provides a ready-to-use health check route for monitoring your application's status and database connectivity.

#### Usage

```rust
use axtra::routes::health::check_health;
use axum::{routing::get, Router};
use sqlx::PgPool;

fn app(pool: PgPool) -> Router {
    Router::new()
        .route("/health", get(check_health))
        .with_state(pool)
}
```

**Response (healthy):**
```json
{
  "status": "healthy",
  "postgres": true,
  "timestamp": "2025-07-15T12:34:56Z"
}
```

**Response (degraded):**
- Returns HTTP 503 Service Unavailable if the database is not reachable.

---

### Static File & Single Page App (SPA) Routes

Axtra includes helpers for serving static files and SPAs (such as Astro or React) with Axum.

#### Serve a Single Page App (SPA)

```rust
use axtra::routes::astro::serve_spa;
use axum::Router;

// Serves files from ./dist/myapp/index.html for /myapp and /myapp/*
let router = Router::new().merge(serve_spa("myapp"));
```

#### Serve Static Files

```rust
use axtra::routes::astro::serve_static_files;
use axum::Router;

// Serves files from ./dist, with compression and custom cache headers
let router = Router::new().merge(serve_static_files());
```

- Requests to `/` and other paths will serve files from the `./dist` directory.
- Requests for missing files will return `404.html` from the same directory.
- Cache headers are set for `_static` and `_astro` assets for optimal performance.

---

**See [`routes/health.rs`](./axtra/src/routes/health.rs) and [`routes/astro.rs`](./axtra/src/routes/astro.rs) for full implementation details.**

---

## Bouncer: IP Banning & Malicious Path Filtering

Axtra's `bouncer` middleware automatically bans IP addresses that hit known malicious or unwanted paths, helping protect your Axum app from common scanner and exploit traffic.
Enable the `bouncer` feature in your `Cargo.toml` to access the Notifier API:

### Features

- Ban IPs for a configurable duration when they access blocked paths.
- Use presets (`"wordpress"`, `"php"`, `"config"`) or custom paths for filtering.
- Customize HTTP status for banned and blocked responses.
- Set log level for event tracing (`trace`, `debug`, `info`, etc).
- Expose the banlist for observability and monitoring.

### Usage Example

```rust
use axtra::bouncer::{BouncerConfig, BouncerLayer};
use axum::{Router, routing::get};
use axum::http::StatusCode;
use tracing::Level;
use std::time::Duration;

// Create a config with presets and custom paths, and customize responses/logging
let config = BouncerConfig::from_rules(
    &["wordpress", "config"],
    &["/custom"]
)
    .duration(Duration::from_secs(1800))
    .banned_response(StatusCode::UNAUTHORIZED)
    .blocked_response(StatusCode::NOT_FOUND)
    .log_level(Level::INFO);

let layer = BouncerLayer::new(config);

let app = Router::new()
    .route("/", get(|| async { "Hello" }))
    .layer(layer);
```

### Extracting the IP Address
To ensure that we have access to the clients IP Address you must start axum with, if you do not IP address will always be None and bouncer will not work.

```rust
axum::serve(listener, app.into_make_service_with_connect_info::<SocketAddr>())
```

instead of the usual

```rust
axum::serve(listener, app.into_make_service())
```


### Trusted Proxy
Since proxy headers can be spoofed you must opt-in to allow proxy headers for IP addresses by specifically setting trust_proxy to true.

If trust_proxy is true we will look for in descending order
- CF-conecting-ip (Cloudflare)
- X-Forwarded-For
- X-Real-IP

If no proxy headers are set or trust_proxy is false we will fallback to the connection IP address.

### Presets

Available presets for common hacker/scanner paths:
- `"wordpress"`
- `"php"`
- `"config"`

### Advanced Usage

You can also pass only presets or only custom paths:

```rust
let config = BouncerConfig::from_preset_rules(&["wordpress"]);
let config = BouncerConfig::from_custom_rules(&["/admin", "/hidden"]);
```

### Tracing & TraceLayer Integration

The bouncer middleware uses [`tracing`](https://docs.rs/tracing) to log blocked and banned events.  
You can configure the log level via `.log_level(Level::DEBUG)` or similar.

**Best Practice:**  
Place `BouncerLayer` **before** Axum's `TraceLayer` so that blocked/banned requests are logged by bouncer and not missed by TraceLayer's `on_response` hooks.

#### Example with TraceLayer

```rust
use axtra::bouncer::{BouncerConfig, BouncerLayer};
use axum::{Router, routing::get};
use tower_http::trace::TraceLayer;

let config = BouncerConfig::from_rules(&["wordpress"], &[])
    .log_level(tracing::Level::INFO);

let app = Router::new()
    .route("/", get(|| async { "Hello" }))
    .layer(TraceLayer::new_for_http())
    .layer(BouncerLayer::new(config));
```

**Logging:**  
- Bouncer logs blocked/banned events at your chosen level.
- TraceLayer logs all requests that reach your handlers.
- Blocked/banned requests are handled and logged by bouncer, and do **not** reach TraceLayer's `on_response`.

---

**See [`bouncer/mod.rs`](./axtra/src/bouncer/mod.rs) and [`bouncer/layer.rs`](./axtra/src/bouncer/layer.rs) for full implementation details.**

## Notifier

Axtra includes a flexible notification system for sending error alerts to Slack and Discord.  
Enable the `notifier` feature in your `Cargo.toml` to access the Notifier API:

```toml
[features]
notifier = []
```

You can then use the Notifier struct to send messages to Slack and Discord webhooks.

### Notifier API

```rust
use axtra::notifier::Notifier;
use serde_json::json;

// Create a notifier for Slack
let slack = Notifier::with_slack("https://hooks.slack.com/services/XXX");

// Send a simple Slack message
slack.notify_slack("Hello from Axtra!").await?;

// Send a rich Slack message (blocks)
let blocks = json!([{ "type": "section", "text": { "type": "plain_text", "text": "Error occurred!" } }]);
slack.notify_slack_rich(blocks).await?;

// Create a notifier for Discord
let discord = Notifier::with_discord("https://discord.com/api/webhooks/XXX");

// Send a simple Discord message
discord.notify_discord("Hello from Axtra!").await?;

// Send a rich Discord message (embeds)
let embeds = json!([{ "title": "Error", "description": "Something went wrong!" }]);
discord.notify_discord_rich(embeds).await?;
```

You can also use static methods for one-off notifications:

```rust
use axtra::notifier::Notifier;
use serde_json::json;

// Send a one-off Slack message
Notifier::slack("https://hooks.slack.com/services/XXX", "Hello!").await?;

// Send a one-off rich Slack message (blocks)
let blocks = json!([
    { "type": "section", "text": { "type": "plain_text", "text": "Critical error occurred!" } }
]);
Notifier::slack_rich("https://hooks.slack.com/services/XXX", blocks).await?;

// Send a one-off Discord message
Notifier::discord("https://discord.com/api/webhooks/XXX", "Hello!").await?;

// Send a one-off rich Discord message (embeds)
let embeds = json!([
    { "title": "Error", "description": "Something went wrong!", "color": 16711680 }
]);
Notifier::discord_rich("https://discord.com/api/webhooks/XXX", embeds).await?;
```

**See [`notifier/mod.rs`](./axtra/src/notifier/mod.rs) for full API details.**

---

## Documentation

- [API Docs (docs.rs)]https://docs.rs/axtra
- [Changelog]./CHANGELOG.md

---

## License

MIT

---

## Contributing

PRs and issues welcome! See [CONTRIBUTING.md](./CONTRIBUTING.md).