-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
main.rs
156 lines (141 loc) · 4.08 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::collections::HashMap;
use poem::{listener::TcpListener, route};
use poem_openapi::{
payload::Json, types::Password, ApiResponse, Object, OpenApi, OpenApiService, Tags,
};
use tokio::sync::Mutex;
#[derive(Tags)]
enum ApiTags {
/// Operations about user
User,
}
/// Create user schema
#[derive(Debug, Object, Clone, Eq, PartialEq)]
struct User {
/// Id
id: i64,
/// Name
#[oai(max_length = 64)]
name: String,
/// Password
#[oai(max_length = 32)]
password: Password,
}
/// Update user schema
#[derive(Debug, Object, Clone, Eq, PartialEq)]
struct UpdateUser {
/// Name
name: Option<String>,
/// Password
password: Option<Password>,
}
#[derive(ApiResponse)]
enum CreateUserResponse {
/// Returns when the user is successfully created.
#[oai(status = 200)]
Ok,
/// Returns when the user already exists.
#[oai(status = 409)]
UserAlreadyExists,
}
#[derive(ApiResponse)]
enum FindUserResponse {
/// Return the specified user.
#[oai(status = 200)]
Ok(Json<User>),
/// Return when the specified user is not found.
#[oai(status = 404)]
NotFound,
}
#[derive(ApiResponse)]
enum DeleteUserResponse {
/// Returns when the user is successfully deleted.
#[oai(status = 200)]
Ok,
/// Return when the specified user is not found.
#[oai(status = 404)]
NotFound,
}
#[derive(ApiResponse)]
enum UpdateUserResponse {
/// Returns when the user is successfully updated.
#[oai(status = 200)]
Ok,
/// Return when the specified user is not found.
#[oai(status = 404)]
NotFound,
}
#[derive(Default)]
struct Api {
users: Mutex<HashMap<i64, User>>,
}
#[OpenApi]
impl Api {
/// Create a new user
#[oai(path = "/users", method = "post", tag = "ApiTags::User")]
async fn create_user(&self, user: Json<User>) -> CreateUserResponse {
let mut users = self.users.lock().await;
if users.contains_key(&user.0.id) {
return CreateUserResponse::UserAlreadyExists;
}
users.insert(user.0.id, user.0);
CreateUserResponse::Ok
}
/// Find user by id
#[oai(path = "/users/:user_id", method = "get", tag = "ApiTags::User")]
async fn find_user(
&self,
#[oai(name = "user_id", in = "path")] user_id: i64,
) -> FindUserResponse {
let users = self.users.lock().await;
match users.get(&user_id) {
Some(user) => FindUserResponse::Ok(Json(user.clone())),
None => FindUserResponse::NotFound,
}
}
/// Delete user by id
#[oai(path = "/users/:user_id", method = "delete", tag = "ApiTags::User")]
async fn delete_user(
&self,
#[oai(name = "user_id", in = "path")] user_id: i64,
) -> DeleteUserResponse {
let mut users = self.users.lock().await;
match users.remove(&user_id) {
Some(_) => DeleteUserResponse::Ok,
None => DeleteUserResponse::NotFound,
}
}
/// Update user by id
#[oai(path = "/users/:user_id", method = "put", tag = "ApiTags::User")]
async fn put_user(
&self,
#[oai(name = "user_id", in = "path")] user_id: i64,
update: Json<UpdateUser>,
) -> UpdateUserResponse {
let mut users = self.users.lock().await;
match users.get_mut(&user_id) {
Some(user) => {
if let Some(name) = update.0.name {
user.name = name;
}
if let Some(password) = update.0.password {
user.password = password;
}
UpdateUserResponse::Ok
}
None => UpdateUserResponse::NotFound,
}
}
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let listener = TcpListener::bind("127.0.0.1:3000");
let api_service = OpenApiService::new(Api::default())
.title("Users")
.server("http://localhost:3000/api");
let ui = api_service.swagger_ui("http://localhost:3000");
poem::Server::new(listener)
.await?
.run(route().nest("/api", api_service).nest("/", ui))
.await
}