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