feat: add proper env vars for home and the csv

This commit is contained in:
silver 2023-07-17 01:24:52 +01:00
parent 480860ca26
commit f73a7dfa29
4 changed files with 19 additions and 13 deletions

5
.gitignore vendored
View file

@ -1,14 +1,11 @@
/target
/.idea
.env
*.db
# flakes
result
/result
tmp.*
*.csv

View file

@ -51,7 +51,9 @@
LDAP_ADMIN = cfg.ldap.admin;
# basic dserver stuff
DATABASE = "${cfg.home}/database.db";
HOME = cfg.home;
DATABASE = "database.db";
CSV = "wolves.csv";
HOST_PORT = cfg.host_port;
# special categories of users

View file

@ -35,7 +35,6 @@ async fn update_users(config: &Config) -> tide::Result<()> {
for every valid user in wolves match to ldap
add to users
*/
println!("{:#?}", users_tmp);
// pull from wolves csv
for user in from_csv(config).await.unwrap_or_default() {
users_tmp.insert(user);
@ -48,8 +47,6 @@ async fn update_users(config: &Config) -> tide::Result<()> {
}
}
println!("{:#?}", users_tmp);
// easier to work with Strings above but easier to work with &str below
let users: Vec<&str> = users_tmp.iter().map(|s| &**s).collect();
@ -184,7 +181,7 @@ async fn from_csv(config: &Config) -> Result<HashSet<String>, Box<dyn Error>> {
let mut uids = HashSet::new();
let (uid_idstudent, uid_email) = ldap_get_accounts(config).await?;
let records = read_csv()?;
let records = read_csv(config)?;
let now = Utc::now();
let today = format!("{}-{:02}-{:02}", now.year(), now.month(), now.day());
@ -218,10 +215,10 @@ struct Record {
expiry: String,
}
fn read_csv() -> Result<Vec<Record>, Box<dyn Error>> {
fn read_csv(config: &Config) -> Result<Vec<Record>, Box<dyn Error>> {
let mut records: Vec<Record> = vec![];
if let Ok(mut rdr) = csv::Reader::from_path("tmp.csv") {
if let Ok(mut rdr) = csv::Reader::from_path(format!("{}/{}", &config.home, &config.csv)) {
for result in rdr.deserialize() {
// Notice that we need to provide a type hint for automatic
// deserialization.

View file

@ -31,7 +31,7 @@ pub struct Accounts {
}
pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
let database = &config.database;
let database = format!("{}/{}", &config.home, &config.database);
let pool = SqlitePoolOptions::new()
.max_connections(5)
.connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true))
@ -91,7 +91,9 @@ pub struct Config {
pub ldap_host: String,
pub ldap_admin: String,
pub ldap_admin_pw: String,
pub home: String,
pub database: String,
pub csv: String,
pub host_port: String,
}
@ -103,7 +105,9 @@ pub fn get_config() -> Config {
ldap_host: "".to_string(),
ldap_admin: "".to_string(),
ldap_admin_pw: "".to_string(),
home: ".".to_string(),
database: "database.db".to_string(),
csv: "wolves.csv".to_string(),
host_port: "127.0.0.1:8087".to_string(),
};
@ -116,9 +120,15 @@ pub fn get_config() -> Config {
if let Ok(x) = env::var("LDAP_ADMIN_PW") {
config.ldap_admin_pw = x.trim().to_string();
}
if let Ok(x) = env::var("HOME") {
config.home = x.trim().to_string();
}
if let Ok(x) = env::var("DATABASE") {
config.database = x.trim().to_string();
}
if let Ok(x) = env::var("CSV") {
config.csv = x.trim().to_string();
}
if let Ok(x) = env::var("HOST_PORT") {
config.host_port = x.trim().to_string();
}