feat: fixup "add a DEBUG page to the UI with a database usage example"
This commit is contained in:
parent
777b7f2425
commit
90e79c1fa4
@ -15,4 +15,5 @@ anyhow = "1.0"
|
||||
dotenv = "0.15"
|
||||
sea-orm-cli = "1.0.1"
|
||||
sea-orm = "1.0.1"
|
||||
serde = { version = "1.0.210", features = ["derive"] }
|
||||
thiserror = "1.0"
|
||||
|
@ -113,5 +113,5 @@ sea-orm-cli migrate up
|
||||
### Génération des entitées
|
||||
|
||||
```bash
|
||||
sea-orm-cli generate entity -o entity/src/entities
|
||||
sea-orm-cli generate entity -o entity/src/entities --with-serde both
|
||||
```
|
@ -5,7 +5,7 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.89"
|
||||
axum = "0.7.6"
|
||||
axum = { version = "0.7.6", features = ["macros"] }
|
||||
listenfd = "1.0.1"
|
||||
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
|
||||
|
||||
@ -15,6 +15,7 @@ sea-orm = { workspace = true, features = [
|
||||
"runtime-tokio-rustls",
|
||||
"macros",
|
||||
] }
|
||||
serde.workspace = true
|
||||
thiserror.workspace = true
|
||||
|
||||
entity = { path = "../../entity" }
|
||||
|
48
crates/backend/src/api/debug.rs
Normal file
48
crates/backend/src/api/debug.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use axum::{extract::State, routing, Json};
|
||||
use sea_orm::*;
|
||||
use serde::Serialize;
|
||||
|
||||
use ::entity::{debug, debug::Entity as DebugEntity};
|
||||
|
||||
use crate::{AppError, AppState};
|
||||
|
||||
// DATABASE DEBUG CONTROLLERS
|
||||
|
||||
async fn get_debug_entries(db: &DatabaseConnection) -> Result<Vec<debug::Model>, DbErr> {
|
||||
DebugEntity::find().all(db).await
|
||||
}
|
||||
|
||||
async fn add_random_debug_entry(State(AppState { db_connection }): State<AppState>) {
|
||||
let random_entry = debug::ActiveModel {
|
||||
title: Set("Random title".to_string()),
|
||||
text: Set("Random text".to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
random_entry.insert(&db_connection).await.unwrap();
|
||||
}
|
||||
|
||||
// API HANDLER
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
struct DebugResponse {
|
||||
db_ping_status: bool,
|
||||
entries: Vec<debug::Model>,
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
async fn debug(
|
||||
State(AppState { db_connection }): State<AppState>,
|
||||
) -> Result<Json<DebugResponse>, AppError> {
|
||||
let db_ping_status = db_connection.ping().await.is_ok();
|
||||
let debug_entries = get_debug_entries(&db_connection).await?;
|
||||
Ok(Json(DebugResponse {
|
||||
db_ping_status,
|
||||
entries: debug_entries,
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn get_routes() -> axum::Router<crate::AppState> {
|
||||
axum::Router::new()
|
||||
.route("/", routing::get(debug))
|
||||
.route("/add_random", routing::post(add_random_debug_entry))
|
||||
}
|
9
crates/backend/src/api/mod.rs
Normal file
9
crates/backend/src/api/mod.rs
Normal file
@ -0,0 +1,9 @@
|
||||
use axum::Router;
|
||||
|
||||
use crate::AppState;
|
||||
|
||||
mod debug;
|
||||
|
||||
pub fn get_routes() -> Router<AppState> {
|
||||
Router::new().nest("/debug", debug::get_routes())
|
||||
}
|
@ -7,6 +7,7 @@ use thiserror::Error;
|
||||
|
||||
use ::utils::config::{load_config, ConfigError};
|
||||
|
||||
mod api;
|
||||
mod db;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
@ -27,10 +28,13 @@ pub struct AppState {
|
||||
|
||||
pub async fn get_router() -> Result<Router, DbErr> {
|
||||
let db_connection = db::get_connection().await?;
|
||||
let state: AppState = AppState { db_connection };
|
||||
|
||||
Ok(Router::new()
|
||||
.route("/", get(|| async { "Hello, world!" }))
|
||||
.fallback(fallback))
|
||||
.merge(api::get_routes())
|
||||
.fallback(fallback)
|
||||
.with_state(state))
|
||||
}
|
||||
|
||||
async fn fallback(uri: Uri) -> (StatusCode, String) {
|
||||
|
@ -9,6 +9,7 @@ path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
sea-orm.workspace = true
|
||||
serde.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
sea-orm-cli.workspace = true
|
||||
|
@ -1,8 +1,9 @@
|
||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.1
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "debug")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
|
Loading…
Reference in New Issue
Block a user