Make SQL request, build buildscript

This commit is contained in:
famfo 2022-05-01 18:54:02 +02:00
parent a4f6bf8895
commit c85ccd18b6
6 changed files with 44 additions and 13 deletions

3
.gitignore vendored
View file

@ -1,4 +1,5 @@
/target
tmtd.toml
.idea
.vs
.vs
.env

1
Cargo.lock generated
View file

@ -1715,6 +1715,7 @@ dependencies = [
"anyhow",
"async-sqlx-session",
"chrono",
"dotenv",
"serde",
"sqlx",
"tera",

View file

@ -11,7 +11,7 @@ lto = true
tokio = { version = "1", features = ["rt", "sync", "signal", "macros"] }
toml = "0.5"
serde = { version = "1.0", features = ["derive"] }
sqlx = { version = "0.5", default-features = false, features = ["runtime-tokio-rustls", "postgres"] }
sqlx = { version = "0.5", default-features = false, features = ["runtime-tokio-rustls", "postgres", "macros"] }
tracing = "0.1"
tracing-subscriber = "0.3"
warp = { version = "0.3", default-features = false }
@ -20,3 +20,9 @@ tera = "1.15"
async-sqlx-session = { version = "0.4", default-features = false, features = ["pg"] }
anyhow = "1.0"
chrono = "0.4"
dotenv = "0.15"
[build-dependencies]
toml = "0.5"
serde = { version = "1.0", features = ["derive"] }

20
build.rs Normal file
View file

@ -0,0 +1,20 @@
use std::env;
use std::fs;
use serde::Deserialize;
#[allow(dead_code)]
#[derive(Deserialize)]
struct Config {
listen_addr: String,
admin_pass: String,
connection_string: String,
log_level: Option<String>,
}
fn main() {
let config_var = env::var("TMTD_CONFIG");
let config_path = config_var.as_deref().unwrap_or("tmtd.toml");
let config = fs::read_to_string(config_path).unwrap();
let cfg: Config = toml::from_str(&config).unwrap();
fs::write(".env", format!("DATABASE_URL={}", cfg.connection_string)).unwrap();
}

View file

@ -53,19 +53,22 @@ impl Database {
self.0.close().await;
}
pub async fn create_user(&self, username: &str, passwod: &str) -> anyhow::Result<()> {
pub async fn create_user(&self, username: &str, passwod: &str) -> anyhow::Result<i32> {
if username.len() < 17 {
let mut conn = self.0.acquire().await?;
let mut hasher = DefaultHasher::new();
passwod.hash(&mut hasher);
let hash = hasher.finish();
tracing::debug!("Password hash for user {}: {:x}", username, hash);
conn.execute(format!("
INSERT INTO users(username, hash)
VALUES ({}, {:x})
RETURNING id;
", username, hash).as_str()).await?;
return Ok(())
let hash = format!("{:x}", hasher.finish());
tracing::debug!("Password hash for user {}: {}", username, hash);
let user = sqlx::query!(
"INSERT INTO users (username, hash)
VALUES ($1::Varchar, $2::Varchar)
RETURNING id",
username,
hash
)
.fetch_one(&self.0)
.await?;
return Ok(user.id)
}
Err(anyhow::anyhow!("Username is longer then 16 characters"))
}

View file

@ -54,6 +54,7 @@ async fn terminate_signal() {
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
let config_var = env::var("TMTD_CONFIG");
let config_path = config_var.as_deref().unwrap_or("tmtd.toml");
println!("Loading config from '{}'...", config_path);
@ -75,7 +76,6 @@ async fn main() -> anyhow::Result<()> {
let session_store =
PostgresSessionStore::from_client(database.pool()).with_table_name("sessions");
session_store.migrate().await?;
database.create_user("famfo", "1234").await?;
let cleanup_task =
spawn_session_cleanup_task(&session_store, Duration::from_secs(600), ctx.subscribe());