bit better error handling

This commit is contained in:
2021-05-19 20:13:00 +02:00
parent 204fe4d942
commit e5b77edeae
5 changed files with 75 additions and 22 deletions

View File

@ -3,16 +3,18 @@ use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
use ansi_term::Color;
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
use rusty_pool;
use std::fmt::Formatter;
use std::time::Duration;
mod database;
mod error;
mod http;
mod udp;
mod usimp;
mod websocket;
mod error;
enum SocketType {
Http,
@ -20,12 +22,24 @@ enum SocketType {
Udp,
}
impl std::fmt::Display for SocketType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
SocketType::Http => "http+ws",
SocketType::Https => "https+wss",
SocketType::Udp => "udp",
})
}
}
struct SocketConfig {
address: SocketAddr,
socket_type: SocketType,
}
fn main() {
println!("Locutus server");
let socket_configs: Vec<SocketConfig> = vec![
SocketConfig {
address: "[::]:8080".parse().unwrap(),
@ -41,7 +55,13 @@ fn main() {
},
];
database::init();
// Note: rust's stdout is line buffered!
eprint!("Initializing database connection pool...");
if let Err(error) = database::init() {
eprintln!("\n{}", Color::Red.bold().paint(error.to_string()));
std::process::exit(1);
}
eprintln!(" {}", Color::Green.paint("success"));
let thread_pool = rusty_pool::Builder::new()
.core_size(4)
@ -55,6 +75,12 @@ fn main() {
for socket_config in socket_configs {
let thread_pool_mutex = thread_pool_mutex.clone();
eprintln!(
"Creating listening thread for {} ({})",
ansi_term::Style::new().bold().paint(socket_config.address.to_string()),
socket_config.socket_type
);
threads.push(match socket_config.socket_type {
SocketType::Http => thread::spawn(move || {
let mut tcp_socket = TcpListener::bind(socket_config.address).unwrap();