@@ -3,6 +3,12 @@ use postgres::Error as PostgresError;
3
3
use std:: net:: { TcpListener , TcpStream } ;
4
4
use std:: io:: { Read , Write } ;
5
5
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 ;
6
12
7
13
#[ macro_use]
8
14
extern crate serde_derive;
@@ -16,18 +22,39 @@ struct User {
16
22
}
17
23
18
24
const DB_URL : & str = env ! ( "DB_URL" ) ;
25
+ const SERVER_PORT : & str = "8080" ;
19
26
const RESPONSE_OK : & str = "HTTP/1.1 200 OK\r \n Content-Type: application/json\r \n \r \n " ;
20
27
const RESPONSE_NOT_FOUND : & str = "HTTP/1.1 404 NOT FOUND\r \n \r \n " ;
21
28
const RESPONSE_INTERNAL_SERVER_ERROR : & str = "HTTP/1.1 500 INTERNAL SERVER ERROR\r \n \r \n " ;
22
29
23
30
fn main ( ) {
31
+
24
32
if let Err ( e) = set_database ( ) {
25
33
println ! ( "Error: {}" , e) ;
26
34
return ;
27
35
}
28
36
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 ) ;
31
58
32
59
for stream in listener. incoming ( ) {
33
60
match stream {
@@ -51,7 +78,7 @@ fn handle_client(mut stream: TcpStream) {
51
78
52
79
let ( status_line, content) = match & * request {
53
80
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) ,
55
82
r if r. starts_with ( "GET /users" ) => handle_get_all_request ( r) ,
56
83
r if r. starts_with ( "PUT /users" ) => handle_put_request ( r) ,
57
84
r if r. starts_with ( "DELETE /users" ) => handle_delete_request ( r) ,
@@ -82,7 +109,9 @@ fn handle_post_request(request: &str) -> (String, String) {
82
109
}
83
110
84
111
fn handle_get_request ( request : & str ) -> ( String , String ) {
112
+ println ! ( "handle_get_request: {}" , request) ;
85
113
match ( get_id ( & request) . parse :: < i32 > ( ) , Client :: connect ( DB_URL , NoTls ) ) {
114
+
86
115
( Ok ( id) , Ok ( mut client) ) =>
87
116
match client. query_one ( "SELECT * FROM users WHERE id = $1" , & [ & id] ) {
88
117
Ok ( row) => {
@@ -97,11 +126,20 @@ fn handle_get_request(request: &str) -> (String, String) {
97
126
_ => ( RESPONSE_NOT_FOUND . to_string ( ) , "User not found" . to_string ( ) ) ,
98
127
}
99
128
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
+ }
101
138
}
102
139
}
103
140
104
141
fn handle_get_all_request ( _request : & str ) -> ( String , String ) {
142
+ println ! ( "handle_get_all_request.." ) ;
105
143
match Client :: connect ( DB_URL , NoTls ) {
106
144
Ok ( mut client) => {
107
145
let mut users = Vec :: new ( ) ;
@@ -116,7 +154,11 @@ fn handle_get_all_request(_request: &str) -> (String, String) {
116
154
117
155
( RESPONSE_OK . to_string ( ) , serde_json:: to_string ( & users) . unwrap ( ) )
118
156
}
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()),
120
162
}
121
163
}
122
164
@@ -138,7 +180,7 @@ fn handle_put_request(request: &str) -> (String, String) {
138
180
139
181
( RESPONSE_OK . to_string ( ) , "User updated" . to_string ( ) )
140
182
}
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 ( ) ) ,
142
184
}
143
185
}
144
186
@@ -153,7 +195,7 @@ fn handle_delete_request(request: &str) -> (String, String) {
153
195
154
196
( RESPONSE_OK . to_string ( ) , "User deleted" . to_string ( ) )
155
197
}
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 ( ) ) ,
157
199
}
158
200
}
159
201
0 commit comments