Updates & axum-controller testing

This commit is contained in:
Tristan D. 2025-03-08 01:39:36 +01:00
parent 4fa9f08cc3
commit 80d34a065b
Signed by: tristan
SSH key fingerprint: SHA256:9oFM1J63hYWJjCnLG6C0fxBS15rwNcWwdQNMOHYKJ/4
7 changed files with 388 additions and 377 deletions

622
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -21,7 +21,7 @@ panic = "abort"
[workspace] [workspace]
members = ["darm_test", "llama_forge_rs", "llama_proxy_man", "redvault_el_rs"] members = ["darm_test", "llama_forge_rs", "llama_proxy_man", "redvault_el_rs"]
resolver = "2" resolver = "3"
[workspace.package] [workspace.package]
authors = ["Tristan Druyen"] authors = ["Tristan Druyen"]

View file

@ -11,6 +11,8 @@ edition.workspace = true
[dependencies] [dependencies]
axum = { version = "0.8", features = ["http2"] } axum = { version = "0.8", features = ["http2"] }
axum-controller = { version = "0.2.0", path = "../../axum-controller/axum-controller" }
axum-controller-macros = { version = "0.2.0", path = "../../axum-controller/axum-controller-macros" }
axum-typed-routing = { git = "https://github.com/jvdwrf/axum-typed-routing", version = "0.2.0" } axum-typed-routing = { git = "https://github.com/jvdwrf/axum-typed-routing", version = "0.2.0" }
datastar = { git = "https://github.com/starfederation/datastar.git", version = "0.1.0" } datastar = { git = "https://github.com/starfederation/datastar.git", version = "0.1.0" }
maud = { version = "0.27.0", features = ["axum"] } maud = { version = "0.27.0", features = ["axum"] }

View file

@ -1,6 +1,6 @@
* Planning (newest) * Planning (newest)
- maud vs hypertext vs minijinja - maud vs hypertext vs minijinja
- try porting to hypertext ? - try porting to hypertext::maud !
* Todos * Todos
** Starter boilerplate ** Starter boilerplate
*** [X] Server: Axum *** [X] Server: Axum

View file

@ -7,37 +7,46 @@ use axum::{
response::{Html, IntoResponse}, response::{Html, IntoResponse},
routing::get, routing::get,
}; };
use axum_typed_routing::{route, TypedRouter}; use axum_controller::*;
use maud::{html, Markup}; use maud::{html, Markup};
use rust_embed::RustEmbed; use rust_embed::RustEmbed;
#[allow(unused)] struct TestController {}
#[route(GET "/item/:id?amount&offset")]
async fn item_handler(
id: u32,
amount: Option<u32>,
offset: Option<u32>,
State(state): State<AppState>,
// Json(json): Json<u32>,
) -> Markup {
// todo!("handle request")
html! { #[controller(
h1 { "Item" } state = AppState
p { )]
(format!("{id:?} {amount:?} {offset:?} {state:?}")) impl TestController {
#[allow(unused)]
#[route(GET "/item/:id?amount&offset")]
async fn item_handler(
id: u32,
amount: Option<u32>,
offset: Option<u32>,
State(state): State<AppState>,
// Json(json): Json<u32>,
) -> Markup {
// todo!("handle request")
html! {
h1 { "Item" }
p {
(format!("{id:?} {amount:?} {offset:?} {state:?}"))
}
} }
} }
} }
// We use a wildcard matcher ("/dist/*file") to match against everything struct DistController;
// within our defined assets directory. This is the directory on our Asset
// struct below, where folder = "examples/public/".
#[route(GET "/dist/*path")]
async fn static_handler(path: String, _: State<AppState>) -> impl IntoResponse {
let path = path.trim_start_matches('/').to_string();
StaticFile(path) #[controller(state=AppState, path="/dist")]
impl DistController {
#[route(GET "/*path")]
async fn static_handler(path: String, _: State<AppState>) -> impl IntoResponse {
let path = path.trim_start_matches('/').to_string();
StaticFile(path)
}
} }
fn markup_404(uri: String) -> Markup { fn markup_404(uri: String) -> Markup {
@ -69,10 +78,6 @@ async fn handle_405() -> impl IntoResponse {
(StatusCode::METHOD_NOT_ALLOWED, markup_405()).into_response() (StatusCode::METHOD_NOT_ALLOWED, markup_405()).into_response()
} }
#[derive(RustEmbed)]
#[folder = "public/"]
struct Asset;
pub struct StaticFile<T>(pub T); pub struct StaticFile<T>(pub T);
impl<T> IntoResponse for StaticFile<T> impl<T> IntoResponse for StaticFile<T>
@ -82,6 +87,9 @@ where
fn into_response(self) -> Response<Body> { fn into_response(self) -> Response<Body> {
let path = self.0.into(); let path = self.0.into();
tracing::debug!(?path); tracing::debug!(?path);
#[derive(RustEmbed)]
#[folder = "public/"]
struct Asset;
match Asset::get(path.as_str()) { match Asset::get(path.as_str()) {
Some(content) => { Some(content) => {
@ -117,14 +125,19 @@ struct AppState {
mj_env: Arc<minijinja::Environment<'static>>, mj_env: Arc<minijinja::Environment<'static>>,
} }
#[route(GET "/")] struct JinjaController;
async fn jinja_index_handler(state: State<AppState>) -> impl IntoResponse {
RenderTemplate::new("/".to_string(), state.mj_env.clone())
}
#[route(GET "/*path")] #[controller(state=AppState)]
async fn jinja_index_handler_path(path: String, state: State<AppState>) -> impl IntoResponse { impl JinjaController {
RenderTemplate::new(path, state.mj_env.clone()) #[route(GET "/")]
async fn jinja_index_handler(state: State<AppState>) -> impl IntoResponse {
RenderTemplate::new("/".to_string(), state.mj_env.clone())
}
#[route(GET "/*path")]
async fn jinja_index_handler_path(path: String, state: State<AppState>) -> impl IntoResponse {
RenderTemplate::new(path, state.mj_env.clone())
}
} }
struct RenderTemplate { struct RenderTemplate {
@ -276,15 +289,10 @@ async fn main() {
// TODO pick free port/config // TODO pick free port/config
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap(); let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap();
let ui_router = axum::Router::new().typed_route(item_handler);
let router: axum::Router = axum::Router::new() let router: axum::Router = axum::Router::new()
// .route("/", get(jinja_index_handler)) .merge(JinjaController::into_router(app_state.clone()))
.merge(ui_router.clone()) .merge(TestController::into_router(app_state.clone()))
.nest("/ui", ui_router) .merge(DistController::into_router(app_state.clone()))
.typed_route(jinja_index_handler)
.typed_route(jinja_index_handler_path)
.typed_route(static_handler)
.fallback_service(get(handle_404)) .fallback_service(get(handle_404))
.method_not_allowed_fallback(handle_405) .method_not_allowed_fallback(handle_405)
.with_state(app_state); .with_state(app_state);

43
flake.lock generated
View file

@ -23,11 +23,11 @@
"nixpkgs-lib": "nixpkgs-lib" "nixpkgs-lib": "nixpkgs-lib"
}, },
"locked": { "locked": {
"lastModified": 1735774679, "lastModified": 1741352980,
"narHash": "sha256-soePLBazJk0qQdDVhdbM98vYdssfs3WFedcq+raipRI=", "narHash": "sha256-+u2UunDA4Cl5Fci3m7S643HzKmIDAe+fiXrLqYsR2fs=",
"owner": "hercules-ci", "owner": "hercules-ci",
"repo": "flake-parts", "repo": "flake-parts",
"rev": "f2f7418ce0ab4a5309a4596161d154cfc877af66", "rev": "f4330d22f1c5d2ba72d3d22df5597d123fdb60a9",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -64,16 +64,16 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1739180049, "lastModified": 1741373670,
"narHash": "sha256-LjIIeMqGJVv6rHsnf5tWhejiysD3LIsOplwE+M3YeZg=", "narHash": "sha256-BPawysk5uWq9W4K/r+ZJr3AliPemVQqKOuSD7U4Px+Y=",
"owner": "ggerganov", "owner": "ggerganov",
"repo": "llama.cpp", "repo": "llama.cpp",
"rev": "d7b31a9d84297b493a61c9a8ee3a458e7ccc64a7", "rev": "7ab364390f92b0b8d83f69821a536b424838f3f8",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "ggerganov", "owner": "ggerganov",
"ref": "b4681", "ref": "b4855",
"repo": "llama.cpp", "repo": "llama.cpp",
"type": "github" "type": "github"
} }
@ -96,28 +96,31 @@
}, },
"nixpkgs-lib": { "nixpkgs-lib": {
"locked": { "locked": {
"lastModified": 1735774519, "lastModified": 1740877520,
"narHash": "sha256-CewEm1o2eVAnoqb6Ml+Qi9Gg/EfNAxbRx1lANGVyoLI=", "narHash": "sha256-oiwv/ZK/2FhGxrCkQkB83i7GnWXPPLzoqFHpDD3uYpk=",
"type": "tarball", "owner": "nix-community",
"url": "https://github.com/NixOS/nixpkgs/archive/e9b51731911566bbf7e4895475a87fe06961de0b.tar.gz" "repo": "nixpkgs.lib",
"rev": "147dee35aab2193b174e4c0868bd80ead5ce755c",
"type": "github"
}, },
"original": { "original": {
"type": "tarball", "owner": "nix-community",
"url": "https://github.com/NixOS/nixpkgs/archive/e9b51731911566bbf7e4895475a87fe06961de0b.tar.gz" "repo": "nixpkgs.lib",
"type": "github"
} }
}, },
"nixpkgs_2": { "nixpkgs_2": {
"locked": { "locked": {
"lastModified": 1738758495, "lastModified": 1741246872,
"narHash": "sha256-CZ8T4vP3ag2hwkpSZjatxJb55ouszvmnWw09qxGW9TU=", "narHash": "sha256-Q6pMP4a9ed636qilcYX8XUguvKl/0/LGXhHcRI91p0U=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "ceaea203f3ae1787b1bd13f021f686391696fc5b", "rev": "10069ef4cf863633f57238f179a0297de84bd8d3",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixos-unstable-small", "ref": "nixos-unstable",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }
@ -170,11 +173,11 @@
"nixpkgs": "nixpkgs_3" "nixpkgs": "nixpkgs_3"
}, },
"locked": { "locked": {
"lastModified": 1738290352, "lastModified": 1741314698,
"narHash": "sha256-YKOHUmc0Clm4tMV8grnxYL4IIwtjTayoq/3nqk0QM7k=", "narHash": "sha256-6Yp0CTwAY/jq/F81Sa8NM0Zi1EwxAdASO6y4A5neGuc=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "b031b584125d33d23a0182f91ddbaf3ab4880236", "rev": "4e9af61c1a631886cdc7e13032af4fc9e75bb76b",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -13,7 +13,7 @@
]; ];
}; };
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay.url = "github:oxalica/rust-overlay"; rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
flake-parts.url = "github:hercules-ci/flake-parts"; flake-parts.url = "github:hercules-ci/flake-parts";
@ -23,7 +23,7 @@
flake = false; flake = false;
}; };
llama-cpp = { llama-cpp = {
url = "github:ggerganov/llama.cpp/b4681"; url = "github:ggerganov/llama.cpp/b4855";
inputs.nixpkgs.follows = "nixpkgs"; inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-parts.follows = "flake-parts"; inputs.flake-parts.follows = "flake-parts";
}; };