-
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.
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];
- Loading branch information
1 parent
c9029ce
commit 6ca6cf4
Showing
11 changed files
with
116 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,13 @@ | ||
use serde::{Serialize, Deserialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Account { | ||
pub email: String, | ||
pub password: String, | ||
} | ||
|
||
impl Account { | ||
pub fn new(email: String, password: String) -> Self { | ||
Account { email, password } | ||
} | ||
} |
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; |