Skip to content

Commit 5e243e3

Browse files
committed
playing around with logger..
1 parent 11cb088 commit 5e243e3

File tree

4 files changed

+193
-7
lines changed

4 files changed

+193
-7
lines changed

Cargo.lock

+141
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ postgres = "0.19"
1010
serde = "1.0"
1111
serde_json = "1.0"
1212
serde_derive = "1.0"
13+
log = "0.4"
14+
env_logger = "*"

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ all: build run
33

44
build: src/main.rs
55
DB_URL='postgres://postgres:postgres@localhost:5432/postgres' \
6+
RUST_LOG=all \
67
cargo build --release
78

89
run:

src/main.rs

+49-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ use postgres::Error as PostgresError;
33
use std::net::{TcpListener, TcpStream};
44
use std::io::{Read, Write};
55
use std::env;
6+
use log::info;
7+
use log::warn;
8+
use log::debug;
9+
use log::error;
10+
use log::log_enabled;
11+
use log::Level;
612

713
#[macro_use]
814
extern crate serde_derive;
@@ -16,18 +22,39 @@ struct User {
1622
}
1723

1824
const DB_URL: &str = env!("DB_URL");
25+
const SERVER_PORT: &str = "8080";
1926
const RESPONSE_OK: &str = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n";
2027
const RESPONSE_NOT_FOUND: &str = "HTTP/1.1 404 NOT FOUND\r\n\r\n";
2128
const RESPONSE_INTERNAL_SERVER_ERROR: &str = "HTTP/1.1 500 INTERNAL SERVER ERROR\r\n\r\n";
2229

2330
fn main() {
31+
2432
if let Err(e) = set_database() {
2533
println!("Error: {}", e);
2634
return;
2735
}
2836

29-
let listener = TcpListener::bind(format!("0.0.0.0:8080")).unwrap();
30-
println!("Server started at port 8080");
37+
env_logger::init();
38+
39+
// debug!("Mary has a little lamb");
40+
// warn!("{}", "The lamb was sure to go");
41+
// warn!("{:#?}", "The lamb was sure to go");
42+
// warn!("server started at port {} ..", SERVER_PORT);
43+
44+
if log_enabled!(Level::Error) {
45+
error!("Error: {}", "Its fleece was white as snow");
46+
}
47+
48+
if log_enabled!(Level::Info) {
49+
info!("{}", "And every where that Mary went");
50+
info!("{:?}", "And every where that Mary went");
51+
info!("{}", "server started at port");
52+
} else {
53+
println!("log_enabled!(Level::Info) not enabled !");
54+
}
55+
56+
let listener = TcpListener::bind(format!("0.0.0.0:{}", SERVER_PORT)).unwrap();
57+
println!("server started at port {} ..", SERVER_PORT);
3158

3259
for stream in listener.incoming() {
3360
match stream {
@@ -51,7 +78,7 @@ fn handle_client(mut stream: TcpStream) {
5178

5279
let (status_line, content) = match &*request {
5380
r if r.starts_with("POST /users") => handle_post_request(r),
54-
r if r.starts_with("GET /users") => handle_get_request(r),
81+
r if r.starts_with("GET /user/") => handle_get_request(r),
5582
r if r.starts_with("GET /users") => handle_get_all_request(r),
5683
r if r.starts_with("PUT /users") => handle_put_request(r),
5784
r if r.starts_with("DELETE /users") => handle_delete_request(r),
@@ -82,7 +109,9 @@ fn handle_post_request(request: &str) -> (String, String) {
82109
}
83110

84111
fn handle_get_request(request: &str) -> (String, String) {
112+
println!("handle_get_request: {}", request);
85113
match (get_id(&request).parse::<i32>(), Client::connect(DB_URL, NoTls)) {
114+
86115
(Ok(id), Ok(mut client)) =>
87116
match client.query_one("SELECT * FROM users WHERE id = $1", &[&id]) {
88117
Ok(row) => {
@@ -97,11 +126,20 @@ fn handle_get_request(request: &str) -> (String, String) {
97126
_ => (RESPONSE_NOT_FOUND.to_string(), "User not found".to_string()),
98127
}
99128

100-
_ => (RESPONSE_INTERNAL_SERVER_ERROR.to_string(), "Internal error".to_string()),
129+
(Err(e_db),Err(e_client)) => {
130+
println!("handle_get_request: {}, {}", e_db, e_client);
131+
(RESPONSE_INTERNAL_SERVER_ERROR.to_string(), "Internal error: handle_get_request".to_string())
132+
}
133+
134+
_ => {
135+
println!("handle_get_request..");
136+
(RESPONSE_INTERNAL_SERVER_ERROR.to_string(), "Internal error: handle_get_request".to_string())
137+
}
101138
}
102139
}
103140

104141
fn handle_get_all_request(_request: &str) -> (String, String) {
142+
println!("handle_get_all_request..");
105143
match Client::connect(DB_URL, NoTls) {
106144
Ok(mut client) => {
107145
let mut users = Vec::new();
@@ -116,7 +154,11 @@ fn handle_get_all_request(_request: &str) -> (String, String) {
116154

117155
(RESPONSE_OK.to_string(), serde_json::to_string(&users).unwrap())
118156
}
119-
_ => (RESPONSE_INTERNAL_SERVER_ERROR.to_string(), "Internal error".to_string()),
157+
Err(e) => {
158+
println!("handle_get_all_request: {}", e);
159+
(RESPONSE_INTERNAL_SERVER_ERROR.to_string(), "Internal error: handle_get_all_request".to_string())
160+
}
161+
// _ => (RESPONSE_INTERNAL_SERVER_ERROR.to_string(), "Internal error".to_string()),
120162
}
121163
}
122164

@@ -138,7 +180,7 @@ fn handle_put_request(request: &str) -> (String, String) {
138180

139181
(RESPONSE_OK.to_string(), "User updated".to_string())
140182
}
141-
_ => (RESPONSE_INTERNAL_SERVER_ERROR.to_string(), "Internal error".to_string()),
183+
_ => (RESPONSE_INTERNAL_SERVER_ERROR.to_string(), "Internal error: handle_put_request".to_string()),
142184
}
143185
}
144186

@@ -153,7 +195,7 @@ fn handle_delete_request(request: &str) -> (String, String) {
153195

154196
(RESPONSE_OK.to_string(), "User deleted".to_string())
155197
}
156-
_ => (RESPONSE_INTERNAL_SERVER_ERROR.to_string(), "Internal error".to_string()),
198+
_ => (RESPONSE_INTERNAL_SERVER_ERROR.to_string(), "Internal error: handle_delete_request".to_string()),
157199
}
158200
}
159201

0 commit comments

Comments
 (0)