From ce79ff8a76cb2cae9dcc4d2bfe483720378d2ff9 Mon Sep 17 00:00:00 2001 From: imlonghao Date: Sun, 23 Jan 2022 16:12:14 +0800 Subject: [PATCH] feat: impl API /imlonghao-api/pkg/{name}/log/{ts} --- src/main.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 8526cef..c1ed214 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,19 @@ use serde::Serialize; use std::fmt::Display; use yaml_rust::YamlLoader; -#[derive(Debug, ToSql, FromSql, Display)] +const STYLE_HTML: &'static str = r#""#; + +#[derive(Debug, ToSql, FromSql, Display, PartialEq)] #[postgres(name = "batchevent")] enum BatchEvent { #[postgres(name = "start")] @@ -211,6 +223,39 @@ async fn get_pkg( HttpResponse::Ok().json(results) } +#[get("/imlonghao-api/pkg/{name}/log/{ts}")] +async fn get_pkg_log( + path: web::Path<(String, i64)>, + db: web::Data, +) -> impl Responder { + let (name, ts) = path.into_inner(); + let dt = chrono::DateTime::::from_utc( + chrono::naive::NaiveDateTime::from_timestamp(ts, 0), + chrono::Utc, + ); + let conn = db.get().await.unwrap(); + let rows = conn + .query( + "select * from lilac.batch where ts < $1 order by id desc limit 1", + &[&dt], + ) + .await + .unwrap(); + let event: BatchEvent = rows[0].get("event"); + if event != BatchEvent::Start { + return HttpResponse::BadRequest().body("wrong time"); + } + let logdir: String = rows[0].get("logdir"); + let contents = + std::fs::read_to_string(format!("/home/lilydjwg/.lilac/log/{}/{}.log", logdir, name)) + .unwrap(); + let converted = ansi_to_html::convert(&contents, true, false).unwrap(); + let html = format!("{}{}", STYLE_HTML, converted); + HttpResponse::Ok() + .content_type("text/html; charset=UTF-8") + .body(html) +} + #[actix_web::main] async fn main() -> std::io::Result<()> { let mut cfg = deadpool_postgres::Config::new(); @@ -234,6 +279,7 @@ async fn main() -> std::io::Result<()> { .service(current) .service(logs) .service(get_pkg) + .service(get_pkg_log) }) .bind("127.0.0.1:9077")? .run()