vyuh 0.2.1

Vyuh web framework for Axum and SQLx with handler-first APIs
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
use std::{any::TypeId, ops::Deref, pin::Pin, sync::Arc};

use indexmap::IndexMap;

use crate::{
    Site,
    callables::{self, FromSite, IntoArgPart},
    db::DbPool,
    site::PartialSite,
};

#[derive(Debug, thiserror::Error)]
pub enum ServiceError {
    #[error("Service already registered for type: {0}")]
    AlreadyRegistered(&'static str),

    #[error("Service not found for type: {0}")]
    NotFound(String),

    #[error("Handler returned an unexpected output type")]
    UnexpectedOutput,

    #[error("Service Arc was shared when exclusive access was required during build")]
    ArcShared,

    #[error("Facade downcast failed: stored type did not match expected Arc<T>")]
    FacadeDowncast,

    #[error(transparent)]
    CallError(#[from] callables::CallError),
}

pub struct ServiceExposer<T: Send + Sync + 'static> {
    _marker: std::marker::PhantomData<T>,
    facades: Vec<ServiceFacade>,
}

impl<T: Send + Sync + 'static> ServiceExposer<T> {
    fn new() -> Self {
        Self {
            _marker: std::marker::PhantomData,
            facades: Vec::new(),
        }
    }

    pub fn expose<U: ?Sized + Send + Sync + 'static>(
        &mut self,
        coercer: impl Fn(Arc<T>) -> Arc<U> + Send + Sync + 'static,
    ) -> Result<(), ServiceError> {
        let facade = ServiceFacade {
            type_id: TypeId::of::<U>(),
            type_name: std::any::type_name::<U>(),
            // `obj` is an Arc<dyn Any> whose concrete type is Arc<T>.
            // We downcast it to Arc<T>, then apply the user-supplied coercer.
            coerce_fn: Box::new(move |obj: AnyArc| {
                let t: Arc<T> = obj
                    .downcast::<T>()
                    .map_err(|_| ServiceError::FacadeDowncast)?;
                let u: Arc<U> = coercer(t);
                Ok(Box::new(u) as AnyBox)
            }),
        };
        self.facades.push(facade);
        Ok(())
    }
}

pub struct ServiceRunner {
    workers: Vec<ServiceWorker>,
}

impl ServiceRunner {
    fn new() -> Self {
        Self {
            workers: Vec::new(),
        }
    }

    pub fn run<H, Args>(&mut self, name: &str, handler: H) -> Result<(), ServiceError>
    where
        H: callables::Specable<Args, Output = Result<(), ServiceError>> + Send + Sync + 'static,
        Args: callables::FromContext<ServiceWorkContext> + callables::IntoArgSpecs + Send + 'static,
    {
        let worker = ServiceWorker::from_callable(name.to_string(), handler);
        self.workers.push(worker);
        Ok(())
    }
}

#[derive(Clone)]
pub struct ServiceBuildContext {
    site: PartialSite,
}

impl ServiceBuildContext {
    pub fn db(&self) -> DbPool {
        self.site.db()
    }
}

impl callables::FromContextParts<ServiceBuildContext> for ServiceBuildContext {
    fn from_context_parts(ctx: &ServiceBuildContext) -> Result<Self, callables::CallError> {
        Ok(ctx.clone())
    }
}

impl callables::FromContext<ServiceBuildContext> for ServiceBuildContext {
    fn from_context(ctx: ServiceBuildContext) -> Result<Self, callables::CallError> {
        Ok(ctx)
    }
}

impl IntoArgPart for ServiceBuildContext {
    fn into_arg_part() -> callables::ArgPart {
        callables::ArgPart::Ignore
    }
}

#[derive(Clone)]
pub struct ServiceWorkContext {
    site: Site,
}

impl ServiceWorkContext {
    pub fn site(&self) -> &Site {
        &self.site
    }
}

impl callables::HasSite for ServiceWorkContext {
    fn site(&self) -> &Site {
        &self.site
    }
}

impl callables::FromContextParts<ServiceWorkContext> for ServiceWorkContext {
    fn from_context_parts(ctx: &ServiceWorkContext) -> Result<Self, callables::CallError> {
        Ok(ctx.clone())
    }
}

impl callables::FromContext<ServiceWorkContext> for ServiceWorkContext {
    fn from_context(ctx: ServiceWorkContext) -> Result<Self, callables::CallError> {
        Ok(ctx)
    }
}

impl IntoArgPart for ServiceWorkContext {
    fn into_arg_part() -> callables::ArgPart {
        callables::ArgPart::Ignore
    }
}

impl callables::FromContextParts<ServiceBuildContext> for DbPool {
    fn from_context_parts(ctx: &ServiceBuildContext) -> Result<Self, callables::CallError> {
        Ok(ctx.db())
    }
}

impl callables::FromContext<ServiceBuildContext> for DbPool {
    fn from_context(ctx: ServiceBuildContext) -> Result<Self, callables::CallError> {
        Ok(ctx.db())
    }
}

impl IntoArgPart for DbPool {
    fn into_arg_part() -> callables::ArgPart {
        callables::ArgPart::Ignore
    }
}

#[derive(Clone)]
struct ServiceWorker {
    name: String,
    func: callables::Callable<ServiceWorkContext, ServiceError>,
}

impl ServiceWorker {
    fn from_callable<H, Args>(name: String, handler: H) -> Self
    where
        H: callables::Specable<Args, Output = Result<(), ServiceError>> + Send + Sync + 'static,
        Args: callables::FromContext<ServiceWorkContext> + callables::IntoArgSpecs + Send + 'static,
    {
        let callable = callables::Callable::new(handler);

        ServiceWorker {
            name,
            func: callable,
        }
    }

    async fn call(&self, ctx: ServiceWorkContext) -> Result<(), ServiceError> {
        self.func.call(ctx).await?;
        Ok(())
    }
}

type AnyBox = Box<dyn std::any::Any + Send + Sync>;
type AnyArc = Arc<dyn std::any::Any + Send + Sync>;

struct ServiceFacade {
    type_id: TypeId,
    type_name: &'static str,
    // Receives a clone of the concrete Arc<T> (type-erased) and produces the
    // coerced Arc<U> for the requested interface, also type-erased.
    coerce_fn: Box<dyn Fn(AnyArc) -> Result<AnyBox, ServiceError> + Send + Sync>,
}

// Stored inside the serviceengine
struct ServiceEntry {
    inner: AnyArc,
    workers: Vec<ServiceWorker>,
    facades: Vec<ServiceFacade>,
}

#[derive(Clone)]
pub struct ServiceHandler {
    type_id: TypeId,
    type_name: &'static str,
    spec: callables::CallSpec,
    build_fn: Arc<
        dyn Fn(
                ServiceBuildContext,
            ) -> Pin<
                Box<dyn std::future::Future<Output = Result<ServiceEntry, ServiceError>> + Send>,
            > + Send
            + Sync,
    >,
}

impl ServiceHandler {
    pub fn new<T, H, Args>(handler: H) -> Self
    where
        T: Service,
        H: callables::Specable<Args, Output = ServiceInstance<T>> + Send + Sync + 'static,
        Args:
            callables::FromContext<ServiceBuildContext> + callables::IntoArgSpecs + Send + 'static,
    {
        let spec = callables::CallSpec::new::<Args, H>(&handler);
        let callable: callables::Callable<ServiceBuildContext, ServiceError> =
            callables::Callable::new(handler);

        let build_fn = move |ctx: ServiceBuildContext| {
            let callable = callable.clone();
            Box::pin(async move {
                let output = callable.call(ctx).await?;

                let inner = output.into_any_arc();

                let mut inner_svc: Arc<T> = inner
                    .downcast::<T>()
                    .map_err(|_| ServiceError::UnexpectedOutput)?;

                let service = Arc::get_mut(&mut inner_svc).ok_or(ServiceError::ArcShared)?;
                let mut exposer = ServiceExposer::<T>::new();
                // Expose the concrete type itself for internal use. This allows services to depend
                // on each other using their concrete types, while still exposing only the intended
                // interfaces to the service engine.
                exposer.expose(std::convert::identity)?;
                T::expose(&mut exposer)?;
                let mut sr = ServiceRunner::new();
                service.run(&mut sr)?;

                let inner_t: Arc<T> = inner_svc;

                Ok(ServiceEntry {
                    inner: inner_t as AnyArc,
                    workers: sr.workers,
                    facades: exposer.facades,
                })
            })
                as Pin<
                    Box<
                        dyn std::future::Future<Output = Result<ServiceEntry, ServiceError>> + Send,
                    >,
                >
        };

        ServiceHandler {
            type_id: TypeId::of::<T>(),
            type_name: std::any::type_name::<T>(),
            spec,
            build_fn: Arc::new(build_fn),
        }
    }

    pub(crate) fn operation(&self) -> callables::Operation {
        callables::Operation::from_specs(callables::OperationKind::Service, &self.spec)
    }
}

pub trait Service: Sized + Send + Sync + 'static {
    fn expose(_exposer: &mut ServiceExposer<Self>) -> Result<(), ServiceError> {
        Ok(())
    }

    fn run(&mut self, _runner: &mut ServiceRunner) -> Result<(), ServiceError> {
        Ok(())
    }
}

pub struct ServiceInstance<T: Service>(pub T);

impl<E, T: Service> callables::IntoOutput<E> for ServiceInstance<T> {
    fn into_output(self) -> Result<callables::PayloadData, E> {
        Ok(callables::PayloadData::new(self.0))
    }
}

impl<T: Service> callables::IntoReturnPart for ServiceInstance<T> {
    fn into_return_part() -> callables::ReturnPart {
        callables::ReturnPart::Empty
    }
}

impl<T: Service> From<T> for ServiceInstance<T> {
    fn from(t: T) -> Self {
        ServiceInstance(t)
    }
}

#[derive(Clone)]
pub struct ServiceRegistry {
    services: IndexMap<TypeId, ServiceHandler>,
}

impl ServiceRegistry {
    pub fn new() -> Self {
        Self {
            services: IndexMap::new(),
        }
    }

    pub(crate) fn register(&mut self, entry: ServiceHandler) -> Result<(), ServiceError> {
        let type_id = entry.type_id;
        if self.services.contains_key(&type_id) {
            return Err(ServiceError::AlreadyRegistered(entry.type_name));
        }
        self.services.insert(type_id, entry);
        Ok(())
    }

    pub(crate) fn merge(&mut self, other: ServiceRegistry) -> Result<(), ServiceError> {
        for entry in other.services.into_values() {
            self.register(entry)?;
        }
        Ok(())
    }
}

pub struct ServiceEngine {
    services: IndexMap<TypeId, AnyBox>,
    workers: Vec<ServiceWorker>,
}

impl ServiceEngine {
    pub fn new() -> Self {
        Self {
            services: IndexMap::new(),
            workers: vec![],
        }
    }

    pub(crate) async fn load(
        &mut self,
        registry: ServiceRegistry,
        partial_site: PartialSite,
    ) -> Result<(), ServiceError> {
        for (_, handler) in registry.services {
            let entry_future = (handler.build_fn)(ServiceBuildContext {
                site: partial_site.clone(),
            });
            let entry = entry_future.await?;
            for facade in entry.facades {
                let iface = (facade.coerce_fn)(entry.inner.clone())?;
                if self.services.contains_key(&facade.type_id) {
                    return Err(ServiceError::AlreadyRegistered(facade.type_name));
                }
                self.services.insert(facade.type_id, iface);
            }
            self.workers.extend(entry.workers);
        }
        Ok(())
    }

    pub(crate) async fn start_workers(
        &self,
        site: Site,
        joinset: &mut tokio::task::JoinSet<()>,
    ) -> Result<(), ServiceError> {
        for worker in &self.workers {
            let ctx = ServiceWorkContext { site: site.clone() };
            let worker = worker.clone();
            joinset.spawn(async move {
                if let Err(e) = worker.call(ctx).await {
                    tracing::error!("Service worker {} failed with error: {:?}", worker.name, e);
                }
            });
        }
        Ok(())
    }

    pub fn get<T: ?Sized + 'static>(&self) -> Option<Arc<T>> {
        let type_id = TypeId::of::<T>();
        self.services
            .get(&type_id)
            .and_then(|boxed_iface| boxed_iface.downcast_ref::<Arc<T>>().cloned())
    }
}

#[derive(Clone)]
pub struct ServiceRef<T: ?Sized + Send + Sync + 'static> {
    inner: Arc<T>,
}

impl<T: ?Sized + Send + Sync + 'static> ServiceRef<T> {
    pub fn get_ref(&self) -> &T {
        &self.inner
    }

    pub fn into_inner(self) -> Arc<T> {
        self.inner
    }
}

impl<T: ?Sized + Send + Sync + 'static> Deref for ServiceRef<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<T: ?Sized + Send + Sync + 'static> From<Arc<T>> for ServiceRef<T> {
    fn from(inner: Arc<T>) -> Self {
        Self { inner }
    }
}

impl<T: ?Sized + Send + Sync + 'static> FromSite for ServiceRef<T> {
    fn from_site(site: &Site) -> Result<Self, callables::CallError> {
        site.service::<T>()
            .map(Self::from)
            .map_err(|err| match err {
                ServiceError::NotFound(name) => callables::CallError::NotFound(name.into()),
                err => callables::CallError::Other(Box::new(err)),
            })
    }
}

impl<T: ?Sized + Send + Sync + 'static> axum::extract::FromRequestParts<Site> for ServiceRef<T> {
    type Rejection = ServiceError;

    async fn from_request_parts(
        _parts: &mut axum::http::request::Parts,
        state: &Site,
    ) -> Result<Self, Self::Rejection> {
        state.service::<T>().map(Self::from)
    }
}

impl<T: ?Sized + Send + Sync + 'static> IntoArgPart for ServiceRef<T> {
    fn into_arg_part() -> callables::ArgPart {
        callables::ArgPart::Ignore
    }
}

impl axum::response::IntoResponse for ServiceError {
    fn into_response(self) -> axum::response::Response {
        axum::response::IntoResponse::into_response(crate::errors::ErrorReport::new(
            axum::http::StatusCode::INTERNAL_SERVER_ERROR,
            crate::errors::ErrorSourceKind::Application,
            "service_error",
            self.to_string(),
        ))
    }
}