Add more debug info to webpages

This commit is contained in:
NGnius (Graham) 2024-05-11 00:18:42 -04:00
parent 8211caf2f0
commit 718beb1375
8 changed files with 51 additions and 5 deletions

View file

@ -6,9 +6,12 @@ mod storage;
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder}; use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
use simplelog::{LevelFilter, WriteLogger}; use simplelog::{LevelFilter, WriteLogger};
static ARGS: std::sync::OnceLock<cli::CliArgs> = std::sync::OnceLock::new();
#[get("/version_info")] #[get("/version_info")]
async fn hello() -> impl Responder { async fn hello() -> impl Responder {
HttpResponse::Ok().body(format!("{} v{}", consts::PACKAGE_NAME, consts::PACKAGE_VERSION)) let args = ARGS.get_or_init(|| cli::CliArgs::get());
HttpResponse::Ok().body(format!("{} v{} {}", consts::PACKAGE_NAME, consts::PACKAGE_VERSION, args.storage.clone().to_descriptor()))
} }
fn build_storage_box(storage: &cli::StorageArgs) -> Box<dyn storage::IStorage> { fn build_storage_box(storage: &cli::StorageArgs) -> Box<dyn storage::IStorage> {
@ -43,7 +46,7 @@ fn build_storage_box(storage: &cli::StorageArgs) -> Box<dyn storage::IStorage> {
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
let args = cli::CliArgs::get(); let args = ARGS.get_or_init(|| cli::CliArgs::get());
let log_filepath = std::path::Path::new("/tmp").join(format!("{}.log", consts::PACKAGE_NAME)); let log_filepath = std::path::Path::new("/tmp").join(format!("{}.log", consts::PACKAGE_NAME));
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
let log_level = LevelFilter::Debug; let log_level = LevelFilter::Debug;

View file

@ -1,14 +1,21 @@
use std::sync::OnceLock;
use actix_web::{get, HttpResponse, Responder, HttpRequest, Either}; use actix_web::{get, HttpResponse, Responder, HttpRequest, Either};
use crate::storage::IStorage;
static DESCRIPTOR: OnceLock<String> = OnceLock::new();
#[get("/")] #[get("/")]
pub async fn decky_index(req: HttpRequest) -> impl Responder { pub async fn decky_index(req: HttpRequest, data: actix_web::web::Data<Box<dyn IStorage>>) -> impl Responder {
if req.headers().contains_key("X-Decky-Version") { if req.headers().contains_key("X-Decky-Version") {
// a real store request // a real store request
Either::Left(actix_web::web::Redirect::to("/plugins").temporary()) Either::Left(actix_web::web::Redirect::to("/plugins").temporary())
} else { } else {
let descriptor = DESCRIPTOR.get_or_init(|| data.descriptor());
Either::Right(HttpResponse::Ok() Either::Right(HttpResponse::Ok()
.insert_header(("Content-Type", "text/html")) .insert_header(("Content-Type", "text/html"))
.body(r##" .body(format!(r##"
<!DOCTYTPE html> <!DOCTYTPE html>
<html lang="en"> <html lang="en">
<head> <head>
@ -28,9 +35,11 @@ pub async fn decky_index(req: HttpRequest) -> impl Responder {
<h3>Maybe one day this page will display plugins in a human readable way...</h3> <h3>Maybe one day this page will display plugins in a human readable way...</h3>
<br/> <br/>
<a href="/plugins">plugins</a> <a href="/plugins">plugins</a>
| <a href="/version_info">version</a>
| <a href="https://git.ngni.us/NG-SD-Plugins/not-decky-store">code</a> | <a href="https://git.ngni.us/NG-SD-Plugins/not-decky-store">code</a>
| GPL v3.0 | GPL v3.0
| <code>{}</code>
</body> </body>
</html>"##)) </html>"##, descriptor)))
} }
} }

View file

@ -115,4 +115,8 @@ impl<S: AsRef<dyn IStorage> + Send + Sync> IStorage for CachedStorage<S> {
fn get_statistics(&self) -> std::collections::HashMap<String, u64> { fn get_statistics(&self) -> std::collections::HashMap<String, u64> {
self.statistics_cache.get(|| self.fallback.as_ref().get_statistics()) self.statistics_cache.get(|| self.fallback.as_ref().get_statistics())
} }
fn descriptor(&self) -> String {
format!("cached {}", self.fallback.as_ref().descriptor())
}
} }

View file

@ -302,4 +302,8 @@ impl IStorage for FileStorage {
} }
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "Plugin version not found")) Err(std::io::Error::new(std::io::ErrorKind::NotFound, "Plugin version not found"))
} }
fn descriptor(&self) -> String {
format!("filesystem {} @ {} ? {}", self.root.display(), &self.domain_root, self.query_handler.descriptor())
}
} }

View file

@ -16,6 +16,8 @@ pub trait IStorage: Send + Sync {
fn increment_statistic(&self, _name: &str, _version: &str, _query: &decky_api::StorePluginIncrement) -> Result<decky_api::StorePluginVersion, std::io::Error> { fn increment_statistic(&self, _name: &str, _version: &str, _query: &decky_api::StorePluginIncrement) -> Result<decky_api::StorePluginVersion, std::io::Error> {
Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "Statistics increment not supported")) Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "Statistics increment not supported"))
} }
fn descriptor(&self) -> String;
} }
pub struct EmptyStorage; pub struct EmptyStorage;
@ -24,6 +26,10 @@ impl IStorage for EmptyStorage {
fn plugins(&self, _query: &str) -> decky_api::StorePluginList { fn plugins(&self, _query: &str) -> decky_api::StorePluginList {
Vec::new() Vec::new()
} }
fn descriptor(&self) -> String {
"empty".to_owned()
}
} }
pub trait IQueryHandler: Send + Sync { pub trait IQueryHandler: Send + Sync {
@ -33,4 +39,6 @@ pub trait IQueryHandler: Send + Sync {
fn supported_params(&self, query: &str) -> String { fn supported_params(&self, query: &str) -> String {
query.to_owned() query.to_owned()
} }
fn descriptor(&self) -> String;
} }

View file

@ -148,4 +148,14 @@ impl<S: AsRef<dyn IStorage> + Send + Sync> IStorage for MergedStorage<S> {
stats stats
} }
fn descriptor(&self) -> String {
use std::fmt::Write;
let mut desc = String::from("merged: ");
for store in &self.stores {
write!(desc, "{}, ", store.as_ref().descriptor()).unwrap()
}
desc.truncate(desc.len() - 2);
desc
}
} }

View file

@ -154,4 +154,8 @@ impl IStorage for ProxiedStorage {
fn get_statistics(&self) -> std::collections::HashMap<String, u64> { fn get_statistics(&self) -> std::collections::HashMap<String, u64> {
self.fallback.get_statistics() self.fallback.get_statistics()
}*/ }*/
fn descriptor(&self) -> String {
format!("proxy {} @ {} ? proxied", &self.store_url, &self.domain_root)
}
} }

View file

@ -54,4 +54,8 @@ impl IQueryHandler for StandardPostRetrievalSort {
Err(_) => "".to_owned() Err(_) => "".to_owned()
} }
} }
fn descriptor(&self) -> String {
"standard_post_retrieval".to_owned()
}
} }