Compare commits
4 Commits
c6fbebf3ff
...
fcde51ab3c
| Author | SHA1 | Date | |
|---|---|---|---|
| fcde51ab3c | |||
| 98a75479f8 | |||
| f2f68aecd8 | |||
| fb31f9102d |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
||||
/target
|
||||
.env
|
||||
.cargo
|
||||
94
Cargo.lock
generated
94
Cargo.lock
generated
@@ -125,6 +125,12 @@ dependencies = [
|
||||
"hybrid-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
@@ -712,6 +718,16 @@ version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.186"
|
||||
@@ -909,6 +925,12 @@ dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.23"
|
||||
@@ -1109,6 +1131,7 @@ dependencies = [
|
||||
"tokio-stream",
|
||||
"tracing",
|
||||
"url",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1174,6 +1197,7 @@ dependencies = [
|
||||
"sqlx-core",
|
||||
"thiserror",
|
||||
"tracing",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1208,6 +1232,7 @@ dependencies = [
|
||||
"stringprep",
|
||||
"thiserror",
|
||||
"tracing",
|
||||
"uuid",
|
||||
"whoami",
|
||||
]
|
||||
|
||||
@@ -1233,6 +1258,7 @@ dependencies = [
|
||||
"thiserror",
|
||||
"tracing",
|
||||
"url",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1475,6 +1501,16 @@ version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3cf4199d1e5d15ddd86a694e4d0dffa9c323ce759fea589f00fef9d81cc1931d"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
@@ -1493,6 +1529,64 @@ version = "0.11.1+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
"rustversion",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "whoami"
|
||||
version = "2.1.2"
|
||||
|
||||
@@ -4,7 +4,7 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
sqlx = {version = "0.9.0", features = ["runtime-tokio", "postgres", "macros"]}
|
||||
sqlx = {version = "0.9.0", features = ["runtime-tokio", "postgres", "macros", "uuid"]}
|
||||
axum = {version = "0.8.9", feature = ["full"]}
|
||||
tokio = {version = "1.52.3", features = ["full"]}
|
||||
tower = "0.5.3"
|
||||
|
||||
6
migrations/20260714035817_create_table_users.sql
Normal file
6
migrations/20260714035817_create_table_users.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
steamid char(64) NOT NULL,
|
||||
name varchar(5) NOT NULL,
|
||||
avatar varchar(20) NOT NULL
|
||||
);
|
||||
11
src/main.rs
11
src/main.rs
@@ -1,13 +1,22 @@
|
||||
use std::error::Error;
|
||||
use std::env::var;
|
||||
|
||||
use axum::routing::get;
|
||||
use axum::{serve, Router};
|
||||
use tokio::net::TcpListener;
|
||||
use sqlx::postgres::PgPool;
|
||||
use dotenvy::dotenv;
|
||||
|
||||
mod users;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
dotenv()?;
|
||||
let db_url = var("DATABASE_URL")?;
|
||||
let pool = PgPool::connect(&db_url).await?;
|
||||
let app = Router::new()
|
||||
.route("/", get(hello));
|
||||
.route("/", get(hello))
|
||||
.with_state(pool);
|
||||
let listener = TcpListener::bind("0.0.0.0:80").await?;
|
||||
Ok(serve(listener, app).await?)
|
||||
}
|
||||
|
||||
37
src/users.rs
Normal file
37
src/users.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use std::error::Error;
|
||||
|
||||
use axum::routing::{post, get};
|
||||
use sqlx::{PgPool, query, query_as, types::Uuid};
|
||||
|
||||
struct User {
|
||||
id: Uuid,
|
||||
steamid: String,
|
||||
name: String,
|
||||
avatar: String
|
||||
}
|
||||
|
||||
impl User {
|
||||
fn new(steamid: String, name: String, avatar: String) -> User {
|
||||
User {
|
||||
id: Uuid::nil(),
|
||||
steamid,
|
||||
name,
|
||||
avatar
|
||||
}
|
||||
}
|
||||
|
||||
async fn create(self, pool: PgPool) -> Result<(), Box<dyn Error>>{
|
||||
query!("INSERT INTO users (steamid, name, avatar) VALUES ($1, $2, $3)", self.steamid, self.name, self.avatar).execute(&pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete(self, pool: PgPool) -> Result<(), Box<dyn Error>>{
|
||||
query!("DELETE FROM users WHERE id=$1", self.id).execute(&pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update(self, pool: PgPool) -> Result<(), Box<dyn Error>>{
|
||||
query!("UPDATE users SET steamid=$1, name=$2, avatar=$3 WHERE id=$4", self.steamid, self.name, self.avatar, self.id).execute(&pool).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user