Browse Source

format code

pull/25/head
Alexey Velikiy 3 years ago
parent
commit
6f12671d39
  1. 44
      server/src/api.rs
  2. 16
      server/src/api/auth.rs
  3. 3
      server/src/main.rs
  4. 5
      server/src/services/data_service.rs
  5. 21
      server/src/services/user_service.rs

44
server/src/api.rs

@ -1,28 +1,19 @@
use hastic::services::user_service;
use warp::filters::method::post;
use warp::http::HeaderValue;
use warp::hyper::Body;
use warp::{Rejection, Reply, body};
use warp::{Filter, http::Response };
use warp::filters::method::post;
use warp::{body, Rejection, Reply};
use warp::{http::Response, Filter};
mod auth;
use serde::{ Serialize };
pub struct API {
}
use serde::Serialize;
pub struct API {}
impl API {
fn new() -> API {
API{}
API {}
}
fn builder<T>(s: T) -> Result<Response<T>, warp::http::Error> {
@ -30,7 +21,7 @@ impl API {
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE")
.header("Access-Control-Allow-Headers", "*")
.body(s)
.body(s);
}
fn json<T: Serialize>(t: &T) -> Response<Body> {
@ -38,21 +29,22 @@ impl API {
let mut rs = j.into_response();
let hs = rs.headers_mut();
hs.insert("Access-Control-Allow-Origin", HeaderValue::from_static("*"));
hs.insert("Access-Control-Allow-Methods", HeaderValue::from_static("POST, GET, OPTIONS, DELETE"));
hs.insert("Access-Control-Allow-Headers", HeaderValue::from_static("*"));
hs.insert(
"Access-Control-Allow-Methods",
HeaderValue::from_static("POST, GET, OPTIONS, DELETE"),
);
hs.insert(
"Access-Control-Allow-Headers",
HeaderValue::from_static("*"),
);
rs
}
pub async fn serve() {
let lg = warp::any().map(move || API::builder("not found") );
let lg = warp::any().map(move || API::builder("not found"));
let login = auth::get_route();
println!("Start server on 8000 port");
warp::serve(login.
or(lg)
)
.run(([127, 0, 0, 1], 8000))
.await;
warp::serve(login.or(lg)).run(([127, 0, 0, 1], 8000)).await;
}
}
}

16
server/src/api/auth.rs

@ -1,27 +1,27 @@
use hastic::services::user_service;
use warp::filters::method::post;
use warp::http::HeaderValue;
use warp::hyper::Body;
use warp::{http::Response, Filter};
use warp::{Rejection, Reply};
use warp::filters::method::post;
use warp::{Filter, http::Response };
use serde::{ Serialize };
use serde::Serialize;
use crate::api;
#[derive(Serialize)]
struct SigninResp {
token: user_service::AccessToken
token: user_service::AccessToken,
}
pub fn get_route() -> 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() })
api::API::json(&SigninResp {
token: "asdad".to_string(),
})
});
}
}

3
server/src/main.rs

@ -1,7 +1,6 @@
mod api;
#[tokio::main]
async fn main() {
api::API::serve().await;
}
}

5
server/src/services/data_service.rs

@ -1,4 +1 @@
struct DataService {
}
struct DataService {}

21
server/src/services/user_service.rs

@ -1,10 +1,9 @@
use std::{collections::HashSet};
use std::collections::HashSet;
use serde::{ Deserialize, Serialize };
use serde::{Deserialize, Serialize};
use std::iter::repeat_with;
pub type AccessToken = String;
const TOKEN_LENGTH: usize = 20;
@ -12,28 +11,30 @@ const TOKEN_LENGTH: usize = 20;
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct User {
pub username: String,
pub password: String
pub password: String,
}
pub struct UserService {
tokens: HashSet<AccessToken>
tokens: HashSet<AccessToken>,
}
impl UserService {
pub fn new() -> UserService {
UserService{
tokens: HashSet::new()
UserService {
tokens: HashSet::new(),
}
}
pub fn login(&mut self, user: &User) -> Option<AccessToken> {
if user.username == "admin" && user.password == "admin" {
let token: AccessToken = repeat_with(fastrand::alphanumeric).take(TOKEN_LENGTH).collect();
let token: AccessToken = repeat_with(fastrand::alphanumeric)
.take(TOKEN_LENGTH)
.collect();
self.tokens.insert(token.to_owned());
return Some(token);
}
return None;
}
pub fn check_token(&self, username: &String, token: &AccessToken ) -> bool {
pub fn check_token(&self, username: &String, token: &AccessToken) -> bool {
return self.tokens.contains(token);
}
}
}

Loading…
Cancel
Save