feat: basic server

This commit is contained in:
Xory 2024-06-23 12:17:37 +03:00
parent 13f9cd71c7
commit 9a0a025550
8 changed files with 5521 additions and 0 deletions

3
.gitignore vendored
View file

@ -1,2 +1,5 @@
/target
/testfiles
/server/target
/server/src/prisma.rs
/server/prisma/migrations/

View file

@ -0,0 +1,2 @@
[alias]
prisma = "run --bin prisma --"

3
server/.env Normal file
View file

@ -0,0 +1,3 @@
DATABASE_URL=postgres://postgres:KILLSECUBERALLEN@192.168.1.2:1488/postgre
PASSWORD=C2N1gh7W47ch-S0L####

5347
server/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

12
server/Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "server"
version = "0.1.0"
edition = "2021"
[dependencies]
actix-web = "4.8.0"
prisma-client-rust = { git = "https://github.com/Brendonovich/prisma-client-rust", tag = "0.6.11" }
prisma-client-rust-cli = { git = "https://github.com/Brendonovich/prisma-client-rust", tag = "0.6.11" }
serde = "1.0.203"
tokio = "1.38.0"

View file

@ -0,0 +1,17 @@
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "cargo prisma"
output = "../src/prisma.rs"
}
model Client {
id String @id @unique
ip String
hostname String
paid Boolean
}

3
server/src/bin/prisma.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
prisma_client_rust_cli::run();
}

134
server/src/main.rs Normal file
View file

@ -0,0 +1,134 @@
use actix_web::{dev::ServiceRequest, get, http::header, post, web, App, HttpResponse, HttpServer, Responder};
use prisma_client_rust::PrismaClient;
use serde::Deserialize;
use tokio::fs::File;
use tokio::io::AsyncReadExt;
#[allow(unused, dead_code)]
mod prisma;
#[get("/")]
async fn index() -> impl Responder {
"HAAAAIIII :33"
}
#[derive(Deserialize)]
struct ClientRegisterRequest {
ip: String,
hostname: String
}
#[derive(Deserialize)]
struct SetClientPaidRequest { password: String }
#[post("/client/register/{id}")]
async fn register_client(path: web::Path<u32>, info: web::Json<ClientRegisterRequest>) -> impl Responder {
let db_client = match prisma::PrismaClient::_builder().build().await {
Ok(client) => client,
Err(err) => {
eprintln!("Error building Prisma Client: {:?}", err);
return HttpResponse::InternalServerError().body("Internal Server Error");
}
};
let client = match db_client.client().create(
format!("client_{}", path),
info.ip.to_string(),
info.hostname.to_string(),
false,
vec![]
).exec().await {
Ok(client) => client,
Err(err) => {
eprintln!("Error building Prisma Client: {:?}", err);
return HttpResponse::InternalServerError().body("Internal Server Error");
}
};
HttpResponse::Ok().json(client)
}
#[get("/client/{id}/ispaid")]
async fn get_client_payment(path: web::Path<u32>) -> impl Responder {
let db_client = match prisma::PrismaClient::_builder().build().await {
Ok(client) => client,
Err(err) => {
eprintln!("Error building Prisma Client: {:?}", err);
return HttpResponse::InternalServerError().body("Internal Server Error");
}
};
let client = match db_client.client()
.find_unique(prisma::client::id::equals(path.to_string()))
.exec()
.await
.unwrap() {
Some(client) => client,
None => {
eprintln!("Couldn't find client");
return HttpResponse::NotFound().body("Not Found");
}
};
HttpResponse::Ok().body(client.paid.to_string())
}
#[post("/client/{id}/setpaid")]
pub async fn set_client_paid(path: web::Path<String>, info: web::Json<SetClientPaidRequest>) -> impl Responder {
let mut dotenv = File::open(".env").await.unwrap();
let mut dotenv_contents = String::new();
match dotenv.read_to_string(&mut dotenv_contents).await {
Ok(result) => result,
Err(err) => {
eprintln!("Encountered error: {:?}", err);
return HttpResponse::InternalServerError().body("Internal Server Error");
},
};
let password = dotenv_contents
.lines()
.filter(|line| line.starts_with("PASSWORD="))
.next()
.unwrap()
.split("=")
.last()
.unwrap(); // don't shout at me for the unwraps, your goddamn fault if you fucked the .env
// file
if path.to_string() != password { return HttpResponse::Forbidden().body("Forbidden") }
let db_client = match prisma::PrismaClient::_builder().build().await {
Ok(client) => client,
Err(err) => {
eprintln!("Error creating Prisma Client: {:?}", err);
return HttpResponse::InternalServerError().body("Internal Server Error");
},
};
let mut client = match db_client
.client()
.find_first(vec![prisma::client::id::equals(path.to_string())])
.exec()
.await
.unwrap() {
Some(client) => client,
None => {
eprintln!("Couldn't find client!");
return HttpResponse::NotFound().body("Not Found");
},
};
client.paid = true;
HttpResponse::Ok().body("Ok")
}
#[actix_web::main]
pub async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
})
.bind("127.0.0.1:8080")?
.run()
.await
}