Add server initialisation
This commit is contained in:
parent
c2dceb0be4
commit
01f0c873a9
2 changed files with 46 additions and 0 deletions
19
backend/sync_server/src/app_state.rs
Normal file
19
backend/sync_server/src/app_state.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
use crate::{config::Config, consts::CONFIG_PATH, database::Database};
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
#[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(path).await?;
|
||||||
|
let database = Database::try_new(&config.database).await?;
|
||||||
|
|
||||||
|
Ok(Self { config, database })
|
||||||
|
}
|
||||||
|
}
|
||||||
27
backend/sync_server/src/main.rs
Normal file
27
backend/sync_server/src/main.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
mod app_state;
|
||||||
|
mod config;
|
||||||
|
mod consts;
|
||||||
|
mod database;
|
||||||
|
mod errors;
|
||||||
|
mod server;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use app_state::AppState;
|
||||||
|
use errors::{init_error, SyncServerError};
|
||||||
|
use server::create_server;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), SyncServerError> {
|
||||||
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
|
let app_state = AppState::try_new()
|
||||||
|
.await
|
||||||
|
.context("Failed to initialise app state")
|
||||||
|
.map_err(init_error)?;
|
||||||
|
|
||||||
|
create_server(app_state)
|
||||||
|
.await
|
||||||
|
.context("Failed to start server")
|
||||||
|
.map_err(init_error)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue