parent
76921e8854
commit
5e77c77559
File diff suppressed because it is too large
Load Diff
@ -1,3 +1,6 @@
|
||||
#[macro_use]
|
||||
extern crate rouille;
|
||||
|
||||
pub mod database;
|
||||
pub mod server;
|
||||
pub mod utils;
|
||||
|
@ -0,0 +1,86 @@
|
||||
use crate::database::Database;
|
||||
use crate::server::messages::LoginMessage;
|
||||
use crate::utils::error::DBError;
|
||||
use rouille::{Request, Response, Server};
|
||||
use serde::export::Formatter;
|
||||
use std::error::Error;
|
||||
use std::fmt::{self, Display};
|
||||
use std::io::Read;
|
||||
|
||||
const LISTEN_ADDRESS: &str = "HTTP_SERVER_ADDRESS";
|
||||
const DEFAULT_LISTEN_ADDRESS: &str = "127.0.0.1:8080";
|
||||
|
||||
pub struct UserHttpServer {
|
||||
database: Database,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct HTTPError {
|
||||
message: String,
|
||||
code: usize,
|
||||
}
|
||||
|
||||
impl Display for HTTPError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
impl Error for HTTPError {}
|
||||
|
||||
impl From<DBError> for HTTPError {
|
||||
fn from(other: DBError) -> Self {
|
||||
Self {
|
||||
message: other.to_string(),
|
||||
code: 400,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HTTPError {
|
||||
pub fn new(message: String, code: usize) -> Self {
|
||||
Self { message, code }
|
||||
}
|
||||
}
|
||||
|
||||
type HTTPResult<T> = Result<T, HTTPError>;
|
||||
|
||||
impl UserHttpServer {
|
||||
pub fn new(database: &Database) -> Self {
|
||||
Self {
|
||||
database: Database::clone(database),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start(&self) {
|
||||
let listen_address =
|
||||
dotenv::var(LISTEN_ADDRESS).unwrap_or(DEFAULT_LISTEN_ADDRESS.to_string());
|
||||
let database = Database::clone(&self.database);
|
||||
let server = Server::new(listen_address, move |request| {
|
||||
router!(request,
|
||||
(POST) (/login) => {
|
||||
Self::login(&database, request).unwrap_or_else(|e|Response::text(e.to_string()))
|
||||
},
|
||||
_ => Response::empty_404()
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
server.run()
|
||||
}
|
||||
|
||||
fn login(database: &Database, request: &Request) -> HTTPResult<Response> {
|
||||
if let Some(mut data) = request.data() {
|
||||
let mut data_string = String::new();
|
||||
data.read_to_string(&mut data_string)
|
||||
.map_err(|_| HTTPError::new("Failed to read request data".to_string(), 500))?;
|
||||
let login_request: LoginMessage = serde_json::from_str(data_string.as_str())
|
||||
.map_err(|e| HTTPError::new(e.to_string(), 400))?;
|
||||
let tokens = database
|
||||
.users
|
||||
.create_get_tokens(login_request.email, login_request.password)?;
|
||||
|
||||
Ok(Response::json(&tokens))
|
||||
} else {
|
||||
Err(HTTPError::new("Missing Request Data".to_string(), 400))
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
pub mod http_server;
|
||||
pub mod messages;
|
||||
pub mod rpc_methods;
|
||||
pub mod user_rpc;
|
||||
|
Loading…
Reference in New Issue