diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..0d99f5c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,7 @@ +repos: +- repo: https://github.com/doublify/pre-commit-rust + rev: v1.0 + hooks: + - id: fmt + - id: cargo-check + - id: clippy diff --git a/src/config.rs b/src/config.rs index 9551375..872cfe7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,15 +9,13 @@ pub struct Config { pub postgres_port: u32, pub postgres_db: String, - pub sentry_dsn: String + pub sentry_dsn: String, } - fn get_env(env: &'static str) -> String { std::env::var(env).unwrap_or_else(|_| panic!("Cannot get the {} env variable", env)) } - impl Config { pub fn load() -> Config { Config { @@ -29,12 +27,9 @@ impl Config { postgres_port: get_env("POSTGRES_PORT").parse().unwrap(), postgres_db: get_env("POSTGRES_DB"), - sentry_dsn: get_env("SENTRY_DSN") + sentry_dsn: get_env("SENTRY_DSN"), } } } - -pub static CONFIG: Lazy = Lazy::new(|| { - Config::load() -}); +pub static CONFIG: Lazy = Lazy::new(Config::load); diff --git a/src/db.rs b/src/db.rs index 8591834..108a8db 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,5 +1,4 @@ -use crate::{prisma::PrismaClient, config::CONFIG}; - +use crate::{config::CONFIG, prisma::PrismaClient}; pub async fn get_prisma_client() -> PrismaClient { let database_url: String = format!( diff --git a/src/main.rs b/src/main.rs index fa79b66..15892a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ pub mod db; pub mod prisma; pub mod views; -use sentry::{ClientOptions, types::Dsn, integrations::debug_images::DebugImagesIntegration}; +use sentry::{integrations::debug_images::DebugImagesIntegration, types::Dsn, ClientOptions}; use tracing::info; use std::{net::SocketAddr, str::FromStr}; diff --git a/src/prisma.rs b/src/prisma.rs index 913dac0..59a014b 100644 --- a/src/prisma.rs +++ b/src/prisma.rs @@ -1,7 +1,8 @@ // Code generated by Prisma Client Rust. DO NOT EDIT -pub static DATAMODEL_STR: &str = - include_str!("../prisma/schema.prisma"); +#![allow(clippy::all)] + +pub static DATAMODEL_STR: &str = include_str!("../prisma/schema.prisma"); static DATABASE_STR: &str = "postgresql"; pub async fn new_client() -> Result { PrismaClient::_builder().build().await @@ -15,15 +16,13 @@ pub async fn new_client_with_url( .await } pub mod service { - + use super::*; pub const NAME: &str = "Service"; pub mod id { use super::super::*; - - use super::{ - OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam, - }; + + use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam}; pub const NAME: &str = "id"; pub struct Set(pub i32); impl From for SetParam { @@ -91,10 +90,8 @@ pub mod service { } pub mod token { use super::super::*; - - use super::{ - OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam, - }; + + use super::{OrderByParam, SetParam, UncheckedSetParam, UniqueWhereParam, WhereParam}; pub const NAME: &str = "token"; pub struct Set(pub String); impl From for SetParam { @@ -158,10 +155,8 @@ pub mod service { } pub mod user { use super::super::*; - - use super::{ - OrderByParam, SetParam, UncheckedSetParam, WhereParam, - }; + + use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam}; pub const NAME: &str = "user"; pub struct Set(pub i64); impl From for SetParam { @@ -229,10 +224,8 @@ pub mod service { } pub mod status { use super::super::*; - - use super::{ - OrderByParam, SetParam, UncheckedSetParam, WhereParam, - }; + + use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam}; pub const NAME: &str = "status"; pub struct Set(pub String); impl From for SetParam { @@ -296,10 +289,8 @@ pub mod service { } pub mod created_time { use super::super::*; - - use super::{ - OrderByParam, SetParam, UncheckedSetParam, WhereParam, - }; + + use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam}; pub const NAME: &str = "created_time"; pub struct Set( pub ::prisma_client_rust::chrono::DateTime<::prisma_client_rust::chrono::FixedOffset>, @@ -401,10 +392,8 @@ pub mod service { } pub mod cache { use super::super::*; - - use super::{ - OrderByParam, SetParam, UncheckedSetParam, WhereParam, - }; + + use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam}; pub const NAME: &str = "cache"; pub struct Set(pub String); impl From for SetParam { @@ -468,10 +457,8 @@ pub mod service { } pub mod username { use super::super::*; - - use super::{ - OrderByParam, SetParam, UncheckedSetParam, WhereParam, - }; + + use super::{OrderByParam, SetParam, UncheckedSetParam, WhereParam}; pub const NAME: &str = "username"; pub struct Set(pub String); impl From for SetParam { diff --git a/src/views.rs b/src/views.rs index 94c1c4a..46588e2 100644 --- a/src/views.rs +++ b/src/views.rs @@ -1,22 +1,30 @@ -use axum::{Router, response::{Response, IntoResponse}, http::{StatusCode, self, Request}, middleware::{Next, self}, Extension, routing::{get, delete, post, patch}, Json, extract::Path}; +use axum::{ + extract::Path, + http::{self, Request, StatusCode}, + middleware::{self, Next}, + response::{IntoResponse, Response}, + routing::{delete, get, patch, post}, + Extension, Json, Router, +}; use axum_prometheus::PrometheusMetricLayer; use serde::Deserialize; -use tower_http::trace::{TraceLayer, self}; -use tracing::Level; use std::sync::Arc; +use tower_http::trace::{self, TraceLayer}; +use tracing::Level; -use crate::{config::CONFIG, db::get_prisma_client, prisma::{PrismaClient, service}}; - +use crate::{ + config::CONFIG, + db::get_prisma_client, + prisma::{service, PrismaClient}, +}; pub type Database = Extension>; - // -async fn get_services( - db: Database -) -> impl IntoResponse { - let services = db.service() +async fn get_services(db: Database) -> impl IntoResponse { + let services = db + .service() .find_many(vec![]) .order_by(service::id::order(prisma_client_rust::Direction::Asc)) .exec() @@ -26,11 +34,9 @@ async fn get_services( Json(services).into_response() } -async fn get_service( - Path(id): Path, - db: Database -) -> impl IntoResponse { - let service = db.service() +async fn get_service(Path(id): Path, db: Database) -> impl IntoResponse { + let service = db + .service() .find_unique(service::id::equals(id)) .exec() .await @@ -42,11 +48,9 @@ async fn get_service( } } -async fn delete_service( - Path(id): Path, - db: Database -) -> impl IntoResponse { - let service = db.service() +async fn delete_service(Path(id): Path, db: Database) -> impl IntoResponse { + let service = db + .service() .find_unique(service::id::equals(id)) .exec() .await @@ -54,13 +58,10 @@ async fn delete_service( match service { Some(v) => { - let _ = db.service() - .delete(service::id::equals(id)) - .exec() - .await; + let _ = db.service().delete(service::id::equals(id)).exec().await; Json(v).into_response() - }, + } None => StatusCode::NOT_FOUND.into_response(), } } @@ -74,13 +75,17 @@ pub struct CreateServiceData { pub username: String, } -async fn create_service( - db: Database, - Json(data): Json, -) -> impl IntoResponse { - let CreateServiceData { token, user, status, cache, username } = data; +async fn create_service(db: Database, Json(data): Json) -> impl IntoResponse { + let CreateServiceData { + token, + user, + status, + cache, + username, + } = data; - let service = db.service() + let service = db + .service() .create( token, user, @@ -88,7 +93,7 @@ async fn create_service( chrono::offset::Local::now().into(), cache, username, - vec![] + vec![], ) .exec() .await @@ -100,15 +105,11 @@ async fn create_service( async fn update_state( Path(id): Path, db: Database, - Json(state): Json + Json(state): Json, ) -> impl IntoResponse { - let service = db.service() - .update( - service::id::equals(id), - vec![ - service::status::set(state) - ] - ) + let service = db + .service() + .update(service::id::equals(id), vec![service::status::set(state)]) .exec() .await; @@ -121,15 +122,11 @@ async fn update_state( async fn update_cache( Path(id): Path, db: Database, - Json(cache): Json + Json(cache): Json, ) -> impl IntoResponse { - let service = db.service() - .update( - service::id::equals(id), - vec![ - service::cache::set(cache) - ] - ) + let service = db + .service() + .update(service::id::equals(id), vec![service::cache::set(cache)]) .exec() .await; @@ -141,9 +138,9 @@ async fn update_cache( // - async fn auth(req: Request, next: Next) -> Result { - let auth_header = req.headers() + let auth_header = req + .headers() .get(http::header::AUTHORIZATION) .and_then(|header| header.to_str().ok()); @@ -160,7 +157,6 @@ async fn auth(req: Request, next: Next) -> Result Ok(next.run(req).await) } - pub async fn get_router() -> Router { let client = Arc::new(get_prisma_client().await); @@ -173,22 +169,19 @@ pub async fn get_router() -> Router { .route("/", post(create_service)) .route("/:id/update_status", patch(update_state)) .route("/:id/update_cache", patch(update_cache)) - .layer(middleware::from_fn(auth)) .layer(Extension(client)) .layer(prometheus_layer); - let metric_router = Router::new() - .route("/metrics", get(|| async move { metric_handle.render() })); + let metric_router = + Router::new().route("/metrics", get(|| async move { metric_handle.render() })); Router::new() .nest("/", app_router) .nest("/", metric_router) .layer( TraceLayer::new_for_http() - .make_span_with(trace::DefaultMakeSpan::new() - .level(Level::INFO)) - .on_response(trace::DefaultOnResponse::new() - .level(Level::INFO)), + .make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO)) + .on_response(trace::DefaultOnResponse::new().level(Level::INFO)), ) }