A simple to-do app focused on tasks that can be completed within a specific time span.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

move routes to seperate module

tobinio 049df332 d674281c

+61 -44
+7 -2
api/src/auth.rs
··· 2 2 3 3 use axum::extract::FromRequestParts; 4 4 use http::{StatusCode, request::Parts}; 5 + use tracing::info; 5 6 use uuid::Uuid; 6 7 7 8 use crate::AppState; ··· 26 27 .and_then(|h| h.strip_prefix("Bearer ")) 27 28 .ok_or_else(|| (StatusCode::UNAUTHORIZED, "Authorization header missing"))?; 28 29 29 - let uuid = 30 - Uuid::from_str(auth_header).map_err(|_| (StatusCode::UNAUTHORIZED, "Invalid UUID"))?; 30 + info!(auth_header); 31 + 32 + let uuid = Uuid::from_str(auth_header).map_err(|e| { 33 + info!(?e); 34 + (StatusCode::UNAUTHORIZED, "Invalid UUID") 35 + })?; 31 36 32 37 Ok(User { uuid }) 33 38 }
+9 -42
api/src/main.rs
··· 1 - use axum::{Json, Router, extract::State, routing::get}; 1 + use axum::Router; 2 2 use color_eyre::eyre; 3 - use http::StatusCode; 4 3 use migration::{Migrator, MigratorTrait}; 5 - use sea_orm::{ 6 - ColumnTrait, ConnectionTrait, Database, DatabaseConnection, EntityTrait, QueryFilter, 7 - }; 4 + use sea_orm::{ConnectionTrait, Database, DatabaseConnection}; 8 5 use std::env; 9 6 use tower_http::trace::{self, TraceLayer}; 10 - use tracing::{Level, info}; 11 - use types::Tag as TagModel; 12 - 13 - use entities::prelude::*; 14 - 15 - use crate::{auth::User, entities::tag}; 7 + use tracing::Level; 16 8 17 9 mod auth; 18 10 mod entities; 11 + mod routes; 19 12 20 13 #[derive(Clone)] 21 14 struct AppState { ··· 35 28 36 29 let state = AppState { db_connection }; 37 30 38 - let app = Router::new() 39 - .route("/tags", get(list_tags)) 40 - .layer( 41 - TraceLayer::new_for_http() 42 - .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) 43 - .on_response(trace::DefaultOnResponse::new().level(Level::INFO)), 44 - ) 45 - .with_state(state); 31 + let app = Router::new().nest("/api", routes::routes(state)).layer( 32 + TraceLayer::new_for_http() 33 + .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) 34 + .on_response(trace::DefaultOnResponse::new().level(Level::INFO)), 35 + ); 46 36 47 37 let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{port}")) 48 38 .await ··· 66 56 67 57 Ok(connection) 68 58 } 69 - 70 - async fn list_tags( 71 - state: State<AppState>, 72 - user: User, 73 - ) -> Result<Json<Vec<TagModel>>, (StatusCode, &'static str)> { 74 - let tags = Tag::find() 75 - .filter(tag::Column::OwnerId.eq(user.uuid)) 76 - .all(&state.db_connection) 77 - .await 78 - .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "failed to fetch tags"))?; 79 - 80 - info!("{:?}", user.uuid); 81 - 82 - Ok(Json( 83 - tags.into_iter() 84 - .map(|tag| TagModel { 85 - uuid: tag.id, 86 - name: tag.name, 87 - color: tag.color, 88 - }) 89 - .collect(), 90 - )) 91 - }
+9
api/src/routes/mod.rs
··· 1 + use axum::Router; 2 + 3 + use crate::AppState; 4 + 5 + mod tags; 6 + 7 + pub fn routes(state: AppState) -> Router { 8 + Router::new().nest("/tags", tags::routes(state.clone())) 9 + }
+36
api/src/routes/tags.rs
··· 1 + use axum::{Json, Router, extract::State, routing::get}; 2 + use http::StatusCode; 3 + use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; 4 + use tracing::info; 5 + use types::Tag as TagModel; 6 + 7 + use crate::{AppState, auth::User}; 8 + 9 + use crate::entities::{prelude::*, tag}; 10 + 11 + pub fn routes(state: AppState) -> Router { 12 + Router::new().route("/", get(get_all)).with_state(state) 13 + } 14 + 15 + async fn get_all( 16 + state: State<AppState>, 17 + user: User, 18 + ) -> Result<Json<Vec<TagModel>>, (StatusCode, &'static str)> { 19 + let tags = Tag::find() 20 + .filter(tag::Column::OwnerId.eq(user.uuid)) 21 + .all(&state.db_connection) 22 + .await 23 + .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "failed to fetch tags"))?; 24 + 25 + info!("{:?}", user.uuid); 26 + 27 + Ok(Json( 28 + tags.into_iter() 29 + .map(|tag| TagModel { 30 + uuid: tag.id, 31 + name: tag.name, 32 + color: tag.color, 33 + }) 34 + .collect(), 35 + )) 36 + }