Initial Commit
This commit is contained in:
		
						commit
						a526b83697
					
				
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,2 @@ | ||||||
|  | /target | ||||||
|  | .env | ||||||
							
								
								
									
										1445
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										1445
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										11
									
								
								Cargo.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Cargo.toml
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,11 @@ | ||||||
|  | [package] | ||||||
|  | name = "api" | ||||||
|  | version = "0.1.0" | ||||||
|  | edition = "2021" | ||||||
|  | 
 | ||||||
|  | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||||||
|  | 
 | ||||||
|  | [dependencies] | ||||||
|  | actix-web = "4.3.1" | ||||||
|  | backend = { path = "../backend" } | ||||||
|  | serde_json = "1.0.105" | ||||||
							
								
								
									
										103
									
								
								src/main.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								src/main.rs
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,103 @@ | ||||||
|  | extern crate backend; | ||||||
|  | 
 | ||||||
|  | use actix_web::{get, post, App, HttpResponse, HttpServer, Responder}; | ||||||
|  | use serde_json::Value; | ||||||
|  | 
 | ||||||
|  | macro_rules! get_value_str { | ||||||
|  |     ($i:expr) => { | ||||||
|  |         match $i { | ||||||
|  |             Value::String(s) => s, | ||||||
|  |             _ => String::from("") | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | macro_rules! get_value_int { | ||||||
|  |     ($i:expr) => { | ||||||
|  |         match $i { | ||||||
|  |              Value::Number(n) => n.as_i64().unwrap(), | ||||||
|  |              _ => 0 | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[post("/new_user")] | ||||||
|  | async fn createuser(req_body: String) -> impl Responder { | ||||||
|  |     use backend::UserCreationStatus::*; | ||||||
|  | 
 | ||||||
|  |     let connection = &mut backend::create_connection(); | ||||||
|  | 
 | ||||||
|  |     let v: Value = match serde_json::from_str(&req_body) { | ||||||
|  |         Ok(v) => v, | ||||||
|  |         Err(_) => { | ||||||
|  |             return HttpResponse::BadRequest().body(""); | ||||||
|  |         } | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     let email: &str = v["email"].as_str().unwrap(); | ||||||
|  |     let username: &str = v["username"].as_str().unwrap(); | ||||||
|  |     let password: &str = v["password"].as_str().unwrap(); | ||||||
|  | 
 | ||||||
|  |     match backend::add_user( | ||||||
|  |         String::from(username), | ||||||
|  |         String::from(email), | ||||||
|  |         String::from(password), | ||||||
|  |         connection, | ||||||
|  |     ) { | ||||||
|  |         Succesful => HttpResponse::Ok().body("Succesful"), | ||||||
|  |         UserExists => HttpResponse::Conflict().body("User Exists already"), | ||||||
|  |         QueryError(_) => HttpResponse::InternalServerError().body("Query Error"), | ||||||
|  |         DatabaseError(_) => HttpResponse::InternalServerError().body("Database Error"), | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[post("/check_password")] | ||||||
|  | async fn check_password(req_body: String) -> impl Responder { | ||||||
|  |     use backend::PasswordCheckStatus; | ||||||
|  | 
 | ||||||
|  |     let connection = &mut backend::create_connection(); | ||||||
|  | 
 | ||||||
|  |     let v: Value = match serde_json::from_str(&req_body) { | ||||||
|  |         Ok(v) => v, | ||||||
|  |         Err(_) => { | ||||||
|  |             return HttpResponse::BadRequest().body(""); | ||||||
|  |         } | ||||||
|  |     }; | ||||||
|  |     
 | ||||||
|  |     let id = get_value_int!(&v["id"]); | ||||||
|  |     let pass = get_value_str!(v["password"].clone()); | ||||||
|  | 
 | ||||||
|  |     match backend::check_password(id, pass, connection) { | ||||||
|  |         PasswordCheckStatus::Correct => HttpResponse::Ok().body(""), | ||||||
|  |         PasswordCheckStatus::Incorrect => HttpResponse::Unauthorized().body(""), | ||||||
|  |         PasswordCheckStatus::UserDoesntExist => HttpResponse::ExpectationFailed().body(""), | ||||||
|  |         PasswordCheckStatus::DatabaseError(_) => HttpResponse::InternalServerError().body(""), | ||||||
|  |         PasswordCheckStatus::QueryError(_) => HttpResponse::InternalServerError().body("") | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[actix_web::main] | ||||||
|  | async fn main() -> std::io::Result<()> { | ||||||
|  |     HttpServer::new(|| App::new().service(createuser).service(check_password)) | ||||||
|  |         .bind(("127.0.0.1", 8080))? | ||||||
|  |         .run() | ||||||
|  |         .await | ||||||
|  | 
 | ||||||
|  |     /*match backend::add_user(
 | ||||||
|  |         String::from("vanten-s"), | ||||||
|  |         String::from("vanten-s@vanten-s.com"), | ||||||
|  |         String::from("XFAZ7EgjM1CKLK66QDGr0FmceqWHgtvsiMs7GRIi"), | ||||||
|  |         &mut connection, | ||||||
|  |     ) { | ||||||
|  |         UserCreationStatus::UserExists => println!("User already exists"), | ||||||
|  |         _ => println!("Got another error"), | ||||||
|  |     }*/ | ||||||
|  | 
 | ||||||
|  |     /*match backend::check_password(18, String::from("12345"), &mut connection) {
 | ||||||
|  |         Ok(v) => match v { | ||||||
|  |             true => println!("CORRECT LES FUCKING GO"), | ||||||
|  |             false => println!("NO ITS FALSEEE") | ||||||
|  |         }, | ||||||
|  |         Err(_) => println!("Error :(") | ||||||
|  |     }*/ | ||||||
|  | } | ||||||
		Loading…
	
		Reference in a new issue