Funny counter
This commit is contained in:
@@ -9,19 +9,43 @@ mod models;
|
|||||||
/// Axum router configuration with all routes and middleware
|
/// Axum router configuration with all routes and middleware
|
||||||
mod router;
|
mod router;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::{
|
||||||
|
Arc,
|
||||||
|
atomic::{AtomicI64, Ordering},
|
||||||
|
};
|
||||||
|
|
||||||
use axum::http::{
|
use axum::{
|
||||||
HeaderValue, Method,
|
Json,
|
||||||
header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
|
http::{
|
||||||
|
HeaderValue, Method, StatusCode,
|
||||||
|
header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use router::create_router;
|
use router::create_router;
|
||||||
|
use serde::Serialize;
|
||||||
use sqlx::{PgPool, postgres::PgPoolOptions};
|
use sqlx::{PgPool, postgres::PgPoolOptions};
|
||||||
use tower_http::cors::CorsLayer;
|
use tower_http::cors::CorsLayer;
|
||||||
|
|
||||||
use crate::env::Env;
|
use crate::env::Env;
|
||||||
|
|
||||||
|
static EASY_FIX_COUNT: AtomicI64 = AtomicI64::new(0);
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct CounterResp {
|
||||||
|
value: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_count() -> Json<CounterResp> {
|
||||||
|
let v = EASY_FIX_COUNT.load(Ordering::SeqCst);
|
||||||
|
Json(CounterResp { value: v })
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn increment() -> Result<Json<CounterResp>, StatusCode> {
|
||||||
|
let new = EASY_FIX_COUNT.fetch_add(1, Ordering::SeqCst) + 1;
|
||||||
|
Ok(Json(CounterResp { value: new }))
|
||||||
|
}
|
||||||
|
|
||||||
/// Shared application state passed to all route handlers.
|
/// Shared application state passed to all route handlers.
|
||||||
///
|
///
|
||||||
/// Contains the database connection pool and environment configuration.
|
/// Contains the database connection pool and environment configuration.
|
||||||
|
|||||||
@@ -8,13 +8,15 @@ use axum::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
AppState,
|
AppState,
|
||||||
cookie::validation::{validate_admin, validate_token},
|
cookie::validation::{validate_admin, validate_token},
|
||||||
|
get_count,
|
||||||
handlers::{
|
handlers::{
|
||||||
auth::{
|
auth::{
|
||||||
check_admin_exists, create_user, delete_user, get_current_user, get_user_by_id, get_users, login, logout,
|
check_admin_exists, create_user, delete_user, get_current_user, get_user_by_id,
|
||||||
setup_initial_admin, update_user,
|
get_users, login, logout, setup_initial_admin, update_user,
|
||||||
},
|
},
|
||||||
ticket::{create_ticket, delete_ticket, edit_ticket, get_ticket_by_id, get_tickets},
|
ticket::{create_ticket, delete_ticket, edit_ticket, get_ticket_by_id, get_tickets},
|
||||||
},
|
},
|
||||||
|
increment,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Creates the complete router with all API endpoints.
|
/// Creates the complete router with all API endpoints.
|
||||||
@@ -73,6 +75,7 @@ pub fn create_router(state: Arc<AppState>) -> Router {
|
|||||||
.route("/api/tickets/create", post(create_ticket))
|
.route("/api/tickets/create", post(create_ticket))
|
||||||
.route("/api/logout", get(logout))
|
.route("/api/logout", get(logout))
|
||||||
.route("/api/users/current", get(get_current_user))
|
.route("/api/users/current", get(get_current_user))
|
||||||
|
.route("/api/count", get(get_count).post(increment))
|
||||||
.layer(middleware::from_fn_with_state(
|
.layer(middleware::from_fn_with_state(
|
||||||
state.clone(),
|
state.clone(),
|
||||||
validate_token,
|
validate_token,
|
||||||
|
|||||||
Reference in New Issue
Block a user