Skip to content

Commit 5f520a6

Browse files
fix: no response logic
1 parent bfe03be commit 5f520a6

File tree

7 files changed

+67
-14
lines changed

7 files changed

+67
-14
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "full_rust_web"
2+
name = "rust_server_sample"
33
version = "0.1.0"
44
edition = "2021"
55

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Rust 全栈式开发 Web 应用
1+
# Rust TCP/HTTP Socket Server
22

33
## 目录结构
44

@@ -9,7 +9,55 @@
99
| http | HTTP 模块目录 |
1010
| httpserver | HTTP 服务器目录 |
1111

12-
## 工作流
12+
## 运行方式
13+
14+
### TCP 服务器
15+
16+
#### 启动 TCP 服务器
17+
18+
```shell
19+
# `--package` or `-p`
20+
cargo run --package tcpserver
21+
```
22+
23+
#### 运行 TCP 客户端访问服务器
24+
25+
```shell
26+
# `--package` or `-p`
27+
cargo run --package tcpclient
28+
```
29+
30+
执行成功后终端中会打印:
31+
`Response from server: "Hello"`
32+
33+
### HTTP 服务器
34+
35+
#### 启动 HTTP 服务器
36+
37+
```shell
38+
# `--package` or `-p`
39+
# 运行于 http://localhost:8000
40+
cargo run --package httpserver
41+
```
42+
43+
#### 暴露的 HTTP 接口
44+
45+
| 接口 | 响应 |
46+
| -------------------------------------------------------------------------- | ----------- |
47+
| <http://localhost:8000> | index.html |
48+
| <http://localhost:8000/health> | health.html |
49+
| <http://localhost:8000/style.css> | style.css |
50+
| <http://localhost:8000/api/shipping/orders> | orders.json |
51+
| <http://localhost:8000/anyelse>(“anyelse” 可改为接口中未列出的任意文字 ) | 404.html |
52+
53+
#### HTTP 模块单元测试
54+
55+
```shell
56+
# `--package` or `-p`
57+
cargo test --package http
58+
```
59+
60+
## 制作流程
1361

1462
### 1. 创建 Workspace
1563

httpserver/public/health.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
<link href="style.css" rel="stylesheet" />
88
</head>
99
<body>
10-
<h1>Please check health</h1>
10+
<h1>Check health</h1>
1111
</body>
1212
</html>

httpserver/src/handler.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl Handler for StaticPageHandler {
6767

6868
HttpResonse::new("200", Some(headers), Some(contents))
6969
}
70-
None => HttpResonse::default(),
70+
None => HttpResonse::new("404", None, Self::load_file("404.html")),
7171
},
7272
};
7373

@@ -99,18 +99,23 @@ impl WebServiceHandler {
9999

100100
impl Handler for WebServiceHandler {
101101
fn handler(request: &HttpRequest) -> HttpResonse {
102+
let default_response = HttpResonse::new("404", None, Self::load_file("404.html"));
103+
102104
let Resource::Path(url) = &request.resource;
103105
let url = url.split('/').collect::<Vec<&str>>();
104-
let is_orders = url.len() > 3 && url[3] == "orders";
106+
107+
if url.len() < 4 {
108+
return default_response;
109+
}
105110

106111
match url[2] {
107-
"shipping" if is_orders => {
112+
"shipping" if url[3] == "orders" => {
108113
let headers = HashMap::from([("Content-Type", "application/json")]);
109114
let body = serde_json::to_string(&Self::load_json()).unwrap();
110115
HttpResonse::new("200", Some(headers), Some(body))
111116
}
112117

113-
_ => HttpResonse::new("404", None, Self::load_file("404.html")),
118+
_ => default_response,
114119
}
115120
}
116121
}

httpserver/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl<'a> Server<'a> {
1313

1414
pub fn run(&self) {
1515
let listener = TcpListener::bind(self.socket_address).unwrap();
16-
println!("Running on {}", self.socket_address);
16+
println!("Running on http://{}", self.socket_address);
1717

1818
for stream in listener.incoming() {
1919
println!("Connection established!");

tcpclient/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn handle_response(mut stream: TcpStream) -> Result<()> {
2525
let _bypes_length = stream.read(&mut buffer)?;
2626

2727
let response = str::from_utf8(&buffer);
28-
println!("Response from server: {:?}", response);
28+
println!("Response from server: {:?}", response.unwrap());
2929

3030
Ok(())
3131
}

0 commit comments

Comments
 (0)