feature: extracting admin and services to
This commit is contained in:
58
src/embed.rs
Normal file
58
src/embed.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{header, StatusCode, Uri},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use rust_embed::RustEmbed;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct EmbeddedFile<E, T> {
|
||||
pub path: T,
|
||||
embed: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E, T> EmbeddedFile<E, T> {
|
||||
pub fn get(path: T) -> Self {
|
||||
Self {
|
||||
path,
|
||||
embed: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E, T> IntoResponse for EmbeddedFile<E, T>
|
||||
where
|
||||
E: RustEmbed,
|
||||
T: AsRef<str>,
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
let path: &str = self.path.as_ref();
|
||||
match E::get(path) {
|
||||
Some(content) => {
|
||||
let body = Body::from(content.data);
|
||||
let mime = mime_guess::from_path(path).first_or_octet_stream();
|
||||
Response::builder()
|
||||
.header(header::CONTENT_TYPE, mime.as_ref())
|
||||
.body(body)
|
||||
.unwrap()
|
||||
}
|
||||
None => StatusCode::NOT_FOUND.into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(RustEmbed)]
|
||||
#[folder = "static"]
|
||||
struct StaticDir;
|
||||
type StaticFile<T> = EmbeddedFile<StaticDir, T>;
|
||||
|
||||
// usage: .route_service("/static/*file", static_handler.into_service())
|
||||
pub async fn static_handler(uri: Uri) -> impl IntoResponse {
|
||||
let mut path = uri.path().trim_start_matches('/').to_string();
|
||||
|
||||
if path.starts_with("static/") {
|
||||
path = path.replace("static/", "");
|
||||
}
|
||||
|
||||
StaticFile::get(path)
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use axum::{extract::State, response::IntoResponse, Form};
|
||||
|
||||
use crate::service::templates;
|
||||
use rear::service::templates;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
||||
@@ -1,4 +1 @@
|
||||
pub mod admin;
|
||||
pub mod auth;
|
||||
pub mod service;
|
||||
pub mod state;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
mod admin_examples;
|
||||
mod embed;
|
||||
mod howto;
|
||||
mod state;
|
||||
|
||||
@@ -16,6 +17,7 @@ use axum::{
|
||||
};
|
||||
use dotenvy::dotenv;
|
||||
use log::info;
|
||||
use rear::admin;
|
||||
use rear::service::{handlers, templates};
|
||||
use std::env;
|
||||
use std::net::SocketAddr;
|
||||
@@ -66,7 +68,7 @@ async fn main() {
|
||||
//.merge(admin_router)
|
||||
.nest("/admin", admin::routes())
|
||||
.nest("/howto", howto::routes())
|
||||
.route_service("/static/*file", handlers::static_handler.into_service())
|
||||
.route_service("/static/*file", embed::static_handler.into_service())
|
||||
.fallback(handlers::not_found_handler)
|
||||
.with_state(state)
|
||||
.layer(
|
||||
|
||||
14
src/state.rs
14
src/state.rs
@@ -1,12 +1,12 @@
|
||||
use axum::extract::FromRef;
|
||||
|
||||
use rear::admin::state::SharedRegistry;
|
||||
use rear::admin::state::{AdminState, SharedRegistry};
|
||||
use rear::service::templates;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
pub templates: templates::Templates,
|
||||
pub admin: AdminState,
|
||||
pub admin: SharedRegistry,
|
||||
}
|
||||
|
||||
impl FromRef<AppState> for templates::Templates {
|
||||
@@ -20,3 +20,13 @@ impl FromRef<AppState> for SharedRegistry {
|
||||
app_state.admin.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl AdminState for AppState {
|
||||
fn get_templates(&self) -> &templates::Templates {
|
||||
&self.templates
|
||||
}
|
||||
|
||||
fn get_registry(&self) -> SharedRegistry {
|
||||
self.admin.clone()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user