Funny counter
This commit is contained in:
@@ -9,19 +9,43 @@ mod models;
|
||||
/// Axum router configuration with all routes and middleware
|
||||
mod router;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicI64, Ordering},
|
||||
};
|
||||
|
||||
use axum::http::{
|
||||
HeaderValue, Method,
|
||||
header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
|
||||
use axum::{
|
||||
Json,
|
||||
http::{
|
||||
HeaderValue, Method, StatusCode,
|
||||
header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
|
||||
},
|
||||
};
|
||||
use dotenv::dotenv;
|
||||
use router::create_router;
|
||||
use serde::Serialize;
|
||||
use sqlx::{PgPool, postgres::PgPoolOptions};
|
||||
use tower_http::cors::CorsLayer;
|
||||
|
||||
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.
|
||||
///
|
||||
/// Contains the database connection pool and environment configuration.
|
||||
|
||||
@@ -8,13 +8,15 @@ use axum::{
|
||||
use crate::{
|
||||
AppState,
|
||||
cookie::validation::{validate_admin, validate_token},
|
||||
get_count,
|
||||
handlers::{
|
||||
auth::{
|
||||
check_admin_exists, create_user, delete_user, get_current_user, get_user_by_id, get_users, login, logout,
|
||||
setup_initial_admin, update_user,
|
||||
check_admin_exists, create_user, delete_user, get_current_user, get_user_by_id,
|
||||
get_users, login, logout, setup_initial_admin, update_user,
|
||||
},
|
||||
ticket::{create_ticket, delete_ticket, edit_ticket, get_ticket_by_id, get_tickets},
|
||||
},
|
||||
increment,
|
||||
};
|
||||
|
||||
/// 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/logout", get(logout))
|
||||
.route("/api/users/current", get(get_current_user))
|
||||
.route("/api/count", get(get_count).post(increment))
|
||||
.layer(middleware::from_fn_with_state(
|
||||
state.clone(),
|
||||
validate_token,
|
||||
|
||||
Reference in New Issue
Block a user