Skip to content

Commit 6ca6cf4

Browse files
committed
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];
1 parent c9029ce commit 6ca6cf4

File tree

11 files changed

+116
-4
lines changed

11 files changed

+116
-4
lines changed

Cargo.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ edition = "2021"
77

88
[dependencies]
99
warp = "0.3.4"
10-
tokio = { version = "1", features = ["full"] }
10+
tokio = { version = "1", features = ["full"] }
11+
serde = { version = "1.0", features = ["derive"] }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::fmt::Display;
2+
3+
use warp::{Reply, Rejection, reply, hyper::StatusCode, reject::{self, Reject}};
4+
5+
use crate::models::account::Account;
6+
7+
#[derive(Debug)]
8+
enum AccountError {
9+
WrongCredentials,
10+
}
11+
12+
impl Reject for AccountError {}
13+
14+
impl Display for AccountError {
15+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16+
match &*self {
17+
AccountError::WrongCredentials => write!(f, "Wrong Credentials"),
18+
}
19+
}
20+
}
21+
22+
pub async fn login(account: Account) -> Result<impl Reply, Rejection> {
23+
if account.email == "admin" && account.password == "password" {
24+
Ok(reply::with_status("you're logged", StatusCode::OK))
25+
} else {
26+
Err(reject::custom(AccountError::WrongCredentials))
27+
}
28+
}

src/controllers/authentication/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod login;
2+
mod registration;
3+
4+
pub use login::login;
5+
pub use registration::registration;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use warp::{Reply, Rejection, reject::Reject, reply, hyper::StatusCode};
2+
3+
use crate::models::account::Account;
4+
5+
#[derive(Debug)]
6+
enum RegistrationError {
7+
UserExists,
8+
}
9+
10+
impl Reject for RegistrationError {}
11+
12+
pub async fn registration(account: Account) -> Result<impl Reply, Rejection> {
13+
if account.email == "admin" {
14+
Err(warp::reject::custom(RegistrationError::UserExists))
15+
} else {
16+
Ok(reply::with_status("user created", StatusCode::CREATED))
17+
}
18+
}

src/controllers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod authentication;

src/lib.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
use routes::authentication::{login_route, registration_route};
12
use warp::Filter;
23

4+
mod routes;
5+
mod models;
6+
mod controllers;
7+
38
pub async fn run() {
4-
let hello_route = warp::path!("hello" / String)
5-
.map(|name| format!("hello {}", name));
9+
10+
let routes = login_route()
11+
.or(registration_route());
12+
13+
let cors = warp::cors()
14+
.allow_any_origin()
15+
.allow_methods(vec!["POST"]);
616

7-
warp::serve(hello_route)
17+
warp::serve(routes.with(cors))
818
.run(([127, 0, 0, 1], 8081))
919
.await;
1020
}

src/models/account.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use serde::{Serialize, Deserialize};
2+
3+
#[derive(Debug, Serialize, Deserialize)]
4+
pub struct Account {
5+
pub email: String,
6+
pub password: String,
7+
}
8+
9+
impl Account {
10+
pub fn new(email: String, password: String) -> Self {
11+
Account { email, password }
12+
}
13+
}

src/models/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod account;

src/routes/authentication.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use warp::{Filter, Reply, Rejection, path};
2+
3+
use crate::controllers::authentication::{login, registration};
4+
5+
pub fn login_route() -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
6+
warp::post()
7+
.and(path("login"))
8+
.and(path::end())
9+
.and(warp::body::json())
10+
.and_then(login)
11+
}
12+
13+
pub fn registration_route() -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
14+
warp::post()
15+
.and(path("registration"))
16+
.and(path::end())
17+
.and(warp::body::json())
18+
.and_then(registration)
19+
}

src/routes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod authentication;

0 commit comments

Comments
 (0)