refactor: starting refactor to extract into rear crate

This commit is contained in:
Gabor Körber 2024-02-26 23:05:12 +01:00
parent 1dc24bf244
commit 7fe7047485
17 changed files with 114 additions and 12 deletions

30
Cargo.lock generated
View File

@ -1839,6 +1839,7 @@ dependencies = [
"minijinja-autoreload", "minijinja-autoreload",
"once_cell", "once_cell",
"pulldown-cmark", "pulldown-cmark",
"rear",
"rust-embed", "rust-embed",
"sea-orm", "sea-orm",
"serde", "serde",
@ -2371,6 +2372,35 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "rear"
version = "0.1.0"
dependencies = [
"anyhow",
"async-trait",
"axum 0.7.4",
"barrel",
"derive_builder",
"dotenvy",
"dunce",
"env_logger",
"log",
"mime_guess",
"minijinja",
"minijinja-autoreload",
"once_cell",
"pulldown-cmark",
"rust-embed",
"sea-orm",
"serde",
"serde_json",
"slug",
"sqlformat",
"tokio",
"tower-http",
"tracing",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.3.5" version = "0.3.5"

View File

@ -9,7 +9,7 @@ default-run = "miniweb"
[workspace] [workspace]
members = [".", "entity", "migration"] members = [".", "entity", "migration", "rear"]
[features] [features]
# https://github.com/rust-db/barrel/blob/master/guides/diesel-setup.md # https://github.com/rust-db/barrel/blob/master/guides/diesel-setup.md
@ -19,6 +19,7 @@ default = ["use_barrel"]
[dependencies] [dependencies]
strinto = { path = "./strinto" } strinto = { path = "./strinto" }
entity = { path = "./entity" } entity = { path = "./entity" }
rear = { path = "./rear" }
sea-orm = { version = "0.12.10", features = [ sea-orm = { version = "0.12.10", features = [
"runtime-tokio-native-tls", "runtime-tokio-native-tls",
"sqlx-postgres", "sqlx-postgres",

View File

@ -263,3 +263,18 @@ Make axum log requests!
There is another crate, that uses some MIME detection middleware for cache-control, called axum-cc, which could inspire middlewares here. There is another crate, that uses some MIME detection middleware for cache-control, called axum-cc, which could inspire middlewares here.
## Changing Rust Toolchain on Windows
change to GNU:
`rustup toolchain install stable-x86_64-pc-windows-gnu`
change back to MSVC:
`rustup toolchain install stable-x86_64-pc-windows-msvc`
or:
`rustup toolchain install stable-msvc`
activate toolchain with `rustup default`

51
rear/Cargo.toml Normal file
View File

@ -0,0 +1,51 @@
[package]
name = "rear"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
name = "rear"
path = "src/lib.rs"
[features]
# https://github.com/rust-db/barrel/blob/master/guides/diesel-setup.md
use_barrel = ["barrel", "sqlformat"]
default = ["use_barrel"]
[dependencies]
sea-orm = { version = "0.12.10", features = [
"runtime-tokio-native-tls",
"sqlx-postgres",
] }
sqlformat = { version = "0.2.2", optional = true }
anyhow = "1.0.75"
axum = "0.7"
barrel = { version = "0.7.0", optional = true, features = ["pg"] }
dotenvy = "0.15.7"
mime_guess = "2.0.4"
minijinja = { version = "1.0.11", features = [
"loader",
"builtins",
"urlencode",
"deserialization",
] }
minijinja-autoreload = "1.0.8"
once_cell = "1.18.0"
pulldown-cmark = "0.9.3"
rust-embed = { version = "8.0.0", features = [
"axum",
"tokio",
"include-exclude",
] }
serde = { version = "1.0.188", features = ["derive"] }
tokio = { version = "1.32.0", features = ["full"] }
dunce = "1.0.4"
log = "0.4.20"
env_logger = "0.11"
serde_json = "1.0.108"
slug = "0.1.5"
derive_builder = "0.13.0"
async-trait = "0.1.77"
tracing = "0.1.40"
tower-http = { version = "0.5.1", features = ["trace"] }

View File

@ -1,12 +1,10 @@
use axum::{routing::get, routing::post, Router}; use axum::{routing::get, routing::post, Router};
use crate::state::AppState;
pub mod domain; pub mod domain;
pub mod state; pub mod state;
pub mod views; pub mod views;
pub fn routes() -> Router<AppState> { pub fn route() -> Router<AppState> {
Router::new() Router::new()
.route("/", get(views::index).post(views::index_action)) .route("/", get(views::index).post(views::index_action))
.route("/app/:app", get(views::list_app)) .route("/app/:app", get(views::list_app))

View File

@ -1,10 +1,16 @@
use tokio::sync::Mutex; use tokio::sync::Mutex;
use super::domain::{AdminApp, AdminModel, AdminModelConfig, AdminRepository}; use super::domain::{AdminApp, AdminModel, AdminModelConfig, AdminRepository};
use crate::service::templates::Templates;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
pub type AdminState = Arc<AdminRegistry>; pub trait AdminState {
fn get_templates(&self) -> &Templates;
fn get_registry(&self) -> SharedRegistry;
}
pub type SharedRegistry = Arc<AdminRegistry>;
// main registry. // main registry.
pub struct AdminRegistry { pub struct AdminRegistry {

3
rear/src/lib.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod admin;
pub mod auth;
pub mod service;

View File

@ -1,10 +1,7 @@
mod admin;
mod admin_examples; mod admin_examples;
mod howto; mod howto;
mod service;
mod state; mod state;
use crate::service::{handlers, templates};
use crate::state::AppState; use crate::state::AppState;
use admin_examples::static_repository; use admin_examples::static_repository;
use admin_examples::user_repository; use admin_examples::user_repository;
@ -19,6 +16,7 @@ use axum::{
}; };
use dotenvy::dotenv; use dotenvy::dotenv;
use log::info; use log::info;
use rear::service::{handlers, templates};
use std::env; use std::env;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;

View File

@ -1,7 +1,7 @@
use axum::extract::FromRef; use axum::extract::FromRef;
use crate::admin::state::AdminState; use rear::admin::state::SharedRegistry;
use crate::service::templates; use rear::service::templates;
#[derive(Clone)] #[derive(Clone)]
pub struct AppState { pub struct AppState {
@ -15,8 +15,8 @@ impl FromRef<AppState> for templates::Templates {
} }
} }
impl FromRef<AppState> for AdminState { impl FromRef<AppState> for SharedRegistry {
fn from_ref(app_state: &AppState) -> AdminState { fn from_ref(app_state: &AppState) -> SharedRegistry {
app_state.admin.clone() app_state.admin.clone()
} }
} }