Skip to content

Commit

Permalink
start handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed May 19, 2024
1 parent 8d1852a commit 7752582
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = "4.5.4"
serde_json = "1.0.117"
tokio = { version = "1.37.0", features = ["full"] }
tracing = "0.1.40"
Expand Down
13 changes: 11 additions & 2 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@ pub fn handle(json: &str) {
println!("{}: {:?}", "val", val["method"]);

match method {
"enter" => {
enter::handle(&val);
}
"login" => {
login_handler::handle(&val);
login::handle(&val);
}
_ => {
tracing::error!("Unkown method request: {:?}", method);
}
}
}

mod login_handler {
mod enter {
pub fn handle(json: &serde_json::Value) {
tracing::trace!("method: {:?}", json["method"]);
}
}

mod login {
pub fn handle(json: &serde_json::Value) {
tracing::trace!("method: {:?}", json["method"]);
}
Expand Down
21 changes: 19 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#![allow(dead_code)]
#![allow(unused_imports)]

mod server;
mod handler;
mod packet;
mod server;

use server::Server;

use std::io;
use tracing_subscriber::{fmt, layer::SubscriberExt};

use clap::{arg, Command};

/// Setup logger rotator.
///
/// https://docs.rs/tracing-appender/0.2.3/tracing_appender/non_blocking/struct.WorkerGuard.html
Expand All @@ -26,6 +29,20 @@ pub fn setup_logger() -> tracing_appender::non_blocking::WorkerGuard {
#[tokio::main]
async fn main() {
let _guard = setup_logger();
let mut server = Server::new("127.0.0.1".to_string(), 8080);

let matches = Command::new("Cogru")
.version("0.1.0")
.about("Where the collaboration start!?")
.arg(
arg!(--port <VALUE>)
.required(false)
.help("Port number")
.default_value("8786"),
)
.get_matches();

let port = matches.get_one::<String>("port").unwrap().parse::<u16>().unwrap();

let mut server = Server::new("127.0.0.1".to_string(), port);
let _ = server.start().await;
}
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

const SEPARATOR_LEN: usize = "\r\n".len();
const BUF_SIZE: usize = 1024;
const BUF_SIZE: usize = 1024 * 1;

fn get_content_len(line: &str) -> usize {
if !line.starts_with("Content-Length: ") {
Expand Down

0 comments on commit 7752582

Please sign in to comment.