@@ -24,6 +24,7 @@ use axum::{
24
24
} ;
25
25
use dotenvy:: dotenv;
26
26
// use log::{debug, info, warn};
27
+ use crate :: service:: hello;
27
28
use pretty_env_logger;
28
29
use serde:: { Deserialize , Serialize } ;
29
30
use std:: {
@@ -36,6 +37,7 @@ use std::{
36
37
use tower:: { BoxError , ServiceBuilder } ;
37
38
use tower_http:: trace:: TraceLayer ;
38
39
use uuid:: Uuid ;
40
+ mod service;
39
41
mod utils;
40
42
use crate :: utils:: { route, shutdown} ;
41
43
use tracing:: { debug, error, info, warn, Level } ;
@@ -63,15 +65,15 @@ async fn main() {
63
65
64
66
// fix conflict with tracing_subscriber
65
67
// pretty_env_logger::init_custom_env("CMS_LOG");
66
-
67
- let db = Db :: default ( ) ;
68
+ let db = hello:: Db :: default ( ) ;
68
69
69
70
// Compose the routes
70
71
let app = Router :: new ( )
71
72
. fallback ( route:: handler_404. into_service ( ) )
72
- . route ( "/" , get ( todos_index) . post ( todos_create) )
73
- . route ( "/todos" , get ( todos_index) . post ( todos_create) )
74
- . route ( "/todos/:id" , patch ( todos_update) . delete ( todos_delete) )
73
+ . route ( "/" , get ( hello:: hello) )
74
+ . route ( "/index" , get ( hello:: todos_index) . post ( hello:: todos_create) )
75
+ . route ( "/todos" , get ( hello:: todos_index) . post ( hello:: todos_create) )
76
+ . route ( "/todos/:id" , patch ( hello:: todos_update) . delete ( hello:: todos_delete) )
75
77
// Add middleware to all routes
76
78
. layer (
77
79
ServiceBuilder :: new ( )
@@ -106,87 +108,3 @@ async fn main() {
106
108
. await
107
109
. unwrap ( ) ;
108
110
}
109
-
110
- // The query parameters for todos index
111
- #[ derive( Debug , Deserialize , Default ) ]
112
- pub struct Pagination {
113
- pub offset : Option < usize > ,
114
- pub limit : Option < usize > ,
115
- }
116
-
117
- async fn todos_index (
118
- pagination : Option < Query < Pagination > > ,
119
- Extension ( db) : Extension < Db > ,
120
- ) -> impl IntoResponse {
121
- let todos = db. read ( ) . unwrap ( ) ;
122
-
123
- let Query ( pagination) = pagination. unwrap_or_default ( ) ;
124
-
125
- let todos = todos
126
- . values ( )
127
- . skip ( pagination. offset . unwrap_or ( 0 ) )
128
- . take ( pagination. limit . unwrap_or ( usize:: MAX ) )
129
- . cloned ( )
130
- . collect :: < Vec < _ > > ( ) ;
131
-
132
- Json ( todos)
133
- }
134
-
135
- #[ derive( Debug , Deserialize ) ]
136
- struct CreateTodo {
137
- text : String ,
138
- }
139
-
140
- async fn todos_create (
141
- Json ( input) : Json < CreateTodo > ,
142
- Extension ( db) : Extension < Db > ,
143
- ) -> impl IntoResponse {
144
- let todo = Todo { id : Uuid :: new_v4 ( ) , text : input. text , completed : false } ;
145
-
146
- db. write ( ) . unwrap ( ) . insert ( todo. id , todo. clone ( ) ) ;
147
-
148
- ( StatusCode :: CREATED , Json ( todo) )
149
- }
150
-
151
- #[ derive( Debug , Deserialize ) ]
152
- struct UpdateTodo {
153
- text : Option < String > ,
154
- completed : Option < bool > ,
155
- }
156
-
157
- async fn todos_update (
158
- Path ( id) : Path < Uuid > ,
159
- Json ( input) : Json < UpdateTodo > ,
160
- Extension ( db) : Extension < Db > ,
161
- ) -> Result < impl IntoResponse , StatusCode > {
162
- let mut todo = db. read ( ) . unwrap ( ) . get ( & id) . cloned ( ) . ok_or ( StatusCode :: NOT_FOUND ) ?;
163
-
164
- if let Some ( text) = input. text {
165
- todo. text = text;
166
- }
167
-
168
- if let Some ( completed) = input. completed {
169
- todo. completed = completed;
170
- }
171
-
172
- db. write ( ) . unwrap ( ) . insert ( todo. id , todo. clone ( ) ) ;
173
-
174
- Ok ( Json ( todo) )
175
- }
176
-
177
- async fn todos_delete ( Path ( id) : Path < Uuid > , Extension ( db) : Extension < Db > ) -> impl IntoResponse {
178
- if db. write ( ) . unwrap ( ) . remove ( & id) . is_some ( ) {
179
- StatusCode :: NO_CONTENT
180
- } else {
181
- StatusCode :: NOT_FOUND
182
- }
183
- }
184
-
185
- type Db = Arc < RwLock < HashMap < Uuid , Todo > > > ;
186
-
187
- #[ derive( Debug , Serialize , Clone ) ]
188
- struct Todo {
189
- id : Uuid ,
190
- text : String ,
191
- completed : bool ,
192
- }
0 commit comments