2020-05-21 13:28:07 +00:00
|
|
|
use std::error::Error;
|
|
|
|
|
2020-05-21 13:43:53 +00:00
|
|
|
use crate::controllers::routes::Method::{GET, POST};
|
2020-05-25 11:25:51 +00:00
|
|
|
use crate::controllers::{server_controller, account_controller, user_controller};
|
2020-05-21 13:28:07 +00:00
|
|
|
use crate::data::http_request_handler::HttpRequestHandler;
|
|
|
|
|
|
|
|
/// Project routes
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum Method {
|
|
|
|
GET,
|
|
|
|
POST,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Define types
|
|
|
|
pub type RequestResult = Result<(), Box<dyn Error>>;
|
|
|
|
pub type RequestProcess = Box<dyn Fn(&mut HttpRequestHandler) -> RequestResult>;
|
|
|
|
|
|
|
|
pub struct Route {
|
|
|
|
/// The Verb used for the request
|
|
|
|
pub method: Method,
|
|
|
|
|
|
|
|
/// The URI of the request, with the leading "/"
|
|
|
|
pub uri: &'static str,
|
|
|
|
|
|
|
|
/// If set to true, unauthenticated requests will be rejected
|
|
|
|
pub need_login: bool,
|
|
|
|
|
|
|
|
/// The function called to process a request
|
|
|
|
pub func: RequestProcess,
|
|
|
|
}
|
|
|
|
|
2020-05-21 13:39:20 +00:00
|
|
|
impl Route {
|
|
|
|
|
|
|
|
pub fn get_without_login(uri: &'static str, func: RequestProcess) -> Route {
|
|
|
|
Route {
|
|
|
|
method: GET,
|
|
|
|
need_login: false,
|
|
|
|
uri,
|
|
|
|
func
|
|
|
|
}
|
|
|
|
}
|
2020-05-21 13:43:53 +00:00
|
|
|
|
|
|
|
pub fn post_without_login(uri: &'static str, func: RequestProcess) -> Route {
|
|
|
|
Route {
|
|
|
|
method: POST,
|
|
|
|
need_login: false,
|
|
|
|
uri,
|
|
|
|
func
|
|
|
|
}
|
|
|
|
}
|
2020-05-24 15:57:47 +00:00
|
|
|
|
|
|
|
pub fn post(uri: &'static str, func: RequestProcess) -> Route {
|
|
|
|
Route {
|
|
|
|
method: POST,
|
|
|
|
need_login: true,
|
|
|
|
uri,
|
|
|
|
func
|
|
|
|
}
|
|
|
|
}
|
2020-05-21 13:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 13:28:07 +00:00
|
|
|
/// Get the list of routes available
|
|
|
|
pub fn get_routes() -> Vec<Route> {
|
|
|
|
vec![
|
|
|
|
// Server meta routes
|
2020-05-21 13:43:53 +00:00
|
|
|
Route::get_without_login("/", Box::new(server_controller::main_index)),
|
|
|
|
|
|
|
|
// Account controller
|
|
|
|
Route::post_without_login("/account/login", Box::new(account_controller::login_user)),
|
2020-05-23 12:08:22 +00:00
|
|
|
Route::post_without_login("/user/connectUSER", Box::new(account_controller::login_user)),
|
2020-05-24 15:57:47 +00:00
|
|
|
|
2020-05-24 17:19:07 +00:00
|
|
|
Route::post("/account/logout", Box::new(account_controller::logout_user)),
|
|
|
|
Route::post("/user/disconnectUSER", Box::new(account_controller::logout_user)),
|
|
|
|
|
2020-05-24 15:57:47 +00:00
|
|
|
Route::post("/account/id", Box::new(account_controller::user_id)),
|
|
|
|
Route::post("/user/getCurrentUserID", Box::new(account_controller::user_id)),
|
2020-05-25 11:25:51 +00:00
|
|
|
|
|
|
|
// User controller
|
|
|
|
Route::post_without_login("/user/getInfo", Box::new(user_controller::get_single)),
|
|
|
|
Route::post_without_login("/user/getInfos", Box::new(user_controller::get_single)),
|
2020-05-27 11:20:08 +00:00
|
|
|
|
|
|
|
Route::post_without_login("/user/getInfoMultiple", Box::new(user_controller::get_multiple)),
|
|
|
|
Route::post_without_login("/user/getInfosMultiple", Box::new(user_controller::get_multiple)),
|
2020-05-27 11:28:00 +00:00
|
|
|
|
|
|
|
Route::post_without_login("/user/getAdvancedUserInfo", Box::new(user_controller::get_advanced_info)),
|
|
|
|
Route::post_without_login("/user/getAdvancedUserInfos", Box::new(user_controller::get_advanced_info)),
|
2020-05-21 13:28:07 +00:00
|
|
|
]
|
|
|
|
}
|