Browse Source

server++

pull/25/head
Alexey Velikiy 3 years ago
parent
commit
ce68ef261b
  1. 48
      server/src/api.rs
  2. 1
      server/src/lib.rs
  3. 26
      server/src/main.rs
  4. 10
      server/src/user_service.rs

48
server/src/api.rs

@ -0,0 +1,48 @@
use warp::{Filter, http::Response };
use warp::filters::method::post;
use crate::user_service;
pub struct API {
}
fn json_body() -> impl Filter<Extract = (user_service::User,), Error = warp::Rejection> + Clone {
// When accepting a body, we want a JSON body
// (and to reject huge payloads)...
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}
impl API {
fn new() -> API {
API{}
}
fn builder(s: &str) -> Result<Response<String>, warp::http::Error> {
return Response::builder()
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE")
.header("Access-Control-Allow-Headers", "*")
.body(s.to_owned())
}
pub async fn serve() {
let lg = warp::any().map(move || API::builder("not found") );
let login = warp::any()
// path!("api" / "auth" / "signin")
.and(post())
.and(json_body())
.map(|user: user_service::User| {
let s = format!("Hello, {}!", &user.username);
API::builder(&s)
});
println!("Start server on 8000 port");
warp::serve(login.or(lg))
.run(([127, 0, 0, 1], 8000))
.await;
}
}

1
server/src/lib.rs

@ -0,0 +1 @@
pub mod user_service;

26
server/src/main.rs

@ -3,31 +3,11 @@ use warp::filters::method::post;
mod user_service;
mod api;
fn json_body() -> impl Filter<Extract = (user_service::User,), Error = warp::Rejection> + Clone {
// When accepting a body, we want a JSON body
// (and to reject huge payloads)...
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}
#[tokio::main]
async fn main() {
let routes = warp::any().map(|| {
Response::builder()
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE")
.header("Access-Control-Allow-Headers", "*")
.body("and a custom body")
// "Hello, World!"
});
let hello = warp::path!("api" / "auth" / "signin")
.and(post())
.and(json_body())
.map(|user: user_service::User| {
format!("Hello, {}!", &user.username)
});
warp::serve(routes)
.run(([127, 0, 0, 1], 8000))
.await;
api::API::serve().await;
}

10
server/src/user_service.rs

@ -1,5 +1,8 @@
use serde::{ Deserialize, Serialize };
pub type AccessToken = String;
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct User {
pub username: String,
@ -14,5 +17,10 @@ impl UserService {
pub fn new() -> UserService {
UserService{}
}
pub fn login(user: &User) -> Option<AccessToken> {
if user.username == "admin" && user.password == "admin" {
return Some("asdsadsad".to_string());
}
return None;
}
}
Loading…
Cancel
Save