diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-08-23 22:19:32 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-08-23 22:19:32 +0200 |
commit | 7b9e9d2da5d85f23d43724fe2ca5012918ea54be (patch) | |
tree | 801ccae65d27fcf69bfca5db18265fbb7b5d7f21 | |
parent | cb1aa9668a967fe875e716469ccf936ed8693ad3 (diff) |
Add doc
-rw-r--r-- | src/config.rs | 9 | ||||
-rw-r--r-- | src/dates.rs | 1 | ||||
-rw-r--r-- | src/graphql/mutation.rs | 3 | ||||
-rw-r--r-- | src/graphql/query.rs | 2 | ||||
-rw-r--r-- | src/graphql/routes.rs | 2 | ||||
-rw-r--r-- | src/graphql/types/position.rs | 10 | ||||
-rw-r--r-- | src/graphql/types/user.rs | 6 | ||||
-rw-r--r-- | src/state.rs | 2 |
8 files changed, 35 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs index cbea92e..7eefff8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,14 +3,23 @@ use lazy_static::lazy_static; use serde::Deserialize; #[derive(Deserialize)] +/// App config pub struct Configuration { + /// Level of Rust logging pub rust_log: String, + + /// Database URL for Postgres pub database_url: String, + + /// JWT secret used for key creation pub jwt_secret: String, + + /// Host URL pub allowed_host: String, } impl Configuration { + /// A new configuration read from the env pub fn new() -> Result<Self, ConfigError> { let builder = config::Config::builder().add_source(config::Environment::default()); diff --git a/src/dates.rs b/src/dates.rs index 87e6f5e..9c007e9 100644 --- a/src/dates.rs +++ b/src/dates.rs @@ -3,6 +3,7 @@ use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Clone)] +/// A DateTime used as GraphQL type pub struct GraphQLDate(pub DateTime<Utc>); impl From<DateTime<Utc>> for GraphQLDate { diff --git a/src/graphql/mutation.rs b/src/graphql/mutation.rs index 9321653..9cee796 100644 --- a/src/graphql/mutation.rs +++ b/src/graphql/mutation.rs @@ -2,10 +2,12 @@ use crate::graphql::types::jwt; use crate::state::AppState; use async_graphql::{Context, Error, FieldResult, Object}; +/// Mutation struct pub struct Mutation; #[Object] impl Mutation { + /// Make GraphQL login async fn login<'ctx>( &self, ctx: &Context<'ctx>, @@ -25,6 +27,7 @@ impl Mutation { let id: Vec<i32> = rows.iter().map(|row| row.get(0)).collect(); if id.len() == 1 { + // Create a new claim using the found ID let claims = jwt::Claims::new(id[0]); let token = claims.get_token().unwrap(); Ok(jwt::AuthBody::new(token)) diff --git a/src/graphql/query.rs b/src/graphql/query.rs index 3d72b2e..9a7ac87 100644 --- a/src/graphql/query.rs +++ b/src/graphql/query.rs @@ -1,10 +1,12 @@ use crate::graphql::types::{position, user}; use async_graphql::{Context, Object}; +/// Query struct pub struct Query; #[Object] impl Query { + /// Returns the API version. It is like a "greet" function async fn api_version(&self) -> &'static str { "1.0" } diff --git a/src/graphql/routes.rs b/src/graphql/routes.rs index a566c65..4a3acfc 100644 --- a/src/graphql/routes.rs +++ b/src/graphql/routes.rs @@ -6,6 +6,8 @@ use axum::extract::Extension; use super::types::jwt::Authentication; +/// Handler for GraphQL route. +/// It executs the schema using the authorization as appdata pub async fn graphql_handler( schema: Extension<Schema<Query, Mutation, EmptySubscription>>, auth: Authentication, diff --git a/src/graphql/types/position.rs b/src/graphql/types/position.rs index 8b2d658..86e219c 100644 --- a/src/graphql/types/position.rs +++ b/src/graphql/types/position.rs @@ -8,6 +8,7 @@ use tokio_postgres::types::{to_sql_checked, FromSql, IsNull, ToSql, Type}; use super::user::find_user; #[derive(Enum, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)] +/// Enumeration which refers to the kind of moving activity pub enum MovingActivity { // "Car" of the doc InVehicle, @@ -67,6 +68,7 @@ impl ToSql for MovingActivity { } #[derive(Clone, Debug, Serialize, Deserialize)] +/// Position struct pub struct Position { pub id: i32, pub user_id: i32, @@ -103,10 +105,18 @@ impl Position { } } +/// Get positions from the database pub async fn get_positions<'ctx>( ctx: &Context<'ctx>, + + // Optional filter by user id. If not defined returns only available positions: + // If claimed user is admin returns everything, otherwise only positions linked to that user. user_id: Option<i32>, + + // Optional limit results limit: Option<i64>, + + // Optional offset results. It should be used with limit field. offset: Option<i64>, ) -> Result<Option<Vec<Position>>, String> { let state = ctx.data::<AppState>().expect("Can't connect to db"); diff --git a/src/graphql/types/user.rs b/src/graphql/types/user.rs index 4ecf086..96fcf40 100644 --- a/src/graphql/types/user.rs +++ b/src/graphql/types/user.rs @@ -6,6 +6,7 @@ use tokio_postgres::Client; use super::jwt::Authentication; #[derive(Clone, Debug, Serialize, Deserialize)] +/// User struct pub struct User { pub id: i32, pub email: String, @@ -32,9 +33,13 @@ impl User { } } +/// Get users from the database pub async fn get_users<'ctx>( ctx: &Context<'ctx>, + + // Optional limit results limit: Option<i64>, + // Optional offset results. It should be used with limit field. offset: Option<i64>, ) -> Result<Option<Vec<User>>, String> { let state = ctx.data::<AppState>().expect("Can't connect to db"); @@ -66,6 +71,7 @@ pub async fn get_users<'ctx>( } } +/// Find an user with id = `id` using the PostgreSQL `client` pub async fn find_user(client: &Client, id: i32) -> Result<User, AppError> { let rows = client .query( diff --git a/src/state.rs b/src/state.rs index 876a5f4..700f6ab 100644 --- a/src/state.rs +++ b/src/state.rs @@ -3,6 +3,8 @@ use std::sync::Arc; use tokio_postgres::Client; #[derive(Clone)] +/// State application shared through Axum pub struct AppState { + /// PostgreSQL client synced via Arc pub client: Arc<Client>, } |