pzzld_server/workers/serve/
actor.rs

1/*
2    Appellation: server <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use super::{serve_dir, ServerContext};
6use crate::config::Scope;
7use core::net::SocketAddr;
8use std::sync::Arc;
9use tokio::sync::broadcast;
10
11/// [Server] is a
12///
13///
14pub struct Server {
15    inner: Arc<ServerContext>,
16}
17
18impl Server {
19    /// Create a new instance of [Server]
20    pub fn new(addr: SocketAddr, scope: Scope) -> Self {
21        let ctx = ServerContext::new(addr, scope);
22        Self {
23            inner: Arc::new(ctx),
24        }
25    }
26    /// serve the resources on the configured address
27    #[tracing::instrument(skip(self), name = "serve", target = "server")]
28    pub async fn serve(self) {
29        let listener = self.listen().await;
30        tracing::debug!("listening on http://{}", self.addr());
31        axum::serve(listener, self.router())
32            .with_graceful_shutdown(crate::graceful_shutdown())
33            .await
34            .unwrap()
35    }
36
37    fn router(&self) -> axum::Router {
38        use tower_http::trace::TraceLayer;
39        let workdir = self.scope().as_path();
40        axum::Router::new()
41            .merge(serve_dir("/", workdir))
42            .layer(TraceLayer::new_for_http())
43    }
44}
45
46impl core::ops::Deref for Server {
47    type Target = ServerContext;
48
49    fn deref(&self) -> &Self::Target {
50        &self.inner
51    }
52}