Small changes

This commit is contained in:
Shiroyasha 2024-09-29 22:35:46 +03:00
parent 7196c719e2
commit 0526cc9412
Signed by: shiroyashik
GPG key ID: E4953D3940D7860A
8 changed files with 84 additions and 69 deletions

View file

@ -5,9 +5,10 @@ pub const LOGS_ENV: &'static str = "LOGS_FOLDER";
pub const ASSETS_ENV: &'static str = "ASSETS_FOLDER";
pub const AVATARS_ENV: &'static str = "AVATARS_FOLDER";
// Sculptor update checker
// Instance info
pub const SCULPTOR_VERSION: &'static str = env!("CARGO_PKG_VERSION");
pub const REPOSITORY: &'static str = "shiroyashik/sculptor";
pub const GIT_HASH: &'static str = env!("GIT_HASH", "Can't run git or build.rs failing!");
// reqwest parameters
pub const USER_AGENT: &'static str = "reqwest";

View file

@ -35,12 +35,7 @@ use state::Config;
// Utils
mod utils;
use utils::{
check_updates, download_assets, get_commit_sha,
get_limit_as_bytes, get_log_file, get_path_to_assets_hash,
is_assets_outdated, remove_assets, update_advanced_users,
update_bans_from_minecraft, write_sha_to_file, FiguraVersions
};
use utils::*;
#[derive(Debug, Clone)]
pub struct AppState {
@ -78,9 +73,10 @@ lazy_static! {
#[tokio::main]
async fn main() -> Result<()> {
// 1. Set up env
let _ = dotenvy::dotenv();
// "trace,axum=info,tower_http=info,tokio=info,tungstenite=info,tokio_tungstenite=info",
// 2. Set up logging
let file_appender = tracing_appender::rolling::never(&*LOGS_VAR, get_log_file(&*LOGS_VAR));
let timer = ChronoLocal::new(String::from("%Y-%m-%dT%H:%M:%S%.3f%:z"));
@ -111,8 +107,34 @@ async fn main() -> Result<()> {
prev_hook(panic_info);
}));
tracing::info!("The Sculptor v{}{}", SCULPTOR_VERSION, check_updates(REPOSITORY, &SCULPTOR_VERSION).await?);
// 3. Display info about current instance and check updates
tracing::info!("The Sculptor v{SCULPTOR_VERSION}+{} ({REPOSITORY})", &GIT_HASH[..7]);
// let _ = check_updates(REPOSITORY, SCULPTOR_VERSION).await; // Currently, there is no need to do anything with the result of the function
match get_latest_version(REPOSITORY).await {
Ok(latest_version) => {
if latest_version > semver::Version::parse(SCULPTOR_VERSION).expect("SCULPTOR_VERSION does not match SemVer!") {
tracing::info!("Available new v{latest_version}! Check https://github.com/{REPOSITORY}/releases");
} else {
tracing::info!("Sculptor are up to date!");
}
},
Err(e) => {
tracing::error!("Can't fetch Sculptor updates due: {e:?}");
},
}
// 4. Starting an app() that starts to serve. If app() returns true, the sculptor will be restarted. for future
loop {
if !app().await? {
break;
}
}
Ok(())
}
async fn app() -> Result<bool> {
// Preparing for launch
{
let path = PathBuf::from(&*AVATARS_VAR);
@ -212,8 +234,8 @@ async fn main() -> Result<()> {
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await?;
tracing::info!("Serve stopped. Closing...");
Ok(())
tracing::info!("Serve stopped.");
Ok(false)
}
async fn shutdown_signal() {

View file

@ -1,11 +1,10 @@
use std::path::{self, PathBuf};
use anyhow::anyhow;
use anyhow::bail;
use reqwest::Client;
use semver::Version;
use serde::{Deserialize, Serialize};
use tokio::{fs::{self, File}, io::{AsyncReadExt as _, AsyncWriteExt as _}};
use tracing::error;
use crate::{ASSETS_VAR, FIGURA_ASSETS_ZIP_URL, FIGURA_RELEASES_URL, TIMEOUT, USER_AGENT};
@ -14,7 +13,7 @@ struct Tag {
name: String
}
async fn get_latest_version(repo: &str, current_version: Version) -> anyhow::Result<Option<String>> {
pub async fn get_latest_version(repo: &str) -> anyhow::Result<Version> {
let url = format!("https://api.github.com/repos/{repo}/tags");
let client = Client::builder().timeout(TIMEOUT).user_agent(USER_AGENT).build().unwrap();
let response = client.get(&url).send().await?;
@ -31,32 +30,12 @@ async fn get_latest_version(repo: &str, current_version: Version) -> anyhow::Res
})
.max();
if let Some(latest_version) = latest_tag {
if latest_version > current_version {
Ok(Some(format!("Available new v{latest_version}")))
} else {
Ok(Some("Up to date".to_string()))
}
Ok(latest_version)
} else {
Err(anyhow!("Can't find version tags!"))
bail!("Can't find version tags!")
}
} else {
Err(anyhow!("Response status code: {}", response.status().as_u16()))
}
}
pub async fn check_updates(repo: &str, current_version: &str) -> anyhow::Result<String> {
let current_version = semver::Version::parse(&current_version)?;
match get_latest_version(repo, current_version).await {
Ok(d) => if let Some(text) = d {
Ok(format!(" - {text}!"))
} else {
Ok(String::new())
},
Err(e) => {
error!("Can't fetch updates: {e:?}");
Ok(String::new())
},
bail!("Response status code: {}", response.status().as_u16())
}
}
@ -79,7 +58,7 @@ pub async fn get_figura_versions() -> anyhow::Result<FiguraVersions> {
let multiple_releases: Vec<Release> = response.json().await?;
for release in multiple_releases {
let tag_ver = if let Ok(res) = Version::parse(&release.tag_name) { res } else {
error!("Incorrect tag name! {release:?}");
tracing::error!("Incorrect tag name! {release:?}");
continue;
};
if release.prerelease {
@ -98,7 +77,7 @@ pub async fn get_figura_versions() -> anyhow::Result<FiguraVersions> {
// Stop
Ok(FiguraVersions { release: release_ver.to_string(), prerelease: prerelease_ver.to_string() })
} else {
Err(anyhow!("Response status code: {}", response.status().as_u16()))
bail!("Response status code: {}", response.status().as_u16())
}
}