diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-03-19 20:27:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-19 20:27:53 +0100 |
commit | 4249c2e09579d6454e7572111e44c284190a2c8f (patch) | |
tree | fc47bb02aa430accce176903af5e36b81179c87a /src | |
parent | 9a791db0c1a8bd08cf51950ed28066adcef84bca (diff) | |
parent | 8225cba5ce69a073b54e2eeba7c9d466efa107e3 (diff) |
Merge pull request #20 from gico-net/feat/cors
Enable CORS
Diffstat (limited to 'src')
-rw-r--r-- | src/branch/routes.rs | 26 | ||||
-rw-r--r-- | src/commit/models.rs | 8 | ||||
-rw-r--r-- | src/commit/routes.rs | 20 | ||||
-rw-r--r-- | src/email/routes.rs | 5 | ||||
-rw-r--r-- | src/main.rs | 19 | ||||
-rw-r--r-- | src/repository/routes.rs | 14 |
6 files changed, 54 insertions, 38 deletions
diff --git a/src/branch/routes.rs b/src/branch/routes.rs index e46d3ad..bd78f2b 100644 --- a/src/branch/routes.rs +++ b/src/branch/routes.rs @@ -26,9 +26,9 @@ async fn index(state: web::Data<AppState>) -> impl Responder { // Endpoint used for getting branches of a repository async fn get_repo_branch( state: web::Data<AppState>, - repo: web::Path<(String,)>, + repo: web::Path<String>, ) -> impl Responder { - let uuid: Uuid = uuid_from_string(&repo.0); + let uuid: Uuid = uuid_from_string(&repo); info!(state.log, "GET /branch/repo/{}/", &uuid); let result = Branch::find_by_repo(state.pool.clone(), &uuid).await; @@ -42,12 +42,12 @@ async fn get_repo_branch( /// It is a String, casted in an Uuid format. async fn get_branch( state: web::Data<AppState>, - id: web::Path<(String,)>, + id: web::Path<String>, ) -> impl Responder { - let uuid: Uuid = uuid_from_string(&id.0); + let uuid: Uuid = uuid_from_string(&id); let result = Branch::find(state.pool.clone(), &uuid).await; - info!(state.log, "GET /branch/{}/", id.0); + info!(state.log, "GET /branch/{}/", id); // `map_err` is also used when repo is not found result @@ -60,15 +60,15 @@ async fn get_branch( async fn delete_branch( req: HttpRequest, state: web::Data<AppState>, - id: web::Path<(String,)>, + id: web::Path<String>, ) -> impl Responder { - let uuid: Uuid = uuid_from_string(&id.0); + let uuid: Uuid = uuid_from_string(&id); 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 /branch/{}/ 401", id.0); + info!(state.log, "DELETE /branch/{}/ 401", id); return Err(AppError { error_type: AppErrorType::AuthorizationError, message: Some( @@ -79,13 +79,13 @@ async fn delete_branch( } Some(_) => {} None => { - info!(state.log, "DELETE /branch/{}/ 400", id.0); + info!(state.log, "DELETE /branch/{}/ 400", id); return Ok(HttpResponse::BadRequest().body("")); } }; let result = Branch::delete(state.pool.clone(), &uuid).await; - info!(state.log, "DELETE /branch/{}/", id.0); + info!(state.log, "DELETE /branch/{}/", id); result .map(|_| HttpResponse::NoContent().body("")) @@ -96,13 +96,13 @@ async fn delete_branch( pub fn config(cfg: &mut web::ServiceConfig) { cfg.service( web::scope("/branch") - .service(web::resource("{_:/?}").route(web::get().to(index))) + .service(web::resource("/").route(web::get().to(index))) .service( - web::resource("/repo/{repo_id}{_:/?}") + web::resource("/repo/{repo_id}/") .route(web::get().to(get_repo_branch)), ) .service( - web::resource("/{id}{_:/?}") + web::resource("/{id}/") .route(web::get().to(get_branch)) .route(web::delete().to(delete_branch)), ), diff --git a/src/commit/models.rs b/src/commit/models.rs index b2aa2d2..987bcd2 100644 --- a/src/commit/models.rs +++ b/src/commit/models.rs @@ -27,7 +27,7 @@ impl Commit { pub async fn find_all(pool: Pool) -> Result<Vec<Commit>, AppError> { let client = get_client(pool.clone()).await.unwrap(); let statement = client - .prepare("SELECT * FROM commit ORDER BY date DESC") + .prepare("SELECT * FROM commit ORDER BY date DESC LIMIT 300") .await?; let commits = client @@ -111,12 +111,12 @@ impl Commit { "('{}', {}, E'{}', '{}', '{}', E'{}', '{}', E'{}', '{}'),", commit.hash, tree, - commit.text.replace("'", "\\'"), + commit.text.replace("\\'", "'").replace("'", "\\'"), commit.date, commit.author_email, - commit.author_name.replace("'", "\\'"), + commit.author_name.replace("\\'", "'").replace("'", "\\'"), commit.committer_email, - commit.committer_name.replace("'", "\\'"), + commit.committer_name.replace("\\'", "'").replace("'", "\\'"), commit.repository_url )[..] } diff --git a/src/commit/routes.rs b/src/commit/routes.rs index e49f698..95c0faa 100644 --- a/src/commit/routes.rs +++ b/src/commit/routes.rs @@ -23,11 +23,11 @@ async fn index(state: web::Data<AppState>) -> impl Responder { // Endpoint used for getting one commit async fn get_commit( state: web::Data<AppState>, - hash: web::Path<(String,)>, + hash: web::Path<String>, ) -> impl Responder { - info!(state.log, "GET /commit/{}/", &hash.0); + info!(state.log, "GET /commit/{}/", &hash); - let result = Commit::find(state.pool.clone(), hash.0.clone()).await; + let result = Commit::find(state.pool.clone(), hash.clone()).await; result .map(|commit| HttpResponse::Ok().json(commit)) @@ -39,14 +39,14 @@ async fn get_commit( async fn delete_commit( req: HttpRequest, state: web::Data<AppState>, - hash: web::Path<(String,)>, + 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); + info!(state.log, "DELETE /commit/{}/ 401", &hash); return Err(AppError { error_type: AppErrorType::AuthorizationError, message: Some( @@ -57,13 +57,13 @@ async fn delete_commit( } Some(_) => {} None => { - info!(state.log, "DELETE /commit/{}/ 400", &hash.0); + info!(state.log, "DELETE /commit/{}/ 400", &hash); return Ok(HttpResponse::BadRequest().body("")); } }; - let result = Commit::delete(state.pool.clone(), &hash.0).await; - info!(state.log, "DELETE /commit/{}/", &hash.0); + let result = Commit::delete(state.pool.clone(), &hash).await; + info!(state.log, "DELETE /commit/{}/", &hash); result .map(|_| HttpResponse::NoContent().body("")) @@ -74,9 +74,9 @@ async fn delete_commit( pub fn config(cfg: &mut web::ServiceConfig) { cfg.service( web::scope("/commit") - .service(web::resource("{_:/?}").route(web::get().to(index))) + .service(web::resource("/").route(web::get().to(index))) .service( - web::resource("/{hash}{_:/?}") + web::resource("/{hash}/") .route(web::get().to(get_commit)) .route(web::delete().to(delete_commit)), ), diff --git a/src/email/routes.rs b/src/email/routes.rs index 14299eb..003d01c 100644 --- a/src/email/routes.rs +++ b/src/email/routes.rs @@ -63,13 +63,12 @@ pub fn config(cfg: &mut web::ServiceConfig) { cfg.service( web::scope("/email") .service( - web::resource("{_:/?}") + web::resource("/") .route(web::get().to(index)) .route(web::post().to(create_email)), ) .service( - web::resource("/search{_:/?}") - .route(web::get().to(search_email)), + web::resource("/search/").route(web::get().to(search_email)), ), ); } diff --git a/src/main.rs b/src/main.rs index 2e499f9..6ac5d2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,9 +10,11 @@ mod commit; mod email; mod repository; -use actix_web::{middleware, App, HttpServer}; +use actix_cors::Cors; +use actix_web::{http::header, middleware, App, HttpServer}; use dotenv::dotenv; use slog::info; +use std::env; use tokio_postgres::NoTls; use crate::config::{AppState, Config}; @@ -39,6 +41,21 @@ async fn main() -> std::io::Result<()> { log: log.clone(), }) .wrap(middleware::Logger::default()) + .wrap( + Cors::default() + .allowed_origin( + &env::var("CLIENT") + .unwrap_or("http://localhost:8080".to_string())[..] + ) + .allowed_methods(vec!["GET", "POST", "DELETE"]) + .allowed_headers(vec![ + header::AUTHORIZATION, + header::ACCEPT, + ]) + .allowed_header(header::CONTENT_TYPE) + .supports_credentials() + .max_age(3600), + ) .configure(repository::routes::config) .configure(email::routes::config) .configure(commit::routes::config) diff --git a/src/repository/routes.rs b/src/repository/routes.rs index 32443b1..d189584 100644 --- a/src/repository/routes.rs +++ b/src/repository/routes.rs @@ -27,15 +27,15 @@ async fn index(state: web::Data<AppState>) -> impl Responder { /// It is a String, casted in an Uuid format. async fn get_repo( state: web::Data<AppState>, - id: web::Path<(String,)>, + id: web::Path<String>, ) -> impl Responder { // I have to match the &id.0 because if it's not a valid Uuid, the server // must response "Repository not found". // If I pass a not valid Uuid to Repository::find() it raises an error. - let uuid: Uuid = uuid_from_string(&id.0); + let uuid: Uuid = uuid_from_string(&id); let result = Repository::find(state.pool.clone(), &uuid).await; - info!(state.log, "GET /repo/{}/", id.0); + info!(state.log, "GET /repo/{}/", id); // `map_err` is also used when repo is not found result @@ -48,9 +48,9 @@ async fn get_repo( async fn delete_repo( req: HttpRequest, state: web::Data<AppState>, - id: web::Path<(String,)>, + id: web::Path<String>, ) -> impl Responder { - let uuid: Uuid = uuid_from_string(&id.0); + let uuid: Uuid = uuid_from_string(&id); match req.headers().get(header::AUTHORIZATION) { Some(x) if x.to_str().unwrap() @@ -102,12 +102,12 @@ pub fn config(cfg: &mut web::ServiceConfig) { cfg.service( web::scope("/repo") .service( - web::resource("{_:/?}") + web::resource("/") .route(web::get().to(index)) .route(web::post().to(create_repo)), ) .service( - web::resource("/{id}{_:/?}") + web::resource("/{id}/") .route(web::get().to(get_repo)) .route(web::delete().to(delete_repo)), ), |