atrg-core 0.1.1

Core framework: AppState, config, app builder for at-rust-go
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
664
665
666
667
668
669
670
671
672
673
674
675
//! Configuration types and loader for `atrg.toml`.
//!
//! The [`Config`] struct is the single source of truth for all framework
//! configuration. It is loaded once at startup by [`Config::load`] and then
//! wrapped in an `Arc` inside [`AppState`](crate::state::AppState).

use std::path::Path;

use axum::http;
use serde::Deserialize;
use url::Url;

// ---------------------------------------------------------------------------
// Top-level config
// ---------------------------------------------------------------------------

/// Root configuration, deserialized from `atrg.toml`.
#[derive(Debug, Clone, Deserialize)]
pub struct Config {
    /// Application-level settings.
    pub app: AppConfig,

    /// OAuth / authentication settings.
    #[serde(default)]
    pub auth: AuthConfig,

    /// Database connection settings.
    #[serde(default)]
    pub database: DatabaseConfig,

    /// Optional Jetstream real-time event consumer settings.
    pub jetstream: Option<JetstreamConfig>,

    /// Optional relay firehose consumer settings.
    pub firehose: Option<FirehoseConfig>,

    /// Optional feed generator settings.
    pub feed_generator: Option<FeedGeneratorConfig>,

    /// Optional labeler settings.
    pub labeler: Option<LabelerConfig>,

    /// Optional rate limiting settings.
    pub rate_limit: Option<RateLimitTomlConfig>,
}

// ---------------------------------------------------------------------------
// AppConfig
// ---------------------------------------------------------------------------

/// `[app]` section of `atrg.toml`.
#[derive(Debug, Clone, Deserialize)]
pub struct AppConfig {
    /// Human-readable application name. Must be non-empty.
    pub name: String,

    /// Bind address for the HTTP server.
    #[serde(default = "default_host")]
    pub host: String,

    /// Bind port for the HTTP server.
    #[serde(default = "default_port")]
    pub port: u16,

    /// Secret key used for session signing. Should be ≥ 32 characters in
    /// production.
    pub secret_key: String,

    /// Allowed CORS origins. An empty list means same-origin only. A single
    /// `"*"` entry enables the permissive wildcard.
    #[serde(default)]
    pub cors_origins: Vec<String>,

    /// `"development"` or `"production"`. Affects cookie flags and security
    /// headers.
    #[serde(default = "default_environment")]
    pub environment: String,
}

impl Default for AppConfig {
    fn default() -> Self {
        Self {
            name: String::new(),
            host: default_host(),
            port: default_port(),
            secret_key: String::new(),
            cors_origins: Vec::new(),
            environment: default_environment(),
        }
    }
}

fn default_host() -> String {
    "127.0.0.1".to_string()
}

fn default_port() -> u16 {
    3000
}

fn default_environment() -> String {
    "development".to_string()
}

// ---------------------------------------------------------------------------
// AuthConfig
// ---------------------------------------------------------------------------

/// `[auth]` section of `atrg.toml`.
#[derive(Debug, Clone, Deserialize)]
pub struct AuthConfig {
    /// AT Protocol OAuth client ID (must be a valid URL).
    #[serde(default = "default_client_id")]
    pub client_id: String,

    /// OAuth redirect URI (must be a valid URL).
    #[serde(default = "default_redirect_uri")]
    pub redirect_uri: String,

    /// OAuth scope string.
    #[serde(default = "default_scope")]
    pub scope: String,
}

impl Default for AuthConfig {
    fn default() -> Self {
        Self {
            client_id: default_client_id(),
            redirect_uri: default_redirect_uri(),
            scope: default_scope(),
        }
    }
}

fn default_client_id() -> String {
    "http://localhost:3000/client-metadata.json".to_string()
}

fn default_redirect_uri() -> String {
    "http://localhost:3000/auth/callback".to_string()
}

fn default_scope() -> String {
    "atproto transition:generic".to_string()
}

// ---------------------------------------------------------------------------
// DatabaseConfig
// ---------------------------------------------------------------------------

/// `[database]` section of `atrg.toml`.
#[derive(Debug, Clone, Deserialize)]
pub struct DatabaseConfig {
    /// SQLite connection URL.
    #[serde(default = "default_database_url")]
    pub url: String,
}

impl Default for DatabaseConfig {
    fn default() -> Self {
        Self {
            url: default_database_url(),
        }
    }
}

fn default_database_url() -> String {
    "sqlite://atrg.db".to_string()
}

// ---------------------------------------------------------------------------
// JetstreamConfig
// ---------------------------------------------------------------------------

/// `[jetstream]` section of `atrg.toml`. Only present when Jetstream
/// consumption is enabled.
#[derive(Debug, Clone, Deserialize)]
pub struct JetstreamConfig {
    /// Jetstream relay host, e.g. `"jetstream1.us-east.bsky.network"`.
    pub host: String,

    /// NSID collections to subscribe to, e.g. `["app.bsky.feed.post"]`.
    pub collections: Vec<String>,

    /// Optional path or URL to a ZSTD dictionary for decompression.
    pub zstd_dict: Option<String>,

    /// Bounded back-pressure channel size.
    #[serde(default = "default_channel_capacity")]
    pub channel_capacity: usize,

    /// Event lag threshold before shedding/warning.
    #[serde(default = "default_max_lag_events")]
    pub max_lag_events: usize,
}

fn default_channel_capacity() -> usize {
    1024
}

fn default_max_lag_events() -> usize {
    10_000
}

// ---------------------------------------------------------------------------
// FirehoseConfig
// ---------------------------------------------------------------------------

/// `[firehose]` section of `atrg.toml`. Present when relay firehose
/// consumption is enabled (full `com.atproto.sync.subscribeRepos`).
#[derive(Debug, Clone, Deserialize)]
pub struct FirehoseConfig {
    /// Relay WebSocket URL, e.g. `"wss://bsky.network"`.
    pub relay: String,

    /// Sequence number to resume from. `None` means start from head.
    pub cursor: Option<i64>,

    /// Bounded back-pressure channel capacity.
    #[serde(default = "default_firehose_channel_capacity")]
    pub channel_capacity: usize,
}

fn default_firehose_channel_capacity() -> usize {
    1024
}

// ---------------------------------------------------------------------------
// FeedGeneratorConfig
// ---------------------------------------------------------------------------

/// `[feed_generator]` section of `atrg.toml`. Present when the server
/// acts as an AT Protocol feed generator.
#[derive(Debug, Clone, Deserialize)]
pub struct FeedGeneratorConfig {
    /// DID of the feed generator service (typically `did:web:<hostname>`).
    pub did: String,
}

// ---------------------------------------------------------------------------
// LabelerConfig
// ---------------------------------------------------------------------------

/// `[labeler]` section of `atrg.toml`. Present when the server acts as
/// an AT Protocol labeler.
#[derive(Debug, Clone, Deserialize)]
pub struct LabelerConfig {
    /// DID of the labeler service.
    pub did: String,

    /// Path to the signing key file (PEM format).
    pub signing_key_path: Option<String>,

    /// Inline signing key (base64-encoded, for env var injection).
    pub signing_key_base64: Option<String>,
}

// ---------------------------------------------------------------------------
// RateLimitConfig (TOML)
// ---------------------------------------------------------------------------

/// `[rate_limit]` section of `atrg.toml`.
#[derive(Debug, Clone, Deserialize)]
pub struct RateLimitTomlConfig {
    /// Maximum sustained requests per second.
    #[serde(default = "default_rps")]
    pub requests_per_second: f64,

    /// Maximum burst size.
    #[serde(default = "default_burst")]
    pub burst: u32,

    /// Whether rate limiting is enabled (default: true in production).
    #[serde(default = "default_rate_limit_enabled")]
    pub enabled: bool,
}

fn default_rps() -> f64 {
    10.0
}

fn default_burst() -> u32 {
    50
}

fn default_rate_limit_enabled() -> bool {
    true
}

// ---------------------------------------------------------------------------
// Loading & validation
// ---------------------------------------------------------------------------

impl Config {
    /// Load and validate a [`Config`] from the TOML file at `path`.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be read, the TOML is malformed, or
    /// mandatory validation checks fail (e.g. empty `app.name`).
    pub fn load(path: impl AsRef<Path>) -> anyhow::Result<Self> {
        let path = path.as_ref();
        let contents = std::fs::read_to_string(path).map_err(|e| {
            anyhow::anyhow!(
                "Failed to read config file '{}': {}. \
                 Make sure you're running from a directory that contains atrg.toml.",
                path.display(),
                e
            )
        })?;
        Self::parse_toml(&contents)
    }

    /// Parse and validate a [`Config`] from a TOML string.
    ///
    /// This is the inner implementation shared by [`Config::load`] and tests.
    pub fn parse_toml(toml_str: &str) -> anyhow::Result<Self> {
        let config: Config = toml::from_str(toml_str).map_err(|e| {
            // Provide a friendlier message when a required section is missing.
            let msg = e.to_string();
            if msg.contains("missing field `app`") {
                anyhow::anyhow!(
                    "Config error: the [app] section is required in atrg.toml. \
                     At minimum you need:\n\n\
                     [app]\n\
                     name = \"my-app\"\n\
                     secret_key = \"some-secret-key\"\n\n\
                     Full error: {e}"
                )
            } else {
                anyhow::anyhow!("Failed to parse atrg.toml: {e}")
            }
        })?;

        config.validate()?;
        Ok(config)
    }

    /// Run all validation checks and emit warnings.
    fn validate(&self) -> anyhow::Result<()> {
        // -- hard errors ------------------------------------------------

        if self.app.name.trim().is_empty() {
            anyhow::bail!("Config error: app.name must not be empty");
        }

        if self.app.secret_key.trim().is_empty() {
            anyhow::bail!("Config error: app.secret_key must not be empty");
        }

        // Validate redirect_uri is a proper URL.
        if Url::parse(&self.auth.redirect_uri).is_err() {
            anyhow::bail!(
                "Config error: auth.redirect_uri '{}' is not a valid URL",
                self.auth.redirect_uri
            );
        }

        // Validate client_id is a proper URL.
        if Url::parse(&self.auth.client_id).is_err() {
            anyhow::bail!(
                "Config error: auth.client_id '{}' is not a valid URL",
                self.auth.client_id
            );
        }

        // Validate each CORS origin entry.
        for origin in &self.app.cors_origins {
            if origin == "*" {
                continue; // wildcard is fine
            }
            if origin.parse::<http::HeaderValue>().is_err() {
                anyhow::bail!(
                    "Config error: cors_origins entry '{}' is not a valid origin",
                    origin
                );
            }
        }

        // -- soft warnings ---------------------------------------------

        if self.app.secret_key.len() < 32 {
            tracing::warn!(
                "app.secret_key is only {} characters — use at least 32 for production",
                self.app.secret_key.len()
            );
        }

        let is_local = self.app.host == "localhost" || self.app.host == "127.0.0.1";
        if self.app.secret_key == "CHANGE_ME_IN_PRODUCTION" && !is_local {
            tracing::warn!(
                "app.secret_key is the scaffold default and host is '{}' — \
                 change it before deploying!",
                self.app.host
            );
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    /// A full config fixture exercising every field.
    const FULL_CONFIG: &str = r#"
[app]
name = "my-app"
host = "0.0.0.0"
port = 8080
secret_key = "super-secret-key-that-is-long-enough"
cors_origins = ["http://localhost:5173", "https://example.com"]
environment = "production"

[auth]
client_id = "https://myapp.example.com/client-metadata.json"
redirect_uri = "https://myapp.example.com/auth/callback"
scope = "atproto transition:generic"

[database]
url = "sqlite://prod.db"

[jetstream]
host = "jetstream1.us-east.bsky.network"
collections = ["app.bsky.feed.post", "app.bsky.feed.like"]
zstd_dict = "/tmp/dict.bin"
channel_capacity = 2048
max_lag_events = 20000
"#;

    /// Minimal config — only the required fields.
    const MINIMAL_CONFIG: &str = r#"
[app]
name = "tiny"
secret_key = "abcdefghijklmnopqrstuvwxyz123456"
"#;

    #[test]
    fn parse_full_config() {
        let cfg = Config::parse_toml(FULL_CONFIG).expect("should parse full config");

        assert_eq!(cfg.app.name, "my-app");
        assert_eq!(cfg.app.host, "0.0.0.0");
        assert_eq!(cfg.app.port, 8080);
        assert_eq!(cfg.app.environment, "production");
        assert_eq!(cfg.app.cors_origins.len(), 2);

        assert_eq!(
            cfg.auth.client_id,
            "https://myapp.example.com/client-metadata.json"
        );
        assert_eq!(
            cfg.auth.redirect_uri,
            "https://myapp.example.com/auth/callback"
        );
        assert_eq!(cfg.auth.scope, "atproto transition:generic");

        assert_eq!(cfg.database.url, "sqlite://prod.db");

        let js = cfg.jetstream.expect("jetstream should be present");
        assert_eq!(js.host, "jetstream1.us-east.bsky.network");
        assert_eq!(js.collections.len(), 2);
        assert_eq!(js.zstd_dict.as_deref(), Some("/tmp/dict.bin"));
        assert_eq!(js.channel_capacity, 2048);
        assert_eq!(js.max_lag_events, 20000);
    }

    #[test]
    fn parse_minimal_config_defaults_applied() {
        let cfg = Config::parse_toml(MINIMAL_CONFIG).expect("should parse minimal config");

        // Explicit values
        assert_eq!(cfg.app.name, "tiny");

        // Defaults
        assert_eq!(cfg.app.host, "127.0.0.1");
        assert_eq!(cfg.app.port, 3000);
        assert_eq!(cfg.app.environment, "development");
        assert!(cfg.app.cors_origins.is_empty());

        assert_eq!(
            cfg.auth.client_id,
            "http://localhost:3000/client-metadata.json"
        );
        assert_eq!(cfg.auth.redirect_uri, "http://localhost:3000/auth/callback");
        assert_eq!(cfg.auth.scope, "atproto transition:generic");

        assert_eq!(cfg.database.url, "sqlite://atrg.db");
        assert!(cfg.jetstream.is_none());
    }

    #[test]
    fn missing_app_section_gives_friendly_error() {
        let toml = r#"
[database]
url = "sqlite://test.db"
"#;
        let err = Config::parse_toml(toml).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("[app] section is required"),
            "expected friendly error, got: {msg}"
        );
    }

    #[test]
    fn empty_name_is_rejected() {
        let toml = r#"
[app]
name = ""
secret_key = "abcdefghijklmnopqrstuvwxyz123456"
"#;
        let err = Config::parse_toml(toml).unwrap_err();
        assert!(
            err.to_string().contains("app.name must not be empty"),
            "got: {}",
            err
        );
    }

    #[test]
    fn empty_secret_key_is_rejected() {
        let toml = r#"
[app]
name = "test"
secret_key = ""
"#;
        let err = Config::parse_toml(toml).unwrap_err();
        assert!(
            err.to_string().contains("app.secret_key must not be empty"),
            "got: {}",
            err
        );
    }

    #[test]
    fn invalid_redirect_uri_is_rejected() {
        let toml = r#"
[app]
name = "test"
secret_key = "abcdefghijklmnopqrstuvwxyz123456"

[auth]
redirect_uri = "not a url at all"
"#;
        let err = Config::parse_toml(toml).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("auth.redirect_uri") && msg.contains("not a valid URL"),
            "expected redirect_uri error, got: {msg}"
        );
    }

    #[test]
    fn invalid_client_id_is_rejected() {
        let toml = r#"
[app]
name = "test"
secret_key = "abcdefghijklmnopqrstuvwxyz123456"

[auth]
client_id = "not a url"
"#;
        let err = Config::parse_toml(toml).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("auth.client_id") && msg.contains("not a valid URL"),
            "expected client_id error, got: {msg}"
        );
    }

    #[test]
    fn invalid_cors_origin_is_rejected() {
        let toml = r#"
[app]
name = "test"
secret_key = "abcdefghijklmnopqrstuvwxyz123456"
cors_origins = ["http://ok.example.com", "\x00bad"]
"#;
        let err = Config::parse_toml(toml).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("cors_origins"),
            "expected cors origin error, got: {msg}"
        );
    }

    #[test]
    fn wildcard_cors_origin_is_accepted() {
        let toml = r#"
[app]
name = "test"
secret_key = "abcdefghijklmnopqrstuvwxyz123456"
cors_origins = ["*"]
"#;
        Config::parse_toml(toml).expect("wildcard should be accepted");
    }

    #[test]
    fn parse_config_with_firehose_and_feeds() {
        let toml = r#"
[app]
name = "test"
secret_key = "abcdefghijklmnopqrstuvwxyz123456"

[firehose]
relay = "wss://bsky.network"

[feed_generator]
did = "did:web:feeds.example.com"

[labeler]
did = "did:web:labels.example.com"
signing_key_path = "/etc/keys/labeler.pem"

[rate_limit]
requests_per_second = 20.0
burst = 100
enabled = true
"#;
        let cfg = Config::parse_toml(toml).unwrap();
        let fh = cfg.firehose.unwrap();
        assert_eq!(fh.relay, "wss://bsky.network");
        assert!(fh.cursor.is_none());
        assert_eq!(fh.channel_capacity, 1024);

        let fg = cfg.feed_generator.unwrap();
        assert_eq!(fg.did, "did:web:feeds.example.com");

        let lb = cfg.labeler.unwrap();
        assert_eq!(lb.did, "did:web:labels.example.com");
        assert_eq!(lb.signing_key_path.unwrap(), "/etc/keys/labeler.pem");

        let rl = cfg.rate_limit.unwrap();
        assert!((rl.requests_per_second - 20.0).abs() < f64::EPSILON);
        assert_eq!(rl.burst, 100);
    }

    #[test]
    fn new_sections_are_all_optional() {
        let toml = r#"
[app]
name = "test"
secret_key = "abcdefghijklmnopqrstuvwxyz123456"
"#;
        let cfg = Config::parse_toml(toml).unwrap();
        assert!(cfg.firehose.is_none());
        assert!(cfg.feed_generator.is_none());
        assert!(cfg.labeler.is_none());
        assert!(cfg.rate_limit.is_none());
    }

    #[test]
    fn jetstream_defaults_applied() {
        let toml = r#"
[app]
name = "test"
secret_key = "abcdefghijklmnopqrstuvwxyz123456"

[jetstream]
host = "jetstream1.us-east.bsky.network"
collections = ["app.bsky.feed.post"]
"#;
        let cfg = Config::parse_toml(toml).unwrap();
        let js = cfg.jetstream.unwrap();
        assert_eq!(js.channel_capacity, 1024);
        assert_eq!(js.max_lag_events, 10_000);
        assert!(js.zstd_dict.is_none());
    }
}