Initial Commit

This commit is contained in:
vanten-s 2023-08-24 14:53:36 +02:00
commit a526b83697
Signed by: vanten-s
GPG key ID: DE3060396884D3F2
4 changed files with 1561 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
.env

1445
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

11
Cargo.toml Normal file
View 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
View 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 :(")
}*/
}