Skip to content

Commit

Permalink
Merge pull request #38 from 847850277/issues_37
Browse files Browse the repository at this point in the history
[ISSUES#37] sync example part 02 run server
  • Loading branch information
847850277 authored Nov 3, 2024
2 parents 8ca7bc1 + 184bf26 commit f2fe0ef
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ edition = "2021"
[workspace]
members = [
"mario-core", "mario-macro",
"mario-rs-examples", "other-examples",
"mario-rs-examples", "sync-core", "other-examples", "sync-examples",
]
resolver = "2"

[workspace.dependencies]
mario-core = { path = "mario-core" }
mario-macro = { path = "mario-macro" }
sync-core = { path = "sync-core" }
tokio = { version = "1.39.3", features = ["full"] }
tracing-subscriber = "0.3.18"
anyhow = "1.0"
Expand Down
8 changes: 8 additions & 0 deletions sync-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "sync-core"
version.workspace = true
edition.workspace = true

[dependencies]
log = "0.4.22"

1 change: 1 addition & 0 deletions sync-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod server;
41 changes: 41 additions & 0 deletions sync-core/src/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use log::info;
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};

pub struct Server {}

impl Server {
pub fn new() -> Self {
Self {}
}

pub fn start(&self) {
//println!("todo start tcp server...");
let addr = "127.0.0.1:8080";
// start tcp server
let listener = TcpListener::bind(addr).unwrap();
info!("Listening on http://{}", addr);
for stream in listener.incoming() {
match stream {
Ok(stream) => {
self.handle_connection(stream);
}
Err(e) => {
eprintln!("failed: {}", e);
}
}
}
}

fn handle_connection(&self, mut tcp_stream: TcpStream) {
//println!("todo handle connection...");
let mut buffer = [0; 1024];
tcp_stream.read(&mut buffer).unwrap();
println!("Request: {}", String::from_utf8_lossy(&buffer));

// response
let response = "HTTP/1.1 200 OK\r\n\r\n";
let response = format!("{}{}", response, "Hello World");
tcp_stream.write(response.as_bytes()).unwrap();
}
}
8 changes: 8 additions & 0 deletions sync-examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "sync-examples"
version.workspace = true
edition.workspace = true

[dependencies]
sync-core = { workspace = true }
tracing-subscriber = "0.3.18"
1 change: 1 addition & 0 deletions sync-examples/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

7 changes: 7 additions & 0 deletions sync-examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use sync_core::server::Server;
fn main() {
//trace log
tracing_subscriber::fmt::init();
let mut server = Server::new();
server.start();
}

0 comments on commit f2fe0ef

Please sign in to comment.