-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.rs
272 lines (235 loc) · 7.48 KB
/
server.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! Coffee Server Daemon implementation,
//!
//! you should start from here is you want understand
//! the code!
//!
//! Please node that this is just a wrapper for the
//! coffee core crate! The goal is to give an web
//! interface to interact with coffee.
use std::collections::HashMap;
use std::net::ToSocketAddrs;
use std::sync::Arc;
use serde_json::Value;
use tokio::sync::Mutex;
use super::macros::handle_httpd_response;
use coffee_core::coffee::CoffeeManager;
use coffee_lib::plugin_manager::PluginManager;
use coffee_lib::types::request::*;
use actix_web::{App, HttpResponse};
use actix_web::{Error, HttpServer};
use paperclip::actix::HttpResponseWrapper;
use paperclip::actix::{
api_v2_operation,
// If you prefer the macro syntax for defining routes, import the paperclip macros
// get, post, put, delete
// use this instead of actix_web::web
get,
post,
web::{self, Json},
// extension trait for actix_web::App and proc-macro attributes
OpenApiExt,
};
// This struct represents state
struct AppState {
#[allow(dead_code)]
coffee: Arc<Mutex<CoffeeManager>>,
}
/// entry point of the httpd to allow
/// run the server
pub async fn run_httpd<T: ToSocketAddrs>(
coffee: CoffeeManager,
host: T,
) -> Result<(), std::io::Error> {
let rc = Arc::new(Mutex::new(coffee));
HttpServer::new(move || {
let state = AppState { coffee: rc.clone() };
App::new()
.app_data(web::Data::new(state))
.wrap_api()
.service(swagger_api)
.service(coffee_help)
.service(coffee_install)
.service(coffee_remove)
.service(coffee_list)
.service(coffee_remote_add)
.service(coffee_remote_rm)
.service(coffee_remote_list)
.service(coffee_show)
.service(coffee_search)
.service(coffee_list_plugins_in_remote)
.service(coffee_disable)
.service(coffee_enable)
.with_json_spec_at("/api/v1")
.build()
})
.bind(host)?
.run()
.await?;
Ok(())
}
#[api_v2_operation]
#[get("/help")]
async fn coffee_help(
_: web::Data<AppState>,
body: Json<HashMap<String, String>>,
) -> Result<Json<HashMap<String, String>>, Error> {
// FIXME: the json need to be a struct
Ok(body)
}
#[api_v2_operation]
#[post("/install")]
async fn coffee_install(
data: web::Data<AppState>,
body: Json<Install>,
) -> Result<HttpResponse, Error> {
let plugin = &body.plugin;
let try_dynamic = body.try_dynamic;
let mut coffee = data.coffee.lock().await;
let result = coffee.install(plugin, None, false, try_dynamic).await;
handle_httpd_response!(result, "Plugin '{plugin}' installed successfully")
}
#[api_v2_operation]
#[post("/remove")]
async fn coffee_remove(
data: web::Data<AppState>,
body: Json<Remove>,
) -> Result<HttpResponse, Error> {
let plugin = &body.plugin;
let mut coffee = data.coffee.lock().await;
let result = coffee.remove(plugin).await;
handle_httpd_response!(result, "Plugin '{plugin}' removed successfully")
}
#[api_v2_operation]
#[get("/list")]
async fn coffee_list(data: web::Data<AppState>) -> Result<Json<Value>, Error> {
let mut coffee = data.coffee.lock().await;
let result = coffee.list().await;
handle_httpd_response!(result)
}
#[api_v2_operation]
#[post("/remote/add")]
async fn coffee_remote_add(
data: web::Data<AppState>,
body: Json<RemoteAdd>,
) -> Result<HttpResponse, Error> {
let repository_name = &body.repository_name;
let repository_url = &body.repository_url;
let mut coffee = data.coffee.lock().await;
let result = coffee.add_remote(repository_name, repository_url).await;
handle_httpd_response!(result, "Repository '{repository_name}' added successfully")
}
#[api_v2_operation]
#[post("/remote/rm")]
async fn coffee_remote_rm(
data: web::Data<AppState>,
body: Json<RemoteRm>,
) -> Result<HttpResponse, Error> {
let repository_name = &body.repository_name;
let mut coffee = data.coffee.lock().await;
let result = coffee.rm_remote(repository_name).await;
handle_httpd_response!(
result,
"Repository '{repository_name}' removed successfully"
)
}
#[api_v2_operation]
#[get("/remote/list")]
async fn coffee_remote_list(data: web::Data<AppState>) -> Result<Json<Value>, Error> {
let mut coffee = data.coffee.lock().await;
let result = coffee.list_remotes().await;
handle_httpd_response!(result)
}
#[api_v2_operation]
#[get("/remote/list_plugins")]
async fn coffee_list_plugins_in_remote(
data: web::Data<AppState>,
body: Json<RemotePluginsList>,
) -> Result<Json<Value>, Error> {
let repository_name = &body.repository_name;
let coffee = data.coffee.lock().await;
let result = coffee.get_plugins_in_remote(repository_name).await;
handle_httpd_response!(result)
}
#[api_v2_operation]
#[get("/show")]
async fn coffee_show(data: web::Data<AppState>, body: Json<Show>) -> Result<Json<Value>, Error> {
let plugin = &body.plugin;
let mut coffee = data.coffee.lock().await;
let result = coffee.show(plugin).await;
handle_httpd_response!(result)
}
#[api_v2_operation]
#[get("/search")]
async fn coffee_search(
data: web::Data<AppState>,
body: Json<Search>,
) -> Result<Json<Value>, Error> {
let plugin = &body.plugin;
let mut coffee = data.coffee.lock().await;
let result = coffee.search(plugin).await;
handle_httpd_response!(result)
}
#[api_v2_operation]
#[post("/disable")]
async fn coffee_disable(
data: web::Data<AppState>,
body: Json<Disable>,
) -> Result<HttpResponse, Error> {
let plugin = &body.plugin;
let mut coffee = data.coffee.lock().await;
let result = coffee.disable(plugin).await;
handle_httpd_response!(result, "Plugin '{plugin}' disabled successfully")
}
#[api_v2_operation]
#[post("/enable")]
async fn coffee_enable(
data: web::Data<AppState>,
body: Json<Enable>,
) -> Result<HttpResponse, Error> {
let plugin = &body.plugin;
let mut coffee = data.coffee.lock().await;
let result = coffee.enable(plugin).await;
handle_httpd_response!(result, "Plugin '{plugin}' enabled successfully")
}
// this is just a hack to support swagger UI with https://paperclip-rs.github.io/paperclip/
// and the raw html is taken from https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/installation.md#unpkg
#[get("/")]
async fn swagger_api() -> HttpResponseWrapper {
// FIXME: the url need to change here so we should support a better way
let resp = HttpResponse::Ok().body(
r#"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
name="description"
content="SwaggerUI"
/>
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/[email protected]/swagger-ui-bundle.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/swagger-ui-standalone-preset.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: 'http://localhost:8080/api/v1',
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout",
});
};
</script>
</body>
</html>
"#,
);
HttpResponseWrapper(resp)
}