mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 09:47:50 +03:00
Подключение к Postgres и тестовый эндпоинт авторизации
This commit is contained in:
86
src/main.rs
86
src/main.rs
@@ -1,39 +1,57 @@
|
||||
use crate::routes::auth::sign_in::sign_in;
|
||||
use crate::xls_downloader::basic_impl::BasicXlsDownloader;
|
||||
use crate::xls_downloader::interface::XLSDownloader;
|
||||
use schedule_parser::parse_xls;
|
||||
use std::{env, fs};
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use chrono::{DateTime, Utc};
|
||||
use diesel::{Connection, PgConnection};
|
||||
use dotenvy::dotenv;
|
||||
use schedule_parser::schema::ScheduleEntity;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
use std::env;
|
||||
|
||||
mod database;
|
||||
mod routes;
|
||||
mod xls_downloader;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
assert_ne!(args.len(), 1);
|
||||
|
||||
let mut downloader = BasicXlsDownloader::new();
|
||||
|
||||
downloader
|
||||
.set_url(args[1].to_string())
|
||||
.await
|
||||
.expect("Failed to set url");
|
||||
|
||||
let fetch_res = downloader.fetch(false).await.expect("Failed to fetch xls");
|
||||
|
||||
let (teachers, groups) = parse_xls(fetch_res.data.as_ref().unwrap());
|
||||
|
||||
fs::write(
|
||||
"./schedule.json",
|
||||
serde_json::to_string_pretty(&groups)
|
||||
.expect("Failed to serialize schedule")
|
||||
.as_bytes(),
|
||||
)
|
||||
.expect("Failed to write schedule");
|
||||
|
||||
fs::write(
|
||||
"./teachers.json",
|
||||
serde_json::to_string_pretty(&teachers)
|
||||
.expect("Failed to serialize teachers schedule")
|
||||
.as_bytes(),
|
||||
)
|
||||
.expect("Failed to write teachers schedule");
|
||||
pub struct AppState {
|
||||
downloader: Mutex<BasicXlsDownloader>,
|
||||
schedule: Mutex<
|
||||
Option<(
|
||||
String,
|
||||
DateTime<Utc>,
|
||||
(
|
||||
HashMap<String, ScheduleEntity>,
|
||||
HashMap<String, ScheduleEntity>,
|
||||
),
|
||||
)>,
|
||||
>,
|
||||
database: Mutex<PgConnection>,
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() {
|
||||
dotenv().ok();
|
||||
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
|
||||
let data = 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)),
|
||||
),
|
||||
});
|
||||
|
||||
HttpServer::new(move || {
|
||||
let schedule_scope = web::scope("/auth").service(sign_in);
|
||||
let api_scope = web::scope("/api/v1").service(schedule_scope);
|
||||
|
||||
App::new().app_data(data.clone()).service(api_scope)
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))
|
||||
.unwrap()
|
||||
.run()
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user