lumina-framework 0.2.1

A beautiful, fast, and elegant web framework for Rust
Documentation
use axum::Router as ax_router;
use axum::{handler::Handler, routing};
use std::sync::Arc;

/// Lumina Router — fluent builder API yang membungkus axum::Router.
#[derive(Clone)]
pub struct Router<S = ()> {
    inner: ax_router<S>,
    prefix: String,
    layers: Vec<Arc<dyn Fn(ax_router<S>) -> ax_router<S> + Send + Sync>>,
}

impl<S> Router<S>
where
    S: Clone + Send + Sync + 'static,
{
    /// Buat router baru yang kosong.
    pub fn new() -> Self {
        Self {
            inner: ax_router::new(),
            prefix: String::new(),
            layers: Vec::new(),
        }
    }

    /// Buat Lumina Router dari axum::Router yang sudah ada.
    pub fn from_axum(inner: ax_router<S>) -> Self {
        Self {
            inner,
            prefix: String::new(),
            layers: Vec::new(),
        }
    }

    /// Tentukan prefix untuk semua route yang didaftarkan setelah ini.
    pub fn prefix(mut self, prefix: &str) -> Self {
        self.prefix = prefix.to_string();
        self
    }

    fn normalize_path(&self, path: &str) -> String {
        if self.prefix.is_empty() {
            return path.to_string();
        }
        format!(
            "{}/{}",
            self.prefix.trim_end_matches('/'),
            path.trim_start_matches('/')
        )
    }

    /// Daftarkan route GET.
    pub fn get<H, T>(mut self, path: &str, handler: H) -> Self
    where
        H: Handler<T, S>,
        T: 'static,
    {
        let full_path = self.normalize_path(path);
        self.inner = self.inner.route(&full_path, routing::get(handler));
        self
    }

    /// Daftarkan route POST.
    pub fn post<H, T>(mut self, path: &str, handler: H) -> Self
    where
        H: Handler<T, S>,
        T: 'static,
    {
        let full_path = self.normalize_path(path);
        self.inner = self.inner.route(&full_path, routing::post(handler));
        self
    }

    /// Daftarkan route PUT.
    pub fn put<H, T>(mut self, path: &str, handler: H) -> Self
    where
        H: Handler<T, S>,
        T: 'static,
    {
        let full_path = self.normalize_path(path);
        self.inner = self.inner.route(&full_path, routing::put(handler));
        self
    }

    /// Daftarkan route DELETE.
    pub fn delete<H, T>(mut self, path: &str, handler: H) -> Self
    where
        H: Handler<T, S>,
        T: 'static,
    {
        let full_path = self.normalize_path(path);
        self.inner = self.inner.route(&full_path, routing::delete(handler));
        self
    }

    /// Daftarkan route PATCH.
    pub fn patch<H, T>(mut self, path: &str, handler: H) -> Self
    where
        H: Handler<T, S>,
        T: 'static,
    {
        let full_path = self.normalize_path(path);
        self.inner = self.inner.route(&full_path, routing::patch(handler));
        self
    }

    /// Grup route dengan prefix dan kustomisasi (seperti middleware).
    /// Contoh:
    /// ```rust,ignore
    /// router.group("/admin", |r| {
    ///     r.middleware(from_fn(admin_auth))
    ///      .get("/dashboard", dashboard_handler)
    /// })
    /// ```
    pub fn group<F>(mut self, prefix: &str, f: F) -> Self
    where
        F: FnOnce(Router<S>) -> Router<S>,
    {
        let sub_router = Router::new();
        let configured_sub = f(sub_router);
        self.inner = self.inner.nest(prefix, configured_sub.into_axum());
        self
    }

    /// Gabungkan dengan Router lain.
    pub fn merge(mut self, other: Router<S>) -> Self {
        self.inner = self.inner.merge(other.into_axum());
        self
    }

    /// Nest router lain di bawah prefix tertentu.
    pub fn nest(mut self, prefix: &str, other: Router<S>) -> Self {
        self.inner = self.inner.nest(prefix, other.into_axum());
        self
    }

    /// Tambahkan middleware (layer) ke router.
    /// Middleware ini akan diterapkan ke semua route yang ada di router ini saat dibuild.
    pub fn layer<L>(mut self, layer: L) -> Self
    where
        L: tower::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
        L::Service: tower::Service<axum::extract::Request> + Clone + Send + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Response:
            axum::response::IntoResponse + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Error:
            Into<std::convert::Infallible> + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Future: Send + 'static,
    {
        self.layers
            .push(Arc::new(move |router| router.layer(layer.clone())));
        self
    }

    /// Alias untuk layer agar lebih familiar bagi pengguna Laravel.
    pub fn middleware<L>(self, layer: L) -> Self
    where
        L: tower::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
        L::Service: tower::Service<axum::extract::Request> + Clone + Send + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Response:
            axum::response::IntoResponse + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Error:
            Into<std::convert::Infallible> + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Future: Send + 'static,
    {
        self.layer(layer)
    }

    /// Tambahkan route-level middleware (hanya berlaku untuk route pada router ini).
    pub fn route_layer<L>(mut self, layer: L) -> Self
    where
        L: tower::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
        L::Service: tower::Service<axum::extract::Request> + Clone + Send + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Response:
            axum::response::IntoResponse + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Error:
            Into<std::convert::Infallible> + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Future: Send + 'static,
    {
        self.layers
            .push(Arc::new(move |router| router.route_layer(layer.clone())));
        self
    }

    /// Alias untuk `route_layer` agar familiar bagi pengguna Laravel.
    pub fn route_middleware<L>(self, layer: L) -> Self
    where
        L: tower::Layer<axum::routing::Route> + Clone + Send + Sync + 'static,
        L::Service: tower::Service<axum::extract::Request> + Clone + Send + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Response:
            axum::response::IntoResponse + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Error:
            Into<std::convert::Infallible> + 'static,
        <L::Service as tower::Service<axum::extract::Request>>::Future: Send + 'static,
    {
        self.route_layer(layer)
    }

    /// Konsumsi Router ini menjadi `axum::Router` beserta seluruh layer middleware-nya.
    pub fn into_axum(self) -> ax_router<S> {
        let mut inner = self.inner;
        for layer_fn in self.layers {
            inner = layer_fn(inner);
        }
        inner
    }
}

impl<S> Default for Router<S>
where
    S: Clone + Send + Sync + 'static,
{
    fn default() -> Self {
        Self::new()
    }
}