Skip to content

Commit

Permalink
Merged in branch 01-add-registration-login-mock
Browse files Browse the repository at this point in the history
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
rogerio-cyclomedia committed Apr 11, 2023
1 parent 9e060e0 commit 6219645
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 4 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ edition = "2021"

[dependencies]
warp = "0.3.4"
tokio = { version = "1", features = ["full"] }
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
28 changes: 28 additions & 0 deletions src/controllers/authentication/login.rs
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))
}
}
5 changes: 5 additions & 0 deletions src/controllers/authentication/mod.rs
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;
18 changes: 18 additions & 0 deletions src/controllers/authentication/registration.rs
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))
}
}
1 change: 1 addition & 0 deletions src/controllers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod authentication;
16 changes: 13 additions & 3 deletions src/lib.rs
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;
}
7 changes: 7 additions & 0 deletions src/models/account.rs
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,
}
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod account;
19 changes: 19 additions & 0 deletions src/routes/authentication.rs
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)
}
1 change: 1 addition & 0 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod authentication;

0 comments on commit 6219645

Please sign in to comment.