Added AppState.
This commit is contained in:
+5
-1
@@ -10,6 +10,7 @@ mod domain;
|
||||
mod error;
|
||||
mod repository;
|
||||
mod routes;
|
||||
mod state;
|
||||
|
||||
#[tokio::main]
|
||||
#[tracing::instrument]
|
||||
@@ -32,6 +33,9 @@ async fn main() -> anyhow::Result<()> {
|
||||
.await
|
||||
.context("failed to run database migrations")?;
|
||||
|
||||
let state = state::AppState::new(pool);
|
||||
let router = routes::create_router(state);
|
||||
|
||||
let host = env::var("HOST").context("HOST must be set")?;
|
||||
let port = env::var("PORT").context("PORT must be set")?;
|
||||
let addr = format!("{}:{}", host, port);
|
||||
@@ -40,7 +44,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
.context(format!("failed to bind to address: {}", addr))?;
|
||||
|
||||
tracing::info!(%addr, "starting server");
|
||||
axum::serve(listener, routes::create_router(pool).into_make_service())
|
||||
axum::serve(listener, router.into_make_service())
|
||||
.await
|
||||
.context("server stopped unexpectedly")?;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use axum::{http::StatusCode, routing::get};
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::{error::AppError, state::AppState};
|
||||
|
||||
mod account_members;
|
||||
mod accounts;
|
||||
@@ -15,7 +15,7 @@ mod user_app_secrets;
|
||||
mod user_apps;
|
||||
mod users;
|
||||
|
||||
pub fn create_router(pool: sqlx::PgPool) -> axum::Router {
|
||||
pub fn create_router(state: AppState) -> axum::Router {
|
||||
let api = axum::Router::new()
|
||||
.nest("/auth/passkey", auth_passkey::create_router())
|
||||
.nest("/auth/token", auth_token::create_router())
|
||||
@@ -40,5 +40,5 @@ pub fn create_router(pool: sqlx::PgPool) -> axum::Router {
|
||||
.nest("/api", api)
|
||||
.route("/health", get(async || StatusCode::NO_CONTENT))
|
||||
.fallback(async || AppError::NotFound)
|
||||
.with_state(pool)
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use crate::{
|
||||
repository::{self, RepositoryError},
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
pub fn create_router() -> axum::Router<crate::state::AppState> {
|
||||
axum::Router::new()
|
||||
.route("/", axum::routing::get(list_account_members))
|
||||
.route(
|
||||
|
||||
@@ -9,7 +9,7 @@ use super::{
|
||||
};
|
||||
use crate::{domain::AccountMember, error::AppError, repository};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
pub fn create_router() -> axum::Router<crate::state::AppState> {
|
||||
axum::Router::new()
|
||||
.route("/", axum::routing::get(list_accounts))
|
||||
.route(
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use axum::routing::post;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::{error::AppError, state::AppState};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
pub fn create_router() -> axum::Router<AppState> {
|
||||
axum::Router::new()
|
||||
.route("/options", post(get_challenge_options))
|
||||
.route("/verify", post(verify_challenge_response))
|
||||
|
||||
@@ -17,7 +17,7 @@ use crate::{
|
||||
const ACCESS_TOKEN_LIFETIME_SECONDS: i64 = 3600;
|
||||
const REFRESH_TOKEN_LIFETIME_SECONDS: i64 = 30 * 24 * 60 * 60;
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
pub fn create_router() -> axum::Router<crate::state::AppState> {
|
||||
axum::Router::new()
|
||||
.route("/", post(issue_token))
|
||||
.route("/revoke", post(revoke_token))
|
||||
|
||||
@@ -14,7 +14,7 @@ use super::{
|
||||
};
|
||||
use crate::{domain, error::AppError, repository};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
pub fn create_router() -> axum::Router<crate::state::AppState> {
|
||||
axum::Router::new()
|
||||
.route("/", axum::routing::get(list_scheduled_transactions))
|
||||
.route(
|
||||
|
||||
@@ -11,7 +11,7 @@ use super::{
|
||||
};
|
||||
use crate::{domain, error::AppError, repository};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
pub fn create_router() -> axum::Router<crate::state::AppState> {
|
||||
axum::Router::new()
|
||||
.route("/", axum::routing::get(list_transactions))
|
||||
.route(
|
||||
|
||||
@@ -16,7 +16,7 @@ use crate::{
|
||||
repository,
|
||||
};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
pub fn create_router() -> axum::Router<crate::state::AppState> {
|
||||
axum::Router::new()
|
||||
.route("/", axum::routing::get(list_user_app_secrets))
|
||||
.route(
|
||||
|
||||
@@ -9,7 +9,7 @@ use super::{
|
||||
};
|
||||
use crate::{error::AppError, repository};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
pub fn create_router() -> axum::Router<crate::state::AppState> {
|
||||
axum::Router::new()
|
||||
.route("/", axum::routing::get(list_user_apps))
|
||||
.route(
|
||||
|
||||
@@ -9,7 +9,7 @@ use super::{
|
||||
};
|
||||
use crate::{error::AppError, repository};
|
||||
|
||||
pub fn create_router() -> axum::Router<PgPool> {
|
||||
pub fn create_router() -> axum::Router<crate::state::AppState> {
|
||||
axum::Router::new()
|
||||
.route("/", axum::routing::get(list_users))
|
||||
.route(
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
use axum::extract::FromRef;
|
||||
use sqlx::PgPool;
|
||||
|
||||
#[derive(Clone, FromRef)]
|
||||
pub struct AppState {
|
||||
pub pool: PgPool,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user