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
|
use crate::commit::models::Commit;
use crate::config::AppState;
use crate::errors::{AppError, AppErrorResponse, AppErrorType};
use actix_web::http::header;
use actix_web::{web, HttpRequest, HttpResponse, Responder};
use slog::info;
use std::env;
/// Endpoint used for getting all commits
async fn index(state: web::Data<AppState>) -> impl Responder {
info!(state.log, "GET /commit/");
let result = Commit::find_all(state.pool.clone()).await;
match result {
Ok(commits) => HttpResponse::Ok().json(commits),
_ => HttpResponse::BadRequest().json(AppErrorResponse {
detail: "Error trying to read all commits from database"
.to_string(),
}),
}
}
// Endpoint used for getting one commit
async fn get_commit(
state: web::Data<AppState>,
hash: web::Path<(String,)>,
) -> impl Responder {
info!(state.log, "GET /commit/{}/", &hash.0);
let result = Commit::find(state.pool.clone(), hash.0.clone()).await;
result
.map(|commit| HttpResponse::Ok().json(commit))
.map_err(|e| e)
}
/// Endpoint used for delete commitsitory.
/// It uses a SECRET_KEY used like an API key
async fn delete_commit(
req: HttpRequest,
state: web::Data<AppState>,
hash: web::Path<(String,)>,
) -> impl Responder {
match req.headers().get(header::AUTHORIZATION) {
Some(x)
if x.to_str().unwrap()
!= env::var("SECRET_KEY").unwrap_or("".to_string()) =>
{
info!(state.log, "DELETE /commit/{}/ 401", &hash.0);
return Err(AppError {
error_type: AppErrorType::AuthorizationError,
message: Some(
"You must provide a valid Authorization".to_string(),
),
cause: None,
});
}
Some(_) => {}
None => {
info!(state.log, "DELETE /commit/{}/ 400", &hash.0);
return Ok(HttpResponse::BadRequest().body(""));
}
};
let result = Commit::delete(state.pool.clone(), &hash.0).await;
info!(state.log, "DELETE /commit/{}/", &hash.0);
result
.map(|_| HttpResponse::NoContent().body(""))
.map_err(|e| e)
}
/// Routes for commits
pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/commit")
.service(web::resource("{_:/?}").route(web::get().to(index)))
.service(
web::resource("/{hash}{_:/?}")
.route(web::get().to(get_commit))
.route(web::delete().to(delete_commit)),
),
);
}
|