feat: rtc peer connection offer

This commit is contained in:
Ahmet Kaan Gümüş 2025-04-11 04:58:16 +03:00
parent 7434d131c4
commit 0aa65f0f60
17 changed files with 389 additions and 59 deletions

74
server/src/lib.rs Normal file
View file

@ -0,0 +1,74 @@
use std::sync::LazyLock;
use utils::naive_toml_parser;
pub mod signal;
pub mod utils;
const SERVER_CONFIG_FILE_LOCATION: &str = "./configs/server_config.toml";
const DATABASE_CONFIG_FILE_LOCATION: &str = "./configs/database_config.toml";
pub static SERVER_CONFIG: LazyLock<ServerConfig> = LazyLock::new(ServerConfig::default);
#[derive(Debug)]
pub struct DatabaseConfig {
pub address: String,
pub username: String,
pub password: String,
pub database: String,
pub backend: String,
pub connection_pool_size: u32,
}
impl Default for DatabaseConfig {
fn default() -> Self {
let (header, mut database_configs) = naive_toml_parser(DATABASE_CONFIG_FILE_LOCATION);
if header == "[database_config]" {
Self {
address: database_configs.pop_front().unwrap().parse().unwrap(),
username: database_configs.pop_front().unwrap().parse().unwrap(),
password: database_configs.pop_front().unwrap().parse().unwrap(),
database: database_configs.pop_front().unwrap().parse().unwrap(),
backend: database_configs.pop_front().unwrap().parse().unwrap(),
connection_pool_size: database_configs.pop_front().unwrap().parse().unwrap(),
}
} else {
panic!("Database Config File Must Include [database_config] at the First Line")
}
}
}
#[derive(Debug)]
pub struct ServerConfig {
pub address: String,
pub otp_time_limit: usize,
pub login_token_expiration_time_limit: usize,
pub login_token_refresh_time_limit: usize,
pub concurrency_limit: usize,
}
impl Default for ServerConfig {
fn default() -> Self {
let (header, mut server_configs) = naive_toml_parser(SERVER_CONFIG_FILE_LOCATION);
let value_or_max = |value: String| value.parse().map_or(usize::MAX, |value| value);
let value_or_semaphore_max = |value: String| {
value
.parse()
.map_or(tokio::sync::Semaphore::MAX_PERMITS, |value| value)
};
if header == "[server_config]" {
Self {
address: server_configs.pop_front().unwrap().parse().unwrap(),
otp_time_limit: value_or_max(server_configs.pop_front().unwrap()),
login_token_expiration_time_limit: value_or_max(
server_configs.pop_front().unwrap(),
),
login_token_refresh_time_limit: value_or_max(server_configs.pop_front().unwrap()),
concurrency_limit: value_or_semaphore_max(server_configs.pop_front().unwrap()),
}
} else {
panic!("Server Config File Must Include [server_config] at the First Line")
}
}
}

View file

@ -1,3 +1,8 @@
fn main() {
use rust_communication_server::signal::start_signalling;
#[tokio::main]
async fn main() {
println!("Hello, world!");
tokio::spawn(start_signalling()).await.unwrap();
}

36
server/src/signal.rs Normal file
View file

@ -0,0 +1,36 @@
use std::sync::{Arc, LazyLock, RwLock};
use axum::{
Json, Router,
http::StatusCode,
response::IntoResponse,
routing::{get, post},
};
use axum_macros::debug_handler;
use protocol::Signal;
use tokio::net::TcpListener;
static SIGNALS: LazyLock<Arc<RwLock<Vec<Signal>>>> =
LazyLock::new(|| Arc::new(RwLock::new(vec![])));
pub async fn start_signalling() {
let route = route();
let listener = TcpListener::bind("0.0.0.0:4546").await.unwrap();
axum::serve(listener, route).await.unwrap();
}
fn route() -> Router {
Router::new()
.route("/alive", get(alive))
.route("/", post(signal))
}
async fn alive() -> impl IntoResponse {
StatusCode::OK
}
#[debug_handler]
async fn signal(Json(signal): Json<Signal>) -> impl IntoResponse {
SIGNALS.write().unwrap().push(signal);
StatusCode::OK
}

24
server/src/utils.rs Normal file
View file

@ -0,0 +1,24 @@
use std::{collections::VecDeque, fs::File, io::Read};
pub fn naive_toml_parser(file_location: &str) -> (String, VecDeque<String>) {
let mut toml_file = File::open(file_location).unwrap();
let mut toml_ingredients = String::default();
toml_file.read_to_string(&mut toml_ingredients).unwrap();
let mut toml_ingredients = toml_ingredients.lines().collect::<VecDeque<&str>>();
let header = toml_ingredients.pop_front().unwrap().trim_end().to_string();
let parsed = toml_ingredients
.iter()
.map(|ingredient| {
ingredient
.split_once('=')
.unwrap()
.1
.replace('"', "")
.trim()
.to_string()
})
.collect();
(header, parsed)
}