main.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. use serde::Deserialize;
  2. use {
  3. crate::lazer_publisher::LazerPublisher,
  4. anyhow::Context,
  5. clap::Parser,
  6. tracing::{info, level_filters::LevelFilter},
  7. tracing_subscriber::{EnvFilter, fmt::format::FmtSpan},
  8. };
  9. mod config;
  10. mod http_server;
  11. mod jrpc_handle;
  12. mod lazer_publisher;
  13. mod publisher_handle;
  14. pub mod relayer_session;
  15. mod websocket_utils;
  16. #[derive(Parser, Deserialize)]
  17. #[command(version)]
  18. struct Cli {
  19. #[clap(short, long, default_value = "config/config.toml")]
  20. config: String,
  21. #[clap(short, long, default_value = "json")]
  22. log_format: LogFormat,
  23. }
  24. #[derive(clap::ValueEnum, Clone, Deserialize, Default)]
  25. enum LogFormat {
  26. #[default]
  27. Json,
  28. Compact,
  29. Pretty,
  30. }
  31. #[tokio::main]
  32. async fn main() -> anyhow::Result<()> {
  33. let args = Cli::parse();
  34. init_tracing_subscriber(args.log_format);
  35. let config =
  36. config::load_config(args.config.to_string()).context("Failed to read config file")?;
  37. info!(?config, "starting lazer-agent");
  38. let lazer_publisher = LazerPublisher::new(&config).await;
  39. http_server::run(config, lazer_publisher).await?;
  40. Ok(())
  41. }
  42. fn init_tracing_subscriber(log_format: LogFormat) {
  43. #[allow(
  44. clippy::expect_used,
  45. reason = "application can fail on invalid RUST_LOG"
  46. )]
  47. let subscriber = tracing_subscriber::fmt()
  48. .with_env_filter(
  49. EnvFilter::builder()
  50. .with_default_directive(LevelFilter::INFO.into())
  51. .from_env()
  52. .expect("invalid RUST_LOG env var"),
  53. )
  54. .with_span_events(FmtSpan::NONE);
  55. match log_format {
  56. LogFormat::Json => {
  57. subscriber.json().with_span_list(false).init();
  58. }
  59. LogFormat::Compact => {
  60. subscriber.compact().init();
  61. }
  62. LogFormat::Pretty => {
  63. subscriber.pretty().init();
  64. }
  65. }
  66. }