PaperTrader/src/libtrader/client/initializer.rs

175 lines
5.4 KiB
Rust
Raw Normal View History

2021-05-26 06:30:52 +00:00
use std::io;
use std::net::ToSocketAddrs;
use tokio::net::TcpStream;
use tokio_rustls::webpki::DNSNameRef;
2021-05-30 17:08:02 +00:00
use tokio_rustls::TlsConnector;
2020-08-11 11:49:29 +00:00
use crate::client::network::gen_tls_client_config::gen_tls_client_config;
2020-07-23 15:04:14 +00:00
2021-05-30 17:08:02 +00:00
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
2021-05-30 17:08:02 +00:00
2021-07-30 04:04:54 +00:00
use argh::FromArgs;
/// Client Options
#[derive(FromArgs)]
struct Options {
/// bind addr
#[argh(positional)]
addr: String,
}
/// Initializes global logger.
///
/// Private function used by libtrader_init() to initialize the logger. Log destinations are
/// platfrom dependent.
/// On unix systems: /var/log/papertrader/
/// On windows/unkown systems: $(pwd)/log/
2021-07-14 20:12:14 +00:00
/// Should be used contexts that return ```io::Result```.
///
2021-07-14 20:12:14 +00:00
/// Returns: ```io::Result```
///
/// Example:
/// ```rust
2021-07-14 20:12:14 +00:00
/// libtrader_init_client().expect("failed running client");
/// ```
///
2021-05-26 06:30:52 +00:00
fn libtrader_init_log() -> io::Result<()> {
use fern::colors::{Color, ColoredLevelConfig};
2020-07-31 08:16:18 +00:00
let mut dispatch = fern::Dispatch::new().format(|out, message, record| {
// configure colors for the whole line
let colors_line = ColoredLevelConfig::new()
.error(Color::Red)
.warn(Color::White)
// we actually don't need to specify the color for debug and info, they are white by default
.info(Color::Green)
.debug(Color::Yellow)
// depending on the terminals color scheme, this is the same as the background color
.trace(Color::BrightBlack);
2020-07-31 08:16:18 +00:00
// configure colors for the name of the level.
// since almost all of them are the same as the color for the whole line, we
// just clone `colors_line` and overwrite our changes
let colors_level = colors_line.clone().info(Color::Green);
2020-07-31 08:16:18 +00:00
out.finish(format_args!(
"{color_line}{date}[{target}][{level}{color_line}] {message}\x1B[0m",
color_line = format_args!(
"\x1B[{}m",
colors_level.get_color(&record.level()).to_fg_str()
),
date = chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
target = record.target(),
level = record.level(),
message = message
))
});
#[cfg(debug_assertions)]
{
dispatch = dispatch
.level(log::LevelFilter::Debug)
.chain(std::io::stdout());
}
#[cfg(not(debug_assertions))]
{
dispatch = dispatch
.level(log::LevelFilter::Warn)
.chain(std::io::stdout())
.chain(fern::log_file(format!(
"log/log-{}.log",
chrono::Utc::now().to_rfc2822()
))?);
}
dispatch.apply().map_err(|err| {
io::Error::new(
io::ErrorKind::Other,
format!("LIBTRADER_INIT_SERVER_LOG_FAILED: {}", err),
)
})
2020-07-28 19:43:00 +00:00
}
2020-08-11 11:49:29 +00:00
/// Client Initialization of the library.
///
2020-08-11 11:49:29 +00:00
/// Public function that initializes the library, and connects to a libtrader server
/// This funciton should not return.
///
/// Example:
/// ```rust
2020-08-11 11:49:29 +00:00
/// libtrader_init_client()?;
/// ```
2021-05-26 06:30:52 +00:00
#[tokio::main]
pub async fn libtrader_init_client() -> std::io::Result<()> {
// Initialize log.
libtrader_init_log()?;
2020-07-28 19:43:00 +00:00
2021-07-30 04:04:54 +00:00
// Initialize arguments
let options: Options = argh::from_env();
let addr = options
.addr
2021-05-30 17:08:02 +00:00
.to_socket_addrs()?
.next()
2021-07-30 04:04:54 +00:00
.ok_or_else(|| std::io::Error::from(std::io::ErrorKind::AddrNotAvailable))?;
2021-05-26 06:30:52 +00:00
let domain = "localhost";
let config = gen_tls_client_config()?;
2021-05-19 14:51:36 +00:00
2021-05-26 06:30:52 +00:00
let connector = TlsConnector::from(config);
let stream = TcpStream::connect(&addr).await?;
let domain = DNSNameRef::try_from_ascii_str(&domain)
.map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "invalid dnsname"))?;
let mut socket = connector.connect(domain, stream).await?;
2021-05-30 17:08:02 +00:00
let username: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(30)
.map(char::from)
.collect();
let email: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(30)
.map(char::from)
.collect();
let password: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(30)
.map(char::from)
.collect();
2021-05-26 06:30:52 +00:00
use crate::client::account::creation::acc_create;
match acc_create(&mut socket, &username, &email, &password).await {
2021-05-26 06:30:52 +00:00
Ok(_) => println!("we created it"),
Err(err) => panic!("panik! {}", err),
}
use crate::client::account::authorization::acc_auth;
let mut jwt: String = String::new();
2021-05-31 14:41:21 +00:00
println!("{}", jwt); // this is for removing pisky warnings,
2021-05-31 14:39:01 +00:00
// this is fine as long as this code is sandbox
match acc_auth(&mut socket, &username, &email, &password).await {
2021-05-26 06:30:52 +00:00
Ok(auth) => {
jwt = auth;
println!("we accessed it, the token: {}", jwt);
2021-05-30 17:08:02 +00:00
}
2021-05-26 06:30:52 +00:00
Err(err) => panic!("panik! {}", err),
2020-08-10 14:21:49 +00:00
}
2021-05-26 06:30:52 +00:00
use crate::client::account::retrieval_portfolio::acc_retrieve_portfolio;
match acc_retrieve_portfolio(&mut socket, String::from(jwt.as_str())).await {
Ok(portfolio) => println!("we got portfolio {:#?}", portfolio),
Err(err) => panic!("panik! {}", err),
}
2020-07-31 11:41:15 +00:00
2021-05-26 06:30:52 +00:00
use crate::client::account::retrieval_transaction::acc_retrieve_transaction;
2021-05-31 14:39:01 +00:00
match acc_retrieve_transaction(&mut socket, jwt).await {
2021-05-26 06:30:52 +00:00
Ok(transaction) => println!("we got the transactions {:#?}", transaction),
Err(err) => panic!("panik! {}", err),
2020-07-31 11:41:15 +00:00
}
2021-05-26 06:30:52 +00:00
Ok(())
2020-07-31 11:27:27 +00:00
}