Files
advotrackerd/src/main.rs
2020-06-12 22:28:16 +02:00

214 lines
5.9 KiB
Rust

/* advotracker infrastructure.
*
* Copyright 2020 Ralf Zerres <ralf.zerres@networkx.de>
* SPDX-License-Identifier: (0BSD or MIT)
*/
// Rust nightly supports procedural macros!
//#![feature(proc_macro)]
//#![deny(rust_2018_idioms)]
/// advotrackerd: the main binary
//use log::info; //substituted with tracing
use dotenv::dotenv;
use locales::t;
use serde::Deserialize;
//use std::{env, process};
use std::env;
//use tracing::{debug, info, instrument, span, Level};
//use tracing::{debug, info, instrument, Level, Metadata, span::{Id, Attributes, Record}, trace};
//use tracing::{debug, instrument, span::*, trace, Level};
use tracing::{debug, trace, Level};
//use diesel::prelude::*;
//use diesel::sqlite::SqliteConnection;
mod parse_args;
#[derive(Deserialize, Debug)]
struct Environment {
test_lang: String,
}
/*
pub struct AdvoTrackerSubscriber;
impl tracing::Subscriber for AdvoTrackerSubscriber{
fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) }
fn record(&self, _: &Id, _: &Record) {}
fn event(&self, _: &tracing::Event) {}
fn record_follows_from(&self, _: &Id, _: &Id) {}
fn enabled(&self, _: &Metadata) -> bool { false }
fn enter(&self, _: &Id) {}
fn exit(&self, _: &Id) {}
}
impl AdvoTrackerSubscriber {
fn new() -> Self { AdvoTrackerSubscriber }
}
#[instrument]
fn run_subcommand(matches: &ArgMatches) -> Result<(), String> {
//info!("inside run_subcommand");
match matches.subcommand() {
("config", Some(cmd)) => run_config(cmd),
("test", Some(cmd)) => run_test(cmd),
_ => Ok(()),
}
}
fn run_config(matches: &ArgMatches) -> Result<(), String> {
//info!("inside run_config");
let _input = matches.value_of("FILE").unwrap();
Ok(())
}
fn run_test(matches: &ArgMatches) -> Result<(), String> {
//info!("inside run_test");
if let Some(matches) = matches.subcommand_matches("test") {
if matches.is_present("debug") {
println!("test: Printing debug info...");
} else {
println!("test: Printing normally...");
}
}
Ok(())
}
*/
/// `advotrackerd` is the daemon that keeps track of legal mandate data
/// that are backed up in a database.
fn main() -> Result<(), Box<dyn std::error::Error>> {
use parse_args::parse_args;
use tracing_subscriber::fmt;
use viperus::Viperus;
//let advotracker_subscriber = AdvoTrackerSubscriber::new();
//tracing::subscriber::set_global_default(advotracker_subscriber)
// .expect("setting tracing default failed");
// initialize the tracing subsystem
// a drop in replacement for classical logging
// reference: https://tokio.rs/blog/2019-08-tracing/
let span = tracing::span!(Level::TRACE, "advotracker_main");
let _enter = span.enter();
let subscriber = fmt::Subscriber::builder()
.with_env_filter("advotracker=trace")
.finish();
// initialize logger
// TODO: exchange with tracing!
//env_logger::init();
//info!("Commencing the proxy!");
tracing::subscriber::with_default(subscriber, || {
// get system environment
let mut lang = env::var("LANG").unwrap_or("en".to_string());
let mut res = t!("parse.environment", lang);
let mut state = t!("state.started", lang);
trace!(target: "advotracker", message = ?res, state = ?state);
//debug!(message = ?res, state = ?state);
trace!(target: "advotracker", environment = "system", lang = ?lang);
// get testing environment (.env)
dotenv().ok();
match envy::from_env::<Environment>() {
Ok(environment) => {
if environment.test_lang != lang { lang = environment.test_lang; }
},
Err(e) => { debug!(target: "advotracker", "{}", e); }
}
res = t!("parse.environment", lang);
trace!(target: "advotracker", environment = "envy", lang = ?lang);
state = t!("state.finished", lang);
trace!(target: "advotracker", message = ?res, state = ?state);
//debug!(message = ?res, state = ?state);
// initialize viperus structure
let mut v = Viperus::new();
// parse commandline arguments
res = t!("parse.arguments", lang);
state = t!("state.started", lang);
trace!(target: "advotracker", process = ?res, state = ?state);
//info!(target: "advotracker", "{}", res);
//debug!(message = ?res, state = ?state);
//info!(target: "advotracker", "parsing commandline args");
let _ = parse_args(&mut v);
state = t!("state.finished", lang);
trace!(target: "advotracker", process = ?res, state = ?state);
//trace!(target: "Viperus", "Config results: {:?}", v);
/*
if verbose > 1 {
for (key, value) in env::vars() {
println!("{}={}", key, value);
}
}
*/
//state = t!("state.finished", lang);
//trace!(target: "advotracker", process = ?res, state = ?state);
//debug!(message = ?res, state = ?state);
/*
// handle subcommands
if let Err(e) = run_subcommand(&matches) {
println!("Subcommand error: {}", e);
process::exit(1);
}
*/
// Starting the program logic
res = t!("main.started", lang);
state = t!("state.started", lang);
//info!(target: "advotracker", "{}", res);
trace!(target: "advotracker", process = ?res, state = ?state);
//use advotracker_backend::*;
//use advotracker_backend::schema::users::dsl::*;
/*
let connection = establish_connection();
trace!(
target: "advotracker",
process = "Sqlite3",
status = "connected",
);
*/
/*
//User::table.load(&connection);
//user::belonging_to(users).load(&connection);
use advotracker_backend::models::*;
let results = users
//.filter(published.eq(true))
.limit(5)
.load::<User>(&connection)
.expect("Error loading users");
println!("Displaying {} users", results.len());
for user in results {
println!("{}", user.user_id);
println!("----------\n");
println!("{}", user.first_name);
println!("{}", user.last_name);
println!("{}", user.alias);
println!("{}", user.email);
}
*/
state = t!("state.finished", lang);
res = t!("main.finished", lang);
//info!(target: "advotracker", "{}", res);
trace!(target: "advotracker", process = ?res, state = ?state);
});
Ok(())
}