1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-29 16:56:28 +00:00
comunicapiv3/src/main.rs

90 lines
2.3 KiB
Rust
Raw Normal View History

2021-05-04 17:18:47 +00:00
use std::future::Future;
2021-02-13 15:36:39 +00:00
use comunic_server::{cleanup_thread, server};
2020-05-20 17:05:59 +00:00
use comunic_server::data::config::{conf, Config};
2021-03-02 18:00:14 +00:00
use comunic_server::helpers::database;
2020-05-22 06:51:15 +00:00
2021-05-04 17:18:47 +00:00
type MainActionFunction = Box<dyn Future<Output=std::io::Result<()>>>;
struct Action {
name: String,
description: String,
arguments: Vec<String>,
function: Box<dyn FnOnce() -> MainActionFunction>,
}
2020-05-21 13:28:07 +00:00
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
2021-05-04 17:18:47 +00:00
let args: Vec<String> = std::env::args().collect();
let conf_file = match args.get(1) {
Some(el) => el.to_string(),
2021-02-12 16:35:26 +00:00
None => "config.yaml".to_string(),
};
2020-05-20 17:05:59 +00:00
// Load configuration
2021-02-12 16:35:26 +00:00
Config::load(&conf_file).expect("Could not load configuration!");
2020-05-20 17:05:59 +00:00
2020-05-21 07:21:58 +00:00
// Connect to the database
database::connect(&conf().database).expect("Could not connect to database!");
2020-05-20 17:05:59 +00:00
2021-05-04 17:18:47 +00:00
// Get selected action
let action = args
.get(2)
.map(|a| a.as_str())
.unwrap_or("serve")
.to_string();
/* let actions = vec![
Action {
name: "serve".to_string(),
description: "Start the Comunic Server (default action)".to_string(),
arguments: vec![],
function: Box::new(serve),
}
];
let selected_action = actions
.iter()
.filter(|p|p.name.eq(&action))
.next();
let selected_action = match selected_action {
None => {
eprintln!("Action {} invalid! For more information try 'help'!", action);
std::process::exit(-1);
}
Some(a) => a
};
2021-02-13 15:36:39 +00:00
2021-05-04 17:18:47 +00:00
if selected_action.arguments.len() != args.len() - 2 {
eprintln!("Invalid number of arguments!");
std::process::exit(-2);
}
let func = (selected_action.function)();
func.await*/
serve()
2020-05-20 15:46:05 +00:00
}
2021-05-04 17:18:47 +00:00
/// Start Comunic Server (main action)
fn serve() -> std::io::Result<()> {
let t = std::thread::spawn(|| {
let sys = actix::System::new("sys");
let promise = async {
// Start cleanup thread
cleanup_thread::start().expect("Failed to start cleanup thread!");
// Start the server
server::start_server(conf()).await
};
tokio::runtime::Runtime::new().unwrap().block_on(promise)
.expect("Failed to start server!");
let _ = sys.run();
});
t.join().unwrap();
Ok(())
}