134 lines
4.1 KiB
Rust
134 lines
4.1 KiB
Rust
|
|
// src/main.rs
|
|||
|
|
//! Главный модуль сервера Falcot
|
|||
|
|
//!
|
|||
|
|
//! Точка входа в приложение, инициализирует сервер и запускает его.
|
|||
|
|
//! Использует wait-free архитектуру с lock-free структурами данных.
|
|||
|
|
|
|||
|
|
mod common;
|
|||
|
|
mod server;
|
|||
|
|
mod client;
|
|||
|
|
mod lua_shell;
|
|||
|
|
|
|||
|
|
use std::env;
|
|||
|
|
use std::fs::OpenOptions;
|
|||
|
|
use std::io::Write;
|
|||
|
|
|
|||
|
|
use crate::common::error::FalcotError;
|
|||
|
|
|
|||
|
|
/// Функция для логирования в файл
|
|||
|
|
fn log_to_file(message: &str) {
|
|||
|
|
match OpenOptions::new()
|
|||
|
|
.create(true)
|
|||
|
|
.append(true)
|
|||
|
|
.open("falcot.log")
|
|||
|
|
{
|
|||
|
|
Ok(mut file) => {
|
|||
|
|
let timestamp = chrono::Local::now().format("%Y-%m-%d %H:%M:%S");
|
|||
|
|
let log_message = format!("[{}] {}\n", timestamp, message);
|
|||
|
|
let _ = file.write_all(log_message.as_bytes());
|
|||
|
|
}
|
|||
|
|
Err(e) => eprintln!("Failed to write to log file: {}", e),
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Простая структура для аргументов командной строки
|
|||
|
|
struct Args {
|
|||
|
|
config: String,
|
|||
|
|
debug: bool,
|
|||
|
|
http_port: Option<u16>,
|
|||
|
|
https_port: Option<u16>,
|
|||
|
|
host: Option<String>,
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Простой парсер аргументов командной строки
|
|||
|
|
fn parse_args() -> Args {
|
|||
|
|
let mut args = Args {
|
|||
|
|
config: "config.toml".to_string(),
|
|||
|
|
debug: false,
|
|||
|
|
http_port: None,
|
|||
|
|
https_port: None,
|
|||
|
|
host: None,
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
let mut iter = env::args().skip(1);
|
|||
|
|
while let Some(arg) = iter.next() {
|
|||
|
|
match arg.as_str() {
|
|||
|
|
"--config" | "-c" => {
|
|||
|
|
if let Some(value) = iter.next() {
|
|||
|
|
args.config = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
"--debug" | "-d" => {
|
|||
|
|
args.debug = true;
|
|||
|
|
}
|
|||
|
|
"--http-port" => {
|
|||
|
|
if let Some(value) = iter.next() {
|
|||
|
|
if let Ok(port) = value.parse() {
|
|||
|
|
args.http_port = Some(port);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
"--https-port" => {
|
|||
|
|
if let Some(value) = iter.next() {
|
|||
|
|
if let Ok(port) = value.parse() {
|
|||
|
|
args.https_port = Some(port);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
"--host" => {
|
|||
|
|
if let Some(value) = iter.next() {
|
|||
|
|
args.host = Some(value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
_ => {
|
|||
|
|
if arg.starts_with("--config=") {
|
|||
|
|
args.config = arg.trim_start_matches("--config=").to_string();
|
|||
|
|
} else if arg.starts_with("-c=") {
|
|||
|
|
args.config = arg.trim_start_matches("-c=").to_string();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
args
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#[tokio::main]
|
|||
|
|
async fn main() -> Result<(), FalcotError> {
|
|||
|
|
// Инициализация логирования в файл
|
|||
|
|
log_to_file("Starting Falcot server");
|
|||
|
|
|
|||
|
|
// Парсим аргументы командной строки
|
|||
|
|
let args = parse_args();
|
|||
|
|
let config_path = args.config;
|
|||
|
|
|
|||
|
|
// Добавляем пустую строкю перед загрузкой конфигурации
|
|||
|
|
println!();
|
|||
|
|
|
|||
|
|
let message = format!("Loading configuration from: {}", config_path);
|
|||
|
|
println!("{}", message);
|
|||
|
|
log_to_file(&message);
|
|||
|
|
|
|||
|
|
// Создание и запуск сервера
|
|||
|
|
match server::FalcotServer::new(&config_path).await {
|
|||
|
|
Ok(server) => {
|
|||
|
|
log_to_file("Server created successfully");
|
|||
|
|
if let Err(e) = server.run().await {
|
|||
|
|
let error_message = format!("Server error: {}", e);
|
|||
|
|
eprintln!("{}", error_message);
|
|||
|
|
log_to_file(&error_message);
|
|||
|
|
std::process::exit(1);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
Err(e) => {
|
|||
|
|
let error_message = format!("Failed to create server: {}", e);
|
|||
|
|
eprintln!("{}", error_message);
|
|||
|
|
log_to_file(&error_message);
|
|||
|
|
std::process::exit(1);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
log_to_file("Falcot server stopped");
|
|||
|
|
Ok(())
|
|||
|
|
}
|