nightly(example): basic database structure and showcase

This commit is contained in:
arielherself 2024-03-29 09:45:07 +08:00
commit 163c7ffc05
No known key found for this signature in database
5 changed files with 2577 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

2519
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

15
Cargo.toml Normal file
View File

@ -0,0 +1,15 @@
[package]
name = "oauth"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tide = "0.16.0"
async-std = { version = "1.8.0", features = ["attributes"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.37.0", features = ["full"] }
tokio-postgres = "0.7.10"
futures = "0.3.30"

13
src/db.rs Normal file
View File

@ -0,0 +1,13 @@
use tokio_postgres::{ NoTls, Error, tls::NoTlsStream };
pub struct Database {
client: tokio_postgres::Client,
connection: tokio_postgres::Connection<tokio_postgres::Socket, NoTlsStream>,
}
impl Database {
pub async fn new(host: &str, port: u16, user: &str, password: &str, dbname: &str) -> Result<Self, Error> {
let (client, connection) = tokio_postgres::connect(format!("host={} port={} user={} password={} dbname={}", host, port, user, password, dbname).as_str(), NoTls).await?;
Ok(Self { client, connection })
}
}

29
src/main.rs Normal file
View File

@ -0,0 +1,29 @@
use async_std::io::prelude::SeekExt;
use tide::Request;
use tide::prelude::*;
use futures::executor::block_on;
mod db;
#[derive(Serialize, Deserialize)]
struct Test {
x: i32,
y: i32
}
impl Test {
fn new(x: i32, y: i32) -> Self {
Self { x, y }
}
}
#[tokio::main]
async fn main() {
let a = Test::new(2, 3);
let b = block_on(db::Database::new("localhost", 5432, "postgres", "configjson", "myoauth")).unwrap();
// let s = serde_json::to_string(&a);
// json!(a);
println!("{}", json!(a));
}