Move app state
This commit is contained in:
parent
cd7fe5fe39
commit
0943681702
7 changed files with 15 additions and 20 deletions
|
|
@ -1,12 +1,11 @@
|
|||
mod app_state;
|
||||
mod config;
|
||||
mod consts;
|
||||
mod database;
|
||||
mod errors;
|
||||
mod server;
|
||||
mod utils;
|
||||
|
||||
use anyhow::{Context as _, Result};
|
||||
use app_state::AppState;
|
||||
use errors::{init_error, SyncServerError};
|
||||
use log::info;
|
||||
use server::create_server;
|
||||
|
|
@ -34,12 +33,7 @@ async fn main() -> Result<(), SyncServerError> {
|
|||
env!("CARGO_PKG_VERSION")
|
||||
);
|
||||
|
||||
let app_state = AppState::try_new()
|
||||
.await
|
||||
.context("Failed to initialise app state")
|
||||
.map_err(init_error)?;
|
||||
|
||||
create_server(app_state)
|
||||
create_server()
|
||||
.await
|
||||
.context("Failed to start server")
|
||||
.map_err(init_error)
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use aide::{
|
|||
transform::TransformOpenApi,
|
||||
};
|
||||
use anyhow::{anyhow, Context as _, Result};
|
||||
use app_state::AppState;
|
||||
use axum::{
|
||||
extract::{DefaultBodyLimit, Request},
|
||||
http::{self, HeaderValue, Method},
|
||||
|
|
@ -28,10 +29,8 @@ use tower_http::{
|
|||
};
|
||||
use tracing::{info_span, Level};
|
||||
|
||||
use crate::{
|
||||
app_state::AppState,
|
||||
errors::{not_found_error, SerializedError},
|
||||
};
|
||||
use crate::errors::{not_found_error, SerializedError};
|
||||
mod app_state;
|
||||
mod auth;
|
||||
mod create_document;
|
||||
mod delete_document;
|
||||
|
|
@ -42,10 +41,14 @@ mod requests;
|
|||
mod responses;
|
||||
mod update_document;
|
||||
|
||||
pub async fn create_server(app_state: AppState) -> Result<()> {
|
||||
pub async fn create_server() -> Result<()> {
|
||||
aide::gen::on_error(|err| error!("{err}"));
|
||||
aide::gen::extract_schemas(true);
|
||||
|
||||
let app_state = AppState::try_new()
|
||||
.await
|
||||
.context("Failed to initialise app state")?;
|
||||
|
||||
let address = format!(
|
||||
"{}:{}",
|
||||
&app_state.config.server.host, &app_state.config.server.port
|
||||
|
|
|
|||
20
backend/sync_server/src/server/app_state.rs
Normal file
20
backend/sync_server/src/server/app_state.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use anyhow::Result;
|
||||
|
||||
use crate::{config::Config, consts::CONFIG_PATH, database::Database};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AppState {
|
||||
pub config: Config,
|
||||
pub database: Database,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub async fn try_new() -> Result<Self> {
|
||||
let path = std::path::Path::new(CONFIG_PATH);
|
||||
|
||||
let config = Config::read_or_create(path).await?;
|
||||
let database = Database::try_new(&config.database).await?;
|
||||
|
||||
Ok(Self { config, database })
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
use super::app_state::AppState;
|
||||
use crate::{
|
||||
app_state::AppState,
|
||||
config::user_config::User,
|
||||
errors::{unauthorized_error, SyncServerError},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -10,9 +10,8 @@ use axum_extra::{
|
|||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::auth::auth;
|
||||
use super::{app_state::AppState, auth::auth};
|
||||
use crate::{
|
||||
app_state::AppState,
|
||||
database::models::{DocumentId, DocumentVersion, VaultId},
|
||||
errors::{not_found_error, server_error, SyncServerError},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -9,9 +9,8 @@ use axum_extra::{
|
|||
use schemars::JsonSchema;
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::{auth::auth, responses::FetchLatestDocumentsResponse};
|
||||
use super::{app_state::AppState, auth::auth, responses::FetchLatestDocumentsResponse};
|
||||
use crate::{
|
||||
app_state::AppState,
|
||||
database::models::{VaultId, VaultUpdateId},
|
||||
errors::{server_error, SyncServerError},
|
||||
};
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ use axum_extra::{
|
|||
TypedHeader,
|
||||
};
|
||||
|
||||
use super::{auth::auth, responses::PingResponse};
|
||||
use crate::{app_state::AppState, errors::SyncServerError};
|
||||
use super::{app_state::AppState, auth::auth, responses::PingResponse};
|
||||
use crate::errors::SyncServerError;
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn ping(
|
||||
|
|
|
|||
Loading…
Reference in a new issue