-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from 847850277/issues_37
[ISSUES#37] sync example part 02 run server
- Loading branch information
Showing
7 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |