update logging system and send websocket message on owner creation via api
This commit is contained in:
parent
3e1c744db1
commit
c016fc915b
7 changed files with 97 additions and 53 deletions
33
src/main.rs
33
src/main.rs
|
|
@ -1,16 +1,16 @@
|
|||
use std::{net::SocketAddr, sync::Arc, time::Duration};
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
|
||||
use axum::{extract::State, http::HeaderMap, routing::get};
|
||||
use reqwest::{header::USER_AGENT};
|
||||
use sea_orm::{ConnectionTrait, Database, DatabaseConnection, EntityTrait, PaginatorTrait, Schema};
|
||||
use tokio::{sync::broadcast::{self, Sender}, task, time};
|
||||
use tokio::{sync::broadcast::{self, Sender}};
|
||||
use utoipa::openapi::{ContactBuilder, InfoBuilder, LicenseBuilder};
|
||||
use utoipa_axum::router::OpenApiRouter;
|
||||
use utoipa_redoc::{Redoc, Servable};
|
||||
use utoipa_swagger_ui::{Config, SwaggerUi};
|
||||
use utoipa_axum::routes;
|
||||
|
||||
use crate::{entities::{owner, prelude::BookInstance}, utils::events::Event};
|
||||
use crate::{entities::prelude::BookInstance, utils::events::Event};
|
||||
|
||||
pub mod entities;
|
||||
pub mod utils;
|
||||
|
|
@ -38,41 +38,26 @@ async fn index(
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
pretty_env_logger::init();
|
||||
|
||||
let db: Arc<DatabaseConnection> = Arc::new(Database::connect(String::from("sqlite://./alexandria.db?mode=rwc")).await.unwrap());
|
||||
|
||||
let builder = db.get_database_backend();
|
||||
let schema = Schema::new(builder);
|
||||
if let Err(err) = db.execute(builder.build(schema.create_table_from_entity(crate::entities::prelude::Book).if_not_exists())).await {
|
||||
println!("Error while creating book table: {err:?}");
|
||||
log::error!(target: "database", "Error while creating book table: {err:?}");
|
||||
return;
|
||||
}
|
||||
if let Err(err) = db.execute(builder.build(schema.create_table_from_entity(crate::entities::prelude::BookInstance).if_not_exists())).await {
|
||||
println!("Error while creating book_instance table: {err:?}");
|
||||
log::error!(target: "database", "Error while creating book_instance table: {err:?}");
|
||||
return;
|
||||
}
|
||||
if let Err(err) = db.execute(builder.build(schema.create_table_from_entity(crate::entities::prelude::Owner).if_not_exists())).await {
|
||||
println!("Error while creating owner table: {err:?}");
|
||||
log::error!(target: "database", "Error while creating owner table: {err:?}");
|
||||
return;
|
||||
}
|
||||
|
||||
let (event_bus, _) = broadcast::channel(16);
|
||||
|
||||
let ntx = event_bus.clone();
|
||||
let _forever = task::spawn(async move {
|
||||
let mut interval = time::interval(Duration::from_secs(5));
|
||||
|
||||
let mut id = 1;
|
||||
loop {
|
||||
interval.tick().await;
|
||||
let _ = ntx.send(Event::WebsocketBroadcast(utils::events::WebsocketMessage::NewOwner(Arc::new(owner::Model {
|
||||
id,
|
||||
first_name: "Avril".to_string(),
|
||||
last_name: "Papillon".to_string(),
|
||||
contact: "avril.papillon@proton.me".to_string()
|
||||
}))));
|
||||
id += 1;
|
||||
}
|
||||
});
|
||||
|
||||
let mut default_headers = HeaderMap::new();
|
||||
default_headers.append(USER_AGENT, "Alexandria/1.0 (unionetudianteauvergne@gmail.com)".parse().unwrap());
|
||||
|
|
@ -118,7 +103,7 @@ async fn main() {
|
|||
let router = router.merge(swagger);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||
println!("Running on http://{}", listener.local_addr().unwrap());
|
||||
log::info!("Running on http://{}", listener.local_addr().unwrap());
|
||||
axum::serve(
|
||||
listener,
|
||||
router.into_make_service_with_connect_info::<SocketAddr>()
|
||||
|
|
|
|||
Reference in a new issue