Browse Source

login trought login service

pull/25/head
Alexey Velikiy 3 years ago
parent
commit
d6bdb8f019
  1. 18
      server/src/api.rs
  2. 16
      server/src/api/auth.rs
  3. 3
      server/src/main.rs

18
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<RwLock<user_service::UserService>>
}
impl API {
fn new() -> API {
API {}
pub fn new() -> API {
API {
user_service: Arc::new(RwLock::new(user_service::UserService::new()))
}
}
fn builder<T>(s: T) -> Result<Response<T>, 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;

16
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<Extract = impl Reply, Error = Rejection> + Clone {
pub fn get_route(user_service: Arc<RwLock<user_service::UserService>>) -> impl Filter<Extract = impl Reply, Error = Rejection> + 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() })
}
});
}

3
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;
}

Loading…
Cancel
Save