use std::thread;
use std::time::Duration;
use rich_rs::{Console, Highlighter, RegexHighlighter, Style, Text, Theme};
fn main() {
let mut console = Console::new();
rich_rs::log!(console, &Text::plain("Server starting...")).unwrap();
rich_rs::log!(console, &Text::plain("Loading configuration...")).unwrap();
thread::sleep(Duration::from_millis(500));
rich_rs::log!(
console,
&Text::from_markup("Serving on [bold cyan]http://127.0.0.1:8000[/]", false).unwrap()
)
.unwrap();
thread::sleep(Duration::from_millis(500));
let request_highlighter = RegexHighlighter::new(
&[
r"(?P<protocol>HTTP)\s+(?P<method>GET|POST|PUT|DELETE|PATCH)\s+(?P<path>\S+)\s+(?P<status>\d{3})",
r"\/(?P<filename>\w+\.\w{2,4})",
],
"req.",
);
let mut theme = Theme::new();
theme.add_style(
"req.protocol",
Style::parse("dim bold green").unwrap_or_default(),
);
theme.add_style("req.method", Style::parse("bold cyan").unwrap_or_default());
theme.add_style("req.path", Style::parse("magenta").unwrap_or_default());
theme.add_style(
"req.status",
Style::parse("bold yellow").unwrap_or_default(),
);
theme.add_style(
"req.filename",
Style::parse("bright_cyan").unwrap_or_default(),
);
console.push_theme(theme);
let requests = [
"HTTP GET /index.html 200",
"HTTP GET /static/style.css 200",
"HTTP GET /api/users 200",
"HTTP POST /api/login 201",
"HTTP GET /favicon.ico 404",
];
for request in requests {
thread::sleep(Duration::from_millis(300));
let mut text = Text::plain(request);
request_highlighter.highlight(&mut text);
rich_rs::log!(console, &text).unwrap();
}
thread::sleep(Duration::from_millis(500));
console
.log(
&Text::from_markup("[bold green]Server shutting down gracefully...[/]", false).unwrap(),
None,
None,
)
.unwrap();
console
.log(
&Text::from_markup("[dim]Goodbye![/]", false).unwrap(),
Some(file!()),
Some(line!()),
)
.unwrap();
}