Added trivial web ui.

This commit is contained in:
didier amyot committed 2023-01-30 07:52:38 -05:00
1 parent f99513a8c8
commit 894f969d0f
11 files changed
+1382 -19

No files matched your search

+80 -3
View File
@@ -1,7 +1,7 @@
use std::path::PathBuf;
use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
use clap::parser::ValuesRef;
use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
use log::{debug, error, info};
use rusqlite::Connection;
use serde::Serialize;
@@ -10,14 +10,16 @@ use simple_logger::SimpleLogger;
use db::securities::list_securities;
use crate::db::users::{create_user, list_users};
use crate::event_processor::{process_event_files};
use crate::event_processor::process_event_files;
use crate::migration::apply_migrations;
use crate::webui::web_ui_main;
mod db;
mod event_processor;
mod migration;
mod services;
mod util;
mod webui;
#[derive(Debug, Serialize)]
struct Output<T> {
@@ -42,6 +44,8 @@ trait CommandLineUserLogic {
fn list_securities(&mut self, conn: &Connection);
fn process_events(&mut self, conn: &mut Connection, files: ValuesRef<PathBuf>);
fn run_webui(&mut self, conn: &mut Connection, port: u16, enable_template_autoreload: bool);
}
fn verbosity_arg() -> Arg {
@@ -130,12 +134,36 @@ fn ls_securities_command_line_parser() -> Command {
.about("List all securities.");
}
fn run_web_ui_command_line_parser() -> Command {
return Command::new("run")
.arg(Arg::new("port")
.long("port")
.value_parser(clap::value_parser!(u16).range(3000..))
.action(ArgAction::Set)
.default_value("3200"))
.arg(Arg::new("enable-template-autoreload")
.long("enable-template-autoreload")
.required(false)
.num_args(0)
.help("Reload underlying template (for development)")
)
.about("Run web user interface.");
}
fn securities_command_line_parser() -> Command {
return Command::new("securities")
.about("Handy command about securities.")
.subcommand(ls_securities_command_line_parser())
.arg_required_else_help(true);
}
fn web_ui_command_line_parser() -> Command {
return Command::new("wui")
.about("Web user interface.")
.subcommand(run_web_ui_command_line_parser())
.arg_required_else_help(true);
}
fn command_line_parser() -> Command {
return Command::new("prog")
.arg(verbosity_arg())
@@ -143,6 +171,7 @@ fn command_line_parser() -> Command {
.subcommand(event_command_line_parser())
.subcommand(user_command_line_parser())
.subcommand(securities_command_line_parser())
.subcommand(web_ui_command_line_parser())
.arg_required_else_help(true);
}
@@ -207,6 +236,17 @@ fn process_process_events_command_line(
logic.process_events(conn, files);
}
fn process_run_wui_command_line(
conn: &mut Connection,
m: &ArgMatches,
logic: &mut dyn CommandLineUserLogic,
) {
debug!("Processing run wui command line {:?}.", m);
let mut port = m.get_one::<u16>("port").unwrap_or(&3200);
let enable_template_autoreload = m.get_flag("enable-template-autoreload");
logic.run_webui(conn, *port, enable_template_autoreload);
}
fn process_securities_command_line(
conn: &Connection,
m: &ArgMatches,
@@ -235,6 +275,20 @@ fn process_events_command_line(
}
}
fn process_wui_command_line(
conn: &mut Connection,
m: &ArgMatches,
logic: &mut dyn CommandLineUserLogic,
) {
debug!("Processing wui command line {:?}.", m);
match m.subcommand() {
Some(("run", run_wui_match)) => {
process_run_wui_command_line(conn, run_wui_match, logic);
}
_ => unreachable!(),
}
}
fn process_command_line(
conn: &mut Connection,
m: &ArgMatches,
@@ -245,13 +299,15 @@ fn process_command_line(
Some(("users", user_match)) => {
process_user_command_line(conn, user_match, logic);
}
Some(("securities", securities_match)) => {
process_securities_command_line(conn, securities_match, logic);
}
Some(("events", events_match)) => {
process_events_command_line(conn, events_match, logic);
}
Some(("wui", wui_match)) => {
process_wui_command_line(conn, wui_match, logic);
}
_ => {
println!("Received un parsable command. {:?}", m.subcommand());
unreachable!();
@@ -268,6 +324,7 @@ mod cli_test {
create_user_count: usize,
list_securities_count: usize,
process_events_count: usize,
run_webui_count: usize,
}
impl TestCommandLineLogic {
@@ -277,6 +334,7 @@ mod cli_test {
create_user_count: 0,
list_securities_count: 0,
process_events_count: 0,
run_webui_count: 0,
};
}
}
@@ -297,6 +355,10 @@ mod cli_test {
fn process_events(&mut self, conn: &mut Connection, _files: ValuesRef<PathBuf>) {
self.process_events_count += 1;
}
fn run_webui(&mut self, conn: &mut Connection, port: u16, enable_template_autoreload: bool) {
self.run_webui_count += 1;
}
}
#[test]
@@ -378,6 +440,16 @@ mod cli_test {
process_command_line(&mut conn, &m, &mut logic);
assert_eq!(logic.process_events_count, 1);
}
// #[test]
// fn run_web_ui() {
// let cmd = command_line_parser();
// let m = cmd.get_matches_from(vec!["test", "webui", "run"]);
// let mut logic = TestCommandLineLogic::new();
// let mut conn = Connection::open_in_memory().unwrap();
// process_command_line(&mut conn, &m, &mut logic);
// assert_eq!(logic.run_webui_count, 1);
// }
}
struct ProCommandLineLogic {}
@@ -426,6 +498,11 @@ impl CommandLineUserLogic for ProCommandLineLogic {
println!("fuck me");
}
fn run_webui(&mut self, conn: &mut Connection, port: u16, enable_template_autoreload: bool) {
info!("Serving web-ui on port {:?}", port);
web_ui_main(port, enable_template_autoreload);
}
}
fn get_connection(m: &ArgMatches) -> Connection {