ipblc/old/db.rs

48 lines
1.2 KiB
Rust

use crate::config::*;
use crate::ip::*;
use futures::executor::block_on;
use rbatis;
use rbatis::crud::{Skip, CRUD};
use rbatis::rbatis::Rbatis;
use rbatis::DateTimeUtc;
use std::process::exit;
const DBTYPE: &'static str = "postgres";
pub async fn init(rb: &Rbatis, cfg: &Config) {
let connection: String = format!(
"{t}://{username}:{password}@{hostname}:{port}/{name}",
t = DBTYPE,
username = cfg.dbusername,
password = cfg.dbpassword,
hostname = cfg.dbhostname,
port = cfg.dbport,
name = cfg.dbname,
)
.to_string();
let link = rb.link(connection.as_str()).await;
match link {
Ok(_) => (),
Err(err) => {
println!("Please check connection parameters: {err}");
exit(1);
}
}
}
pub fn push_ip(ip: String, src: &String, rb: &Rbatis) {
let i = IP {
id: Some(0),
ip: Some(ip),
rdns: None,
src: Some(src.to_string()),
created: Some(DateTimeUtc::now()),
updated: Some(DateTimeUtc::now()),
};
match block_on(rb.save(&i, &[Skip::Column("id")])) {
Ok(r) => println!("{r:?}"),
Err(err) => println!("{error}", error = err.to_string()),
};
}