update user.rs file and integration in main file

This commit is contained in:
2026-03-31 16:21:46 +02:00
parent 3a6bd21b59
commit 6a5774667e
5 changed files with 79 additions and 10 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
Test
Test
backend/user.db

57
backend/Cargo.lock generated
View File

@@ -90,6 +90,7 @@ dependencies = [
"axum",
"axum-server",
"serde",
"sqlite",
"tokio",
"tower-http",
"tracing-subscriber",
@@ -107,6 +108,16 @@ version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33"
[[package]]
name = "cc"
version = "1.2.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1e928d4b69e3077709075a938a05ffbedfa53a84c8f766efbf8220bb1ff60e1"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
@@ -135,6 +146,12 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
[[package]]
name = "fnv"
version = "1.0.7"
@@ -450,6 +467,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pkg-config"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]]
name = "proc-macro2"
version = "1.0.106"
@@ -563,6 +586,12 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "signal-hook-registry"
version = "1.4.8"
@@ -595,6 +624,34 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "sqlite"
version = "0.37.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f66e9c01a11936154f3910dbba732c01f8b591543bc4d6672bddee79fd9c4783"
dependencies = [
"sqlite3-sys",
]
[[package]]
name = "sqlite3-src"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5b6d3c860886b0a33e69e421796a5f4a27f23597a182c2450f6d7ace5103120"
dependencies = [
"cc",
"pkg-config",
]
[[package]]
name = "sqlite3-sys"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7781d97adc13a1d5081127a9ee29afad8427f3757bd984daf814d8265267039"
dependencies = [
"sqlite3-src",
]
[[package]]
name = "syn"
version = "2.0.117"

View File

@@ -10,3 +10,4 @@ serde = "1.0.228"
tokio = { version = "1.50.0", features = ["full"] }
tracing-subscriber = "0.3.23"
tower-http = { version = "0.6", features = ["fs"] }
sqlite = "*"

View File

@@ -4,9 +4,12 @@ use std::path::Path;
use tokio::fs::File;
use tokio::io::{self, AsyncReadExt};
use tower_http::services::ServeDir;
mod user;
#[tokio::main]
async fn main() {
let db = user::DataBase::open(String::from("user"));
db.create_table("users", "name TEXT, level TEXT, password TEXT");
let app = Router::new()
.route("/", get(handler))
.route("/game", get(game))
@@ -34,6 +37,20 @@ async fn game() -> Html<String> {
Html(html_content)
}
async fn register() -> Html<String> {
let html_content = read_html_from_file("../web/templates/view/register.html")
.await
.unwrap_or_else(|_| "<h1>Error loading HTML file</h1>".to_string());
Html(html_content)
}
async fn login() -> Html<String> {
let html_content = read_html_from_file("../web/templates/view/login.html")
.await
.unwrap_or_else(|_| "<h1>Error loading HTML file</h1>".to_string());
Html(html_content)
}
async fn read_html_from_file<P: AsRef<Path>>(path: P) -> io::Result<String> {
let mut file = File::open(path).await?;
let mut contents = String::new();

View File

@@ -1,24 +1,17 @@
use sqlite::Connection;
// use sodiumoxide::crypto::secretbox;
pub struct User {
name: String,
level: u32,
// password: [u8],
}
impl User {
pub fn open(name: String, level: u32, password: [u8]) -> User {
User {name, level, password}
pub fn open(name: String, level: u32) -> User {
User {name, level}
}
pub fn get_name(&self) -> String{return self.name.clone()}
pub fn get_level(&self) -> u32{return self.level}
// pub fn get_password(&self) -> [u8]{return self.password.clone()}
// pub fn check_password(&self,nonce:secretbox::Nonce,key:secretbox::Key){
// assert!("" == secretbox::open(self.get_password(), &nonce, &key).unwrap()[..]);
// }
}