mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
Регистрация и тесты эндпоинтов
This commit is contained in:
39
src/app_state.rs
Normal file
39
src/app_state.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use crate::xls_downloader::basic_impl::BasicXlsDownloader;
|
||||
use actix_web::web;
|
||||
use chrono::{DateTime, Utc};
|
||||
use diesel::{Connection, PgConnection};
|
||||
use schedule_parser::schema::ParseResult;
|
||||
use std::env;
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
|
||||
pub struct Schedule {
|
||||
pub etag: String,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
pub parsed_at: DateTime<Utc>,
|
||||
pub data: ParseResult,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
pub downloader: Mutex<BasicXlsDownloader>,
|
||||
pub schedule: Mutex<Option<Schedule>>,
|
||||
pub database: Mutex<PgConnection>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn connection(&self) -> MutexGuard<PgConnection> {
|
||||
self.database.lock().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn app_state() -> web::Data<AppState> {
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
|
||||
web::Data::new(AppState {
|
||||
downloader: Mutex::new(BasicXlsDownloader::new()),
|
||||
schedule: Mutex::new(None),
|
||||
database: Mutex::new(
|
||||
PgConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)),
|
||||
),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user