Added lots of debugging + token auth
Now you dont have to send your password over the wire every time and can make do with a token instead. There's lots of debugging info now to make it easier for me to fix bugs with the server
This commit is contained in:
+26
-9
@@ -15,7 +15,7 @@ use std::process::ExitCode;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use serde::{Deserialize,Serialize};
|
||||
use tracing::{error,instrument,warn,debug,Level};
|
||||
use tracing::{error,instrument,warn,debug,info,Level};
|
||||
use tokio::sync::{RwLock,broadcast,mpsc};
|
||||
|
||||
|
||||
@@ -25,6 +25,12 @@ use crate::state::{State,StateError};
|
||||
#[derive(Hash,Eq,PartialEq,Clone,Serialize,Deserialize,Debug)]
|
||||
pub struct Coordinate(Vec<i64>);
|
||||
|
||||
impl Coordinate {
|
||||
pub fn to_roomid(self) -> fedichat::RoomId {
|
||||
fedichat::RoomId{coordinates: self.0}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<fedichat::RoomId> for Coordinate {
|
||||
fn from(other: fedichat::RoomId) -> Coordinate {
|
||||
Coordinate(other.coordinates)
|
||||
@@ -75,8 +81,12 @@ async fn main() -> ExitCode {
|
||||
|
||||
|
||||
// Set up database connection
|
||||
let db_config = AsyncDieselConnectionManager::<diesel_async::AsyncPgConnection>::new(config.database.url.clone());
|
||||
let db_pool = match Pool::builder(db_config).build() {
|
||||
let db_string = format!("postgres://{}:{}@{}/{}",config.database.user,config.database.password,config.database.url,config.database.db_name);
|
||||
let db_config = AsyncDieselConnectionManager::<diesel_async::AsyncPgConnection>::new(db_string);
|
||||
let db_pool = match Pool::builder(db_config)
|
||||
.max_size(config.database.num_connections)
|
||||
.build()
|
||||
{
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
error!("Error while creating database connection pool");
|
||||
@@ -101,6 +111,7 @@ async fn main() -> ExitCode {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
let quinn_config = match quinn::ServerConfig::with_single_cert(certs, key){
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
@@ -122,6 +133,8 @@ async fn main() -> ExitCode {
|
||||
}
|
||||
};
|
||||
|
||||
debug!("Bound to {address} on port {}",config.port);
|
||||
|
||||
// Load or create new state
|
||||
let state = match State::load_from_file(&config.statefile) {
|
||||
Ok(state) => state,
|
||||
@@ -130,7 +143,10 @@ async fn main() -> ExitCode {
|
||||
match fs::File::create(&config.statefile) {
|
||||
// If the statefile is writable then create an empty state
|
||||
// and use that
|
||||
Ok(_) => State::new(),
|
||||
Ok(_) => {
|
||||
debug!("Creating fresh state");
|
||||
State::new()
|
||||
},
|
||||
Err(e) => {
|
||||
error!("Could not open or create statefile. Check your config.");
|
||||
error!("{:?}",e);
|
||||
@@ -153,9 +169,10 @@ async fn main() -> ExitCode {
|
||||
let (message_send,message_recv) = broadcast::channel(128);
|
||||
let (message_ack_send,message_ack_recv) = mpsc::channel(128);
|
||||
|
||||
debug!("Setting ctrl-c handler");
|
||||
match ctrlc_async::set_async_handler(
|
||||
async move { match close_send.send(()) {
|
||||
Ok(_val) => (),
|
||||
Ok(_val) => debug!("Propogating ctrl-c"),
|
||||
Err(e) => {
|
||||
error!("Shutdown handler is broken. Cannot gracefully exit.");
|
||||
error!("{:?}",e);
|
||||
@@ -177,6 +194,7 @@ async fn main() -> ExitCode {
|
||||
|
||||
// Create client listener
|
||||
|
||||
debug!("Setting up client handler");
|
||||
let statehandle_cloned = statehandle.clone();
|
||||
let config_cloned = config.clone();
|
||||
join_handles.push(tokio::spawn(async move {
|
||||
@@ -191,6 +209,8 @@ async fn main() -> ExitCode {
|
||||
).await;
|
||||
} ));
|
||||
|
||||
info!("Successfully started confetti");
|
||||
|
||||
// Wait for child threads to exit
|
||||
for handle in join_handles {
|
||||
match handle.await {
|
||||
@@ -202,6 +222,7 @@ async fn main() -> ExitCode {
|
||||
}
|
||||
}
|
||||
|
||||
info!("Shutting down");
|
||||
//Save state
|
||||
match statehandle.write().await.write_to_file(&config.statefile).await {
|
||||
Ok(()) => debug!("Successfully wrote state to {:?}",config.statefile),
|
||||
@@ -211,9 +232,5 @@ async fn main() -> ExitCode {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Should never reach this
|
||||
// How should I gracefully shutdown? Can I trap ctrlc
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user