Compare commits

...

3 Commits

Author SHA256 Message Date
889fde4636 - add: missing call to dotenv
- remove: unreachable code in route_packet
2024-11-26 13:17:27 -05:00
47899d47cd - add: virtual workspace
- add: start of packet-service
2024-11-26 13:16:20 -05:00
815cb210dc - fix: warnings about unused variables
- add: LOG_LEVEL env variable
2024-11-26 13:15:33 -05:00
8 changed files with 123 additions and 17 deletions

7
Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[workspace]
members = [
"auth-service",
"database-service",
"packet-service",
]
resolver = "2"

View File

@@ -77,31 +77,31 @@ impl DatabaseClientTrait for DatabaseClient {
async fn store_password_reset(
&mut self,
email: &str,
reset_token: &str,
expires_at: DateTime<Utc>,
_email: &str,
_reset_token: &str,
_expires_at: DateTime<Utc>,
) -> Result<(), Box<dyn Error>> {
Ok(())
}
async fn get_password_reset(
&self,
reset_token: &str,
_reset_token: &str,
) -> Result<Option<PasswordReset>, Box<dyn std::error::Error>> {
todo!()
}
async fn delete_password_reset(
&self,
reset_token: &str,
_reset_token: &str,
) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
async fn update_user_password(
&self,
email: &str,
hashed_password: &str,
_email: &str,
_hashed_password: &str,
) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

View File

@@ -6,9 +6,10 @@ use auth_service::grpc::MyAuthService;
use dotenv::dotenv;
use std::env;
use std::net::ToSocketAddrs;
use std::str::FromStr;
use tokio::{select, signal};
use tonic::transport::Server;
use tracing::info;
use tracing::{info, Level};
use warp::Filter;
mod consul_registration;
@@ -20,9 +21,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_thread_names(true)
.with_timer(tracing_subscriber::fmt::time::ChronoLocal::rfc_3339())
.with_max_level(Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())).unwrap_or_else(|_| Level::INFO))
.init();
// Set the gRPC server address

View File

@@ -7,10 +7,11 @@ use dotenv::dotenv;
use sqlx::postgres::PgPoolOptions;
use std::env;
use std::net::ToSocketAddrs;
use std::str::FromStr;
use std::sync::Arc;
use tokio::{select, signal};
use tonic::transport::Server;
use tracing::info;
use tracing::{info, Level};
use warp::Filter;
mod consul_registration;
@@ -19,9 +20,7 @@ mod consul_registration;
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_thread_names(true)
.with_timer(tracing_subscriber::fmt::time::ChronoLocal::rfc_3339())
.with_max_level(Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())).unwrap_or_else(|_| Level::INFO))
.init();
let addr = env::var("DATABASE_SERVICE_ADDR").unwrap_or_else(|_| "127.0.0.1".to_string());
@@ -69,7 +68,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
};
// Pass `shared_cache` into services as needed
println!("Database Service running on {}", address);
info!("Database Service running on {}", address);
tokio::spawn(Server::builder()
.add_service(DatabaseServiceServer::new(database_service))
.serve(address));

17
packet-service/Cargo.toml Normal file
View File

@@ -0,0 +1,17 @@
[package]
name = "packet-service"
version = "0.1.0"
edition = "2021"
[features]
mocks = []
consul = []
[dependencies]
dotenv = "0.15"
tokio = { version = "1.41.1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
bytes = { version = "1.8.0", features = ["std", "serde"] }
tracing = "0.1"
tracing-subscriber = "0.3.18"

View File

@@ -0,0 +1,40 @@
use std::env;
use std::str::FromStr;
use dotenv::dotenv;
use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt};
use tracing::{info, error};
use tracing::Level;
mod packet;
mod router;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
tracing_subscriber::fmt()
.with_max_level(Level::from_str(&env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string())).unwrap_or_else(|_| Level::INFO))
.init();
let listener = TcpListener::bind("127.0.0.1:4000").await?;
info!("Packet service listening on 127.0.0.1:4000");
loop {
let (mut socket, addr) = listener.accept().await?;
info!("New connection from {}", addr);
tokio::spawn(async move {
let mut buffer = vec![0u8; 1024];
match socket.read(&mut buffer).await {
Ok(n) if n > 0 => {
if let Err(e) = router::route_packet(&buffer[..n]).await {
error!("Failed to route packet: {}", e);
}
}
Ok(_) => info!("Connection closed by {}", addr),
Err(e) => error!("Failed to read from socket: {}", e),
}
});
}
}

View File

@@ -0,0 +1,31 @@
use serde::{Serialize, Deserialize};
use std::error::Error;
use tracing::debug;
#[derive(Debug, Serialize, Deserialize)]
pub struct Packet {
pub packet_size: u16,
pub packet_type: u16,
pub packet_crc: u16,
pub payload: Vec<u8>,
}
impl Packet {
pub fn parse(data: &[u8]) -> Result<Self, Box<dyn Error>> {
if data.len() < 6 {
return Err("Invalid packet: Too short".into());
}
let packet_size = u16::from_be_bytes(data[0..2].try_into()?);
let packet_type = u16::from_be_bytes(data[2..4].try_into()?);
let packet_crc = u16::from_be_bytes(data[4..6].try_into()?);
let payload = data[6..].to_vec();
debug!("Packet Size: {}", packet_size);
debug!("Packet Type: {}", packet_type);
debug!("Packet CRC: {}", packet_crc);
debug!("Payload: {:?}", String::from_utf8(payload.clone())?);
Ok(Packet { packet_size, packet_type, packet_crc, payload })
}
}

View File

@@ -0,0 +1,13 @@
use crate::packet::Packet;
// use crate::handlers::{chat, movement};
use std::error::Error;
pub async fn route_packet(data: &[u8]) -> Result<(), Box<dyn Error>> {
let packet = Packet::parse(data)?;
match packet.packet_type {
// 1 => chat::handle_chat(packet).await?,
// 2 => movement::handle_movement(packet).await?,
_ => return Err("Unknown packet type".into()),
}
}