♻️ refactor: change PerInstance to Startup

main
_ 2020-12-20 17:44:03 +00:00
parent d052f42507
commit e8d94da661
4 changed files with 13 additions and 12 deletions

View File

@ -40,7 +40,7 @@ pub struct Config {
struct ServerState <'a> { struct ServerState <'a> {
config: Config, config: Config,
handlebars: handlebars::Handlebars <'a>, handlebars: handlebars::Handlebars <'a>,
instance_metrics: metrics::PerInstance, instance_metrics: metrics::Startup,
hidden_path: Option <PathBuf>, hidden_path: Option <PathBuf>,
} }
@ -105,7 +105,7 @@ async fn main () -> Result <(), anyhow::Error> {
let handlebars = file_server::load_templates (&PathBuf::new ())?; let handlebars = file_server::load_templates (&PathBuf::new ())?;
let instance_metrics = metrics::PerInstance::new ( let instance_metrics = metrics::Startup::new (
config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ()) config_file.name.unwrap_or_else (|| "PTTH File Server".to_string ())
); );

View File

@ -12,7 +12,7 @@ fn serialize_ulid <S: serde::Serializer> (t: &Ulid, s: S)
// They don't change after that. // They don't change after that.
#[derive (Debug, serde::Serialize)] #[derive (Debug, serde::Serialize)]
pub struct PerInstance { pub struct Startup {
// D-Bus machine ID, if we're on Linux // D-Bus machine ID, if we're on Linux
pub machine_id: Option <String>, pub machine_id: Option <String>,
@ -28,6 +28,7 @@ pub struct PerInstance {
#[serde (serialize_with = "serialize_ulid")] #[serde (serialize_with = "serialize_ulid")]
pub instance_id: Ulid, pub instance_id: Ulid,
// System UTC
pub startup_utc: DateTime <Utc>, pub startup_utc: DateTime <Utc>,
} }
@ -54,7 +55,7 @@ fn get_machine_id () -> Option <String> {
Some (s) Some (s)
} }
impl PerInstance { impl Startup {
#[must_use] #[must_use]
pub fn new (server_name: String) -> Self pub fn new (server_name: String) -> Self
{ {
@ -74,8 +75,8 @@ mod tests {
#[test] #[test]
fn ulid_null () { fn ulid_null () {
let a = PerInstance::new ("bogus".to_string ()); let a = Startup::new ("bogus".to_string ());
let b = PerInstance::new ("bogus".to_string ()); let b = Startup::new ("bogus".to_string ());
assert_ne! (a.instance_id, b.instance_id); assert_ne! (a.instance_id, b.instance_id);
} }

View File

@ -89,7 +89,7 @@ struct DirEntryHtml {
#[derive (Serialize)] #[derive (Serialize)]
struct DirHtml <'a> { struct DirHtml <'a> {
#[serde (flatten)] #[serde (flatten)]
instance_metrics: &'a metrics::PerInstance, instance_metrics: &'a metrics::Startup,
path: Cow <'a, str>, path: Cow <'a, str>,
entries: Vec <DirEntryHtml>, entries: Vec <DirEntryHtml>,
@ -187,7 +187,7 @@ async fn read_dir_entry_json (entry: DirEntry) -> Option <DirEntryJson>
async fn serve_root ( async fn serve_root (
handlebars: &Handlebars <'static>, handlebars: &Handlebars <'static>,
instance_metrics: &metrics::PerInstance instance_metrics: &metrics::Startup
) -> Result <Response, FileServerError> ) -> Result <Response, FileServerError>
{ {
let s = handlebars.render ("file_server_root", &instance_metrics)?; let s = handlebars.render ("file_server_root", &instance_metrics)?;
@ -232,7 +232,7 @@ async fn serve_dir_json (
#[instrument (level = "debug", skip (handlebars, instance_metrics, dir))] #[instrument (level = "debug", skip (handlebars, instance_metrics, dir))]
async fn serve_dir_html ( async fn serve_dir_html (
handlebars: &Handlebars <'static>, handlebars: &Handlebars <'static>,
instance_metrics: &metrics::PerInstance, instance_metrics: &metrics::Startup,
path: Cow <'_, str>, path: Cow <'_, str>,
mut dir: ReadDir mut dir: ReadDir
) -> Result <Response, FileServerError> ) -> Result <Response, FileServerError>
@ -358,7 +358,7 @@ async fn serve_file (
#[instrument (level = "debug", skip (handlebars, headers, instance_metrics))] #[instrument (level = "debug", skip (handlebars, headers, instance_metrics))]
pub async fn serve_all ( pub async fn serve_all (
handlebars: &Handlebars <'static>, handlebars: &Handlebars <'static>,
instance_metrics: &metrics::PerInstance, instance_metrics: &metrics::Startup,
root: &Path, root: &Path,
method: Method, method: Method,
uri: &str, uri: &str,

View File

@ -54,7 +54,7 @@ pub fn password_is_bad (mut password: String) -> bool {
struct ServerState { struct ServerState {
config: Config, config: Config,
handlebars: Handlebars <'static>, handlebars: Handlebars <'static>,
instance_metrics: file_server::metrics::PerInstance, instance_metrics: file_server::metrics::Startup,
gauges: RwLock <file_server::metrics::Gauges>, gauges: RwLock <file_server::metrics::Gauges>,
client: Client, client: Client,
hidden_path: Option <PathBuf>, hidden_path: Option <PathBuf>,
@ -205,7 +205,7 @@ pub async fn run_server (
.build ().map_err (ServerError::CantBuildHttpClient)?; .build ().map_err (ServerError::CantBuildHttpClient)?;
let handlebars = file_server::load_templates (&asset_root)?; let handlebars = file_server::load_templates (&asset_root)?;
let instance_metrics = file_server::metrics::PerInstance::new (config_file.name); let instance_metrics = file_server::metrics::Startup::new (config_file.name);
let state = Arc::new (ServerState { let state = Arc::new (ServerState {
config: Config { config: Config {