From d6bdb8f019fde792841480eaa4a109546da15fbe Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Sun, 24 Oct 2021 22:19:28 +0300 Subject: [PATCH] login trought login service --- server/src/api.rs | 18 +++++++++++++----- server/src/api/auth.rs | 16 +++++++++++----- server/src/main.rs | 3 ++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/server/src/api.rs b/server/src/api.rs index e7b3195..e81c69a 100644 --- a/server/src/api.rs +++ b/server/src/api.rs @@ -9,11 +9,19 @@ mod auth; use serde::Serialize; -pub struct API {} +use parking_lot::RwLock; +use std::sync::Arc; + + +pub struct API { + user_service: Arc> +} impl API { - fn new() -> API { - API {} + pub fn new() -> API { + API { + user_service: Arc::new(RwLock::new(user_service::UserService::new())) + } } fn builder(s: T) -> Result, warp::http::Error> { @@ -40,9 +48,9 @@ impl API { rs } - pub async fn serve() { + pub async fn serve(&self) { let lg = warp::any().map(move || API::builder("not found")); - let login = auth::get_route(); + let login = auth::get_route(self.user_service.clone()); println!("Start server on 8000 port"); warp::serve(login.or(lg)).run(([127, 0, 0, 1], 8000)).await; diff --git a/server/src/api/auth.rs b/server/src/api/auth.rs index c1b64fb..6de6a17 100644 --- a/server/src/api/auth.rs +++ b/server/src/api/auth.rs @@ -10,18 +10,24 @@ use serde::Serialize; use crate::api; +use parking_lot::RwLock; +use std::sync::Arc; + #[derive(Serialize)] struct SigninResp { token: user_service::AccessToken, } -pub fn get_route() -> impl Filter + Clone { +pub fn get_route(user_service: Arc>) -> impl Filter + Clone { return warp::path!("api" / "auth" / "signin") .and(post()) .and(warp::body::json()) - .map(|user: user_service::User| { - api::API::json(&SigninResp { - token: "asdad".to_string(), - }) + .map(move |user: user_service::User| { + let us = user_service.write().login(&user); + match us { + Some(token) => api::API::json(&SigninResp { token }), + None => api::API::json(&SigninResp { token: "no token".to_string() }) + } + }); } diff --git a/server/src/main.rs b/server/src/main.rs index 94858c2..b63a824 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -2,5 +2,6 @@ mod api; #[tokio::main] async fn main() { - api::API::serve().await; + let api = api::API::new(); + api.serve().await; }