roadster 0.9.0-alpha.5

A "Batteries Included" web framework for rust designed to get you moving fast.
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
use crate::app::context::AppContext;
use crate::error::RoadsterResult;
use crate::service::{Service, ServiceBuilder};
use axum_core::extract::FromRef;
use std::any::{Any, TypeId, type_name};
use std::collections::{BTreeMap, HashSet};
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;
use tracing::info;

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ServiceRegistryError {
    /// The provided [`Service`] was already registered. Contains the [`Service::name`]
    /// of the provided service.
    #[error("The provided `Service` was already registered: `{0}`")]
    AlreadyRegistered(String),

    /// Unable to find a [`Service`] instance of the requested type. Contains the [`type_name`]
    /// of the requested type.
    #[error("Unable to find an `Service` instance of type `{0}`")]
    NotRegistered(String),

    /// The [`Service`] was already ran using [`Service::run`], so it is no longer available
    /// in the [`ServiceRegistry`].
    #[error("`Service` instance of type `{0}` was already ran")]
    AlreadyRan(String),

    /// Unable to downcast the registered instance to the requested type. Contains the [`type_name`]
    /// of the requested type.
    #[error("Unable to downcast the registered instance of `Service` to type `{0}`")]
    Downcast(String),

    #[error(transparent)]
    Other(#[from] Box<dyn Send + Sync + std::error::Error>),
}

/// Registry for [`Service`]s that will be run in the app.
pub struct ServiceRegistry<S>
where
    S: 'static + Send + Sync + Clone,
    AppContext: FromRef<S>,
{
    pub(crate) state: S,
    pub(crate) service_names: HashSet<String>,
    pub(crate) services: BTreeMap<TypeId, ServiceWrapper<S>>,
}

impl<S> ServiceRegistry<S>
where
    S: 'static + Send + Sync + Clone,
    AppContext: FromRef<S>,
{
    pub(crate) fn new(state: &S) -> Self {
        Self {
            state: state.clone(),
            service_names: Default::default(),
            services: Default::default(),
        }
    }

    /// Register a new service. If the service is not enabled (e.g., [`Service::enabled`] is `false`),
    /// the service will not be registered.
    pub fn register_service<Srvc>(&mut self, service: Srvc) -> RoadsterResult<()>
    where
        Srvc: 'static + Service<S>,
    {
        self.register_wrapped(ServiceWrapper::new(service))
    }

    /// Build and register a new service. If the service is not enabled (e.g.,
    /// [`Service::enabled`] is `false`), the service will not be built or registered.
    pub async fn register_builder<Srvc, B>(&mut self, builder: B) -> RoadsterResult<()>
    where
        Srvc: 'static + Service<S>,
        B: 'static + ServiceBuilder<S, Srvc>,
    {
        if !builder.enabled(&self.state) {
            info!(service.builder.name=%builder.name(), "Service is not enabled, skipping building and registration");
            return Ok(());
        }

        info!(service.builder.name=%builder.name(), "Building service");
        let service = builder
            .build(&self.state)
            .await
            .map_err(|err| ServiceRegistryError::Other(Box::new(err)))?;

        self.register_wrapped(ServiceWrapper::new(service))
    }

    pub(crate) fn register_wrapped(&mut self, service: ServiceWrapper<S>) -> RoadsterResult<()> {
        let name = service.name();

        info!(service.name=%name, "Registering service");

        if !self.service_names.insert(name.clone())
            || self.services.insert(service.type_id, service).is_some()
        {
            return Err(ServiceRegistryError::AlreadyRegistered(name).into());
        }
        Ok(())
    }

    /// Invoke a callback on a reference to a previously registered [`Service`] of the specified
    /// type.
    ///
    /// This is useful to call a method that only exists on a concrete [`Service`]
    /// implementor after the app was prepared.
    #[cfg_attr(
        all(feature = "http", feature = "open-api"),
        doc = r##"
For example, to get the OpenAPI schema for an app,
setup and register the [`crate::service::http::service::HttpService`], get the service
from the registry with this method ([`ServiceRegistry::invoke`]), and call
[`crate::service::http::service::HttpService::print_open_api_schema`] to get the schema.
    "##
    )]
    ///
    /// # Examples
    #[cfg_attr(
        all(feature = "open-api", feature = "otel-grpc"),
        doc = r##"
  ```rust
# tokio_test::block_on(async {
# use roadster::service::http::service::OpenApiArgs;
# use roadster::app::RoadsterApp;
# use roadster::service::ServiceBuilder;
# use roadster::service::http::service::HttpService;
# use std::env::current_dir;
# use std::path::PathBuf;
# use std::sync::LazyLock;
# use uuid::Uuid;
# use roadster::app::PrepareOptions;
# use roadster::config::environment::Environment;
# use async_trait::async_trait;
# use tokio_util::sync::CancellationToken;
# use roadster::app::context::AppContext;
# use roadster::error::RoadsterResult;
# use roadster::service::function::service::FunctionService;
# use roadster::service::registry::ServiceRegistry;
# use roadster::app::prepare;
# use roadster::service::Service;
#
type App = RoadsterApp<AppContext>;

let app: App = RoadsterApp::builder()
    .state_provider(|state| Ok(state))
    .add_service_provider(|registry, state| Box::pin(async  {
        registry.register_builder(
            HttpService::builder(state, Some("/api"))
        ).await?;
        Ok(())
    }))
    .build();

// Prepare the app. This runs all initialization logic for the app but does not actually
// start the app.
let prepared = prepare(
    app,
    PrepareOptions::builder()
        .env(Environment::Development)
#       .config_dir(PathBuf::from("examples/full/config").canonicalize().unwrap())
        .build()
).await.unwrap();
// Get the `HttpService` from the `ServiceRegistry` and get the OpenAPI schema.
prepared.service_registry.invoke(async |service: &HttpService| {
    service.open_api_schema(&OpenApiArgs::builder().build()).unwrap();
}).await;
# })
```
"##
    )]
    pub async fn invoke<Srvc, F, R>(&self, invoke: F) -> RoadsterResult<R>
    where
        Srvc: 'static + Service<S>,
        F: AsyncFnOnce(&Srvc) -> R,
    {
        let service_wrapper = self
            .services
            .get(&TypeId::of::<Srvc>())
            .ok_or_else(|| ServiceRegistryError::NotRegistered(type_name::<Srvc>().to_string()))?;
        let guard = service_wrapper.inner.lock().await;
        let inner = guard
            .as_ref()
            .ok_or_else(|| ServiceRegistryError::AlreadyRan(type_name::<Srvc>().to_string()))?;
        let srvc = inner
            .downcast_ref::<Srvc>()
            .ok_or_else(|| ServiceRegistryError::Downcast(type_name::<Srvc>().to_string()))?;
        let result = invoke(srvc).await;
        Ok(result)
    }
}

type EnabledFn<S> = Box<
    dyn Send
        + Sync
        + for<'a> Fn(&'a S) -> std::pin::Pin<Box<dyn 'a + Send + Future<Output = bool>>>,
>;

type BeforeRunFn<S> = Box<
    dyn Send
        + Sync
        + for<'a> Fn(&'a S) -> std::pin::Pin<Box<dyn 'a + Send + Future<Output = RoadsterResult<()>>>>,
>;

type RunFn<S> = Box<
    dyn Send
        + Sync
        + for<'a> Fn(
            &'a S,
            CancellationToken,
        )
            -> std::pin::Pin<Box<dyn 'a + Send + Future<Output = RoadsterResult<()>>>>,
>;

/// Wrapper around a [`Service`] to allow storing the [`Service`]s in a collection regardless of
/// their [`Service::Error`] associated types.
pub(crate) struct ServiceWrapper<S>
where
    S: 'static + Send + Sync + Clone,
    AppContext: FromRef<S>,
{
    type_id: TypeId,
    name: String,
    enabled_fn: EnabledFn<S>,
    before_run_fn: BeforeRunFn<S>,
    run_fn: RunFn<S>,
    inner: Arc<Mutex<Option<Box<dyn Send + Sync + Any>>>>,
}

impl<S> ServiceWrapper<S>
where
    S: 'static + Send + Sync + Clone,
    AppContext: FromRef<S>,
{
    pub(crate) fn new<Srvc>(service: Srvc) -> Self
    where
        Srvc: 'static + Send + Sync + Any + Service<S>,
    {
        let type_id = service.type_id();
        let name = service.name();
        /*
        For some reason, we need to explicitly annotate the type here. If we don't, Rust
        complains about the value inside the `Box` not being compatible with the `Send + Sync + Any`
        trait bounds when trying to assign the value to the `ServiceWrapper#inner` field. This is
        also why we need to downcast in the method wrappers below (we don't have a handle to a
        `Service` instance, just an `Any`.
         */
        let inner: Arc<Mutex<Option<Box<dyn Send + Sync + Any>>>> =
            Arc::new(Mutex::new(Some(Box::new(service))));
        let enabled_fn: EnabledFn<S> = {
            let inner = inner.clone();
            Box::new(move |state| {
                let inner = inner.clone();
                Box::pin(async move {
                    let guard = inner.lock().await;
                    #[allow(clippy::expect_used)]
                    let inner = guard
                        .as_ref()
                        .unwrap_or_else(|| panic!("`Service#enabled` can not be called for `Service` of type `{}`; `Service#run` was already called", type_name::<Srvc>()))
                        .downcast_ref::<Srvc>()
                        .unwrap_or_else(|| panic!("Registered `Service` can not be downcast to type `{}`", type_name::<Srvc>()));
                    inner.enabled(state)
                })
            })
        };
        let before_run_fn: BeforeRunFn<S> = {
            let inner = inner.clone();
            Box::new(move |state| {
                let inner = inner.clone();
                Box::pin(async move {
                    let guard = inner.lock().await;
                    let inner = guard
                        .as_ref()
                        .ok_or_else(|| {
                            ServiceRegistryError::AlreadyRan(type_name::<Srvc>().to_string())
                        })?
                        .downcast_ref::<Srvc>()
                        .ok_or_else(|| {
                            ServiceRegistryError::Downcast(type_name::<Srvc>().to_string())
                        })?;
                    inner
                        .before_run(state)
                        .await
                        .map_err(|err| ServiceRegistryError::Other(Box::new(err)))?;
                    Ok(())
                })
            })
        };
        let run_fn: RunFn<S> = {
            let inner = inner.clone();
            Box::new(move |state, cancellation_token| {
                let inner = inner.clone();
                Box::pin(async move {
                    let mut guard = inner.lock().await;
                    let inner = guard
                        .take()
                        .ok_or_else(|| {
                            ServiceRegistryError::AlreadyRan(type_name::<Srvc>().to_string())
                        })?
                        .downcast::<Srvc>()
                        .map_err(|_err| {
                            ServiceRegistryError::Downcast(type_name::<Srvc>().to_string())
                        })?;
                    inner
                        .run(state, cancellation_token)
                        .await
                        .map_err(|err| ServiceRegistryError::Other(Box::new(err)))?;
                    Ok(())
                })
            })
        };
        Self {
            type_id,
            name,
            enabled_fn,
            before_run_fn,
            run_fn,
            inner,
        }
    }

    pub(crate) fn name(&self) -> String {
        self.name.clone()
    }

    pub(crate) async fn enabled(&self, state: &S) -> bool {
        (self.enabled_fn)(state).await
    }

    pub(crate) async fn before_run(&self, state: &S) -> RoadsterResult<()> {
        (self.before_run_fn)(state).await
    }

    pub(crate) async fn run(
        &self,
        state: &S,
        cancel_token: CancellationToken,
    ) -> RoadsterResult<()> {
        (self.run_fn)(state, cancel_token).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::Error;
    use crate::service::{MockService, MockServiceBuilder};
    use async_trait::async_trait;
    use rstest::rstest;
    use tokio_util::sync::CancellationToken;
    use uuid::Uuid;

    #[rstest]
    #[case(true, 1)]
    #[case(false, 1)]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn register_service(#[case] service_enabled: bool, #[case] expected_count: usize) {
        // Arrange
        let context = AppContext::test(None, None, None).unwrap();

        let mut service: MockService<AppContext> = MockService::default();
        service.expect_enabled().return_const(service_enabled);
        service.expect_name().return_const("test".to_string());

        // Act
        let mut subject: ServiceRegistry<AppContext> = ServiceRegistry::new(&context);
        subject.register_service(service).unwrap();

        // Assert
        assert_eq!(subject.services.len(), expected_count);
        assert_eq!(subject.services.len(), subject.service_names.len());
        assert!(
            subject
                .services
                .contains_key(&TypeId::of::<MockService<AppContext>>())
        );
    }

    #[rstest]
    #[case(true, true, 1)]
    #[case(false, true, 1)]
    #[case(true, false, 0)]
    #[case(false, false, 0)]
    #[tokio::test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    async fn register_builder(
        #[case] service_enabled: bool,
        #[case] builder_enabled: bool,
        #[case] expected_count: usize,
    ) {
        // Arrange
        let context = AppContext::test(None, None, None).unwrap();

        let mut builder = MockServiceBuilder::default();
        builder.expect_enabled().return_const(builder_enabled);
        builder.expect_name().return_const("test".to_string());
        builder.expect_build().returning(move |_| {
            let mut service: MockService<AppContext> = MockService::default();
            service.expect_enabled().return_const(service_enabled);
            service.expect_name().return_const("test".to_string());
            Ok(service)
        });

        // Act
        let mut subject: ServiceRegistry<AppContext> = ServiceRegistry::new(&context);
        subject.register_builder(builder).await.unwrap();

        // Assert
        assert_eq!(subject.services.len(), expected_count);
        assert_eq!(subject.services.len(), subject.service_names.len());
        assert_eq!(
            subject
                .services
                .contains_key(&TypeId::of::<MockService<AppContext>>()),
            expected_count > 0
        );
    }

    struct FooService {
        id: Uuid,
    }
    #[async_trait]
    #[cfg_attr(coverage_nightly, coverage(off))]
    impl Service<AppContext> for FooService {
        type Error = crate::error::Error;

        fn name(&self) -> String {
            "foo".to_string()
        }
        #[cfg_attr(coverage_nightly, coverage(off))]
        fn enabled(&self, _: &AppContext) -> bool {
            true
        }
        #[cfg_attr(coverage_nightly, coverage(off))]
        async fn run(self: Box<Self>, _: &AppContext, _: CancellationToken) -> RoadsterResult<()> {
            todo!()
        }
    }

    struct BarService;
    #[async_trait]
    #[cfg_attr(coverage_nightly, coverage(off))]
    impl Service<AppContext> for BarService {
        type Error = crate::error::Error;

        fn name(&self) -> String {
            "bar".to_string()
        }
        #[cfg_attr(coverage_nightly, coverage(off))]
        fn enabled(&self, _: &AppContext) -> bool {
            true
        }
        #[cfg_attr(coverage_nightly, coverage(off))]
        async fn run(self: Box<Self>, _: &AppContext, _: CancellationToken) -> RoadsterResult<()> {
            todo!()
        }
    }

    #[rstest]
    #[case(true, true)]
    #[case(false, true)]
    #[case(false, false)]
    #[tokio::test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    async fn invoke(#[case] registered: bool, #[case] correct_type: bool) {
        // Arrange
        let context = AppContext::test(None, None, None).unwrap();

        let id = Uuid::new_v4();
        let service = FooService { id };

        let mut subject: ServiceRegistry<AppContext> = ServiceRegistry::new(&context);
        if registered && correct_type {
            subject.register_service(service).unwrap();

            let duplicate = subject.register_service(FooService { id: Uuid::new_v4() });
            assert!(matches!(
                duplicate,
                Err(Error::ServiceRegistry(
                    ServiceRegistryError::AlreadyRegistered(_)
                ))
            ));
        } else if registered && !correct_type {
            subject.register_service(BarService).unwrap();
        }

        // Act
        let service = subject
            .invoke::<FooService, _, _>(async |srvc| srvc.id)
            .await;

        if !registered {
            assert!(matches!(
                service,
                Err(Error::ServiceRegistry(ServiceRegistryError::NotRegistered(
                    _
                )))
            ));
        } else if !correct_type {
            assert!(matches!(
                service,
                Err(Error::ServiceRegistry(ServiceRegistryError::Downcast(_)))
            ));
        } else {
            assert_eq!(service.unwrap(), id);
        }
    }
}