-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in branch 01-add-registration-login-mock
commit d4ffc0e Author: Rogerio Oliveira <[email protected]> Date: Tue Apr 11 12:36:06 2023 +0200 removed associated function to create an account; commit 929ece9 Author: Rogerio Oliveira <[email protected]> Date: Tue Apr 11 00:16:31 2023 +0200 added mvc structure (only controllers and routers); added controllers and routers to access login and registration mocks; fixed the warn on "impl Reply" [rust-lang/rust#107729]; Changes to be committed: modified: Cargo.lock modified: Cargo.toml new file: src/controllers/authentication/login.rs new file: src/controllers/authentication/mod.rs new file: src/controllers/authentication/registration.rs new file: src/controllers/mod.rs modified: src/lib.rs new file: src/models/account.rs new file: src/models/mod.rs new file: src/routes/authentication.rs new file: src/routes/mod.rs
- Loading branch information
1 parent
9e060e0
commit 6219645
Showing
11 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::fmt::Display; | ||
|
||
use warp::{Reply, Rejection, reply, hyper::StatusCode, reject::{self, Reject}}; | ||
|
||
use crate::models::account::Account; | ||
|
||
#[derive(Debug)] | ||
enum AccountError { | ||
WrongCredentials, | ||
} | ||
|
||
impl Reject for AccountError {} | ||
|
||
impl Display for AccountError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match &*self { | ||
AccountError::WrongCredentials => write!(f, "Wrong Credentials"), | ||
} | ||
} | ||
} | ||
|
||
pub async fn login(account: Account) -> Result<impl Reply, Rejection> { | ||
if account.email == "admin" && account.password == "password" { | ||
Ok(reply::with_status("you're logged", StatusCode::OK)) | ||
} else { | ||
Err(reject::custom(AccountError::WrongCredentials)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod login; | ||
mod registration; | ||
|
||
pub use login::login; | ||
pub use registration::registration; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use warp::{Reply, Rejection, reject::Reject, reply, hyper::StatusCode}; | ||
|
||
use crate::models::account::Account; | ||
|
||
#[derive(Debug)] | ||
enum RegistrationError { | ||
UserExists, | ||
} | ||
|
||
impl Reject for RegistrationError {} | ||
|
||
pub async fn registration(account: Account) -> Result<impl Reply, Rejection> { | ||
if account.email == "admin" { | ||
Err(warp::reject::custom(RegistrationError::UserExists)) | ||
} else { | ||
Ok(reply::with_status("user created", StatusCode::CREATED)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod authentication; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
use routes::authentication::{login_route, registration_route}; | ||
use warp::Filter; | ||
|
||
mod routes; | ||
mod models; | ||
mod controllers; | ||
|
||
pub async fn run() { | ||
let hello_route = warp::path!("hello" / String) | ||
.map(|name| format!("hello {}", name)); | ||
|
||
let routes = login_route() | ||
.or(registration_route()); | ||
|
||
let cors = warp::cors() | ||
.allow_any_origin() | ||
.allow_methods(vec!["POST"]); | ||
|
||
warp::serve(hello_route) | ||
warp::serve(routes.with(cors)) | ||
.run(([127, 0, 0, 1], 8081)) | ||
.await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use serde::{Serialize, Deserialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Account { | ||
pub email: String, | ||
pub password: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod account; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use warp::{Filter, Reply, Rejection, path}; | ||
|
||
use crate::controllers::authentication::{login, registration}; | ||
|
||
pub fn login_route() -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone { | ||
warp::post() | ||
.and(path("login")) | ||
.and(path::end()) | ||
.and(warp::body::json()) | ||
.and_then(login) | ||
} | ||
|
||
pub fn registration_route() -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone { | ||
warp::post() | ||
.and(path("registration")) | ||
.and(path::end()) | ||
.and(warp::body::json()) | ||
.and_then(registration) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod authentication; |