v2.2.1 - Compatibility, logging
Added feature that checks for backwards compatibility issues and fixes them if present. It checks for a version number in `data/app.json` and compares it to the current version number. The amount of logs has been severely reduced to prevent the sheer amount of clutter being produced.
This commit is contained in:
parent
093b6cd868
commit
fa924285b0
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "board_server"
|
||||
version = "2.2.0"
|
||||
version = "2.2.1"
|
||||
edition = "2018"
|
||||
license = "Zlib"
|
||||
repository = "https://git.solow.xyz/cgit.cgi/Boarders/"
|
||||
@ -21,6 +21,7 @@ tokio = {version = "1.7.1", features=["net", "rt-multi-thread", "sync", "macros"
|
||||
futures = "0.3.15"
|
||||
handlebars = "4.0.1"
|
||||
maplit = "1.0.2"
|
||||
log = "0.4.14"
|
||||
simple_logger = "1.11.0"
|
||||
chrono = "0.4.19"
|
||||
ammonia = "3.1.2"
|
||||
|
||||
8
compat/2.2.0.sh
Executable file
8
compat/2.2.0.sh
Executable file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
for board in ./data/boards/*/ ; do
|
||||
FILE="$board/board.json"
|
||||
sed -E 's/"timestamp"\:([0-9]+)/"timestamp":{"secs_since_epoch":\1,"nanos_since_epoch":0}/g' $FILE > "${FILE}.new"
|
||||
mv "${FILE}.new" $FILE
|
||||
done
|
||||
96
src/main.rs
96
src/main.rs
@ -1,11 +1,11 @@
|
||||
use std::{
|
||||
env, fs,
|
||||
env, fs, io,
|
||||
io::{Write, Read},
|
||||
sync::Mutex,
|
||||
fs::{
|
||||
ReadDir
|
||||
},
|
||||
fs::ReadDir,
|
||||
path::Path,
|
||||
error::Error
|
||||
error::Error,
|
||||
process::Command
|
||||
};
|
||||
|
||||
use actix_web::{
|
||||
@ -46,12 +46,15 @@ use handlebars::{Handlebars, handlebars_helper};
|
||||
use console::style;
|
||||
use maplit::btreemap;
|
||||
|
||||
use simple_logger::{SimpleLogger};
|
||||
use log::LevelFilter;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
pub struct ThreadData<'a> {
|
||||
handlebars: Handlebars<'a>
|
||||
}
|
||||
|
||||
use simple_logger::SimpleLogger;
|
||||
|
||||
#[get("/")]
|
||||
async fn index(
|
||||
tdata: web::Data<ThreadData<'_>>
|
||||
@ -106,10 +109,49 @@ async fn socket_loop(socket: &UnixStream) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
struct AppInfo<'s> {
|
||||
version: &'s str,
|
||||
}
|
||||
|
||||
impl<'s> AppInfo<'s> {
|
||||
fn get_version(&self) -> Option<[u8; 3]> {
|
||||
let mut a: [u8; 3] = [0; 3];
|
||||
let mut ver = self.version.split('.');
|
||||
a[0] = ver.next()?.parse().ok()?;
|
||||
a[1] = ver.next()?.parse().ok()?;
|
||||
a[2] = ver.next()?.parse().ok()?;
|
||||
Some(a)
|
||||
}
|
||||
}
|
||||
|
||||
static mut app_info: AppInfo = AppInfo {
|
||||
version: env!("CARGO_PKG_VERSION")
|
||||
};
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
async fn main() -> io::Result<()> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let mut args = args.iter();
|
||||
args.next().unwrap();
|
||||
for arg in args {
|
||||
match arg.as_str() {
|
||||
"-v" | "--version" => {
|
||||
println!("Boarders v{}", unsafe { app_info.version });
|
||||
std::process::exit(0);
|
||||
}
|
||||
_ => {
|
||||
println!("Unknown argument \"{}\"", arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* quick and dirty logging */
|
||||
SimpleLogger::new().init().unwrap();
|
||||
SimpleLogger::new()
|
||||
.with_module_level("mio::poll", LevelFilter::Off)
|
||||
.with_module_level("actix_server::worker", LevelFilter::Debug)
|
||||
.with_module_level("handlebars", LevelFilter::Info)
|
||||
.init().unwrap();
|
||||
|
||||
println!("Initializing...");
|
||||
/*
|
||||
@ -122,6 +164,40 @@ async fn main() -> std::io::Result<()> {
|
||||
let data_dir = Path::new("data/");
|
||||
let mut pwd = env::current_dir().unwrap();
|
||||
|
||||
/* Getting app version and checking for backwards compatability issues */
|
||||
let mut app_info_file = data_dir.to_path_buf();
|
||||
app_info_file.push("app.json");
|
||||
let mut app_info_file = fs::OpenOptions::new().read(true).write(true).create(true).open(app_info_file).unwrap();
|
||||
let mut app_info_str = String::new();
|
||||
app_info_file.read_to_string(&mut app_info_str).unwrap();
|
||||
let cur_app_info: AppInfo = match serde_json::from_str(&app_info_str) {
|
||||
Ok(a) => a,
|
||||
Err(_) => {
|
||||
println!("Malformed app.json file, backing up then creating new");
|
||||
let s = unsafe { serde_json::to_string(&app_info).unwrap() };
|
||||
app_info_file.write_all(s.as_bytes()).unwrap();
|
||||
unsafe { app_info.clone() }
|
||||
}
|
||||
};
|
||||
|
||||
let ver = cur_app_info.get_version().unwrap();
|
||||
match ver[0] {
|
||||
x if x <= 2 => match ver[1] {
|
||||
y if y <= 2 => match ver[2] {
|
||||
0 => {
|
||||
if Command::new("./compat/2.2.0.sh").output().is_err() {
|
||||
println!("Error, could not complete compatability upgrade");
|
||||
} else {
|
||||
println!("Fixed compat issues for v2.2.0");
|
||||
}
|
||||
}
|
||||
_ => {println!("Invalid version number?")}
|
||||
}
|
||||
_ => {println!("Invalid version number?")}
|
||||
}
|
||||
_ => {println!("Invalid version number?")}
|
||||
}
|
||||
|
||||
/* open/create the directory where board data is placed */
|
||||
let board_files: ReadDir = fs::read_dir({
|
||||
pwd.push(data_dir);
|
||||
@ -273,7 +349,7 @@ async fn main() -> std::io::Result<()> {
|
||||
* loaded dynamically or statically, probably via environment
|
||||
* variable at compile time
|
||||
*/
|
||||
/* register handlebars partials here */
|
||||
/* register handlebars partials here */
|
||||
handlebars_helper!(msg_helper: |s: str| format!("helped: {}", s.to_string()));
|
||||
h.register_helper("msg", Box::new(msg_helper));
|
||||
h.register_partial("head", include_str!("res/snippets/head.hbs")).unwrap();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user