
- added cli param to change threads used by server - added cli param hide html header and/or footer - added cli param to remove css styling - added cli param to set http basic auth username and optionally password for the server - file pastas now delete the files from the disk as well when they expire
54 lines
1.6 KiB
Rust
54 lines
1.6 KiB
Rust
use std::fs::File;
|
|
use std::io;
|
|
use std::io::{BufReader, BufWriter};
|
|
|
|
use crate::Pasta;
|
|
|
|
static DATABASE_PATH: &'static str = "pasta_data/database.json";
|
|
|
|
pub fn save_to_file(pasta_data: &Vec<Pasta>) {
|
|
let mut file = File::create(DATABASE_PATH);
|
|
match file {
|
|
Ok(_) => {
|
|
let writer = BufWriter::new(file.unwrap());
|
|
serde_json::to_writer(writer, &pasta_data).expect("Failed to create JSON writer");
|
|
}
|
|
Err(_) => {
|
|
log::info!("Database file {} not found!", DATABASE_PATH);
|
|
file = File::create(DATABASE_PATH);
|
|
match file {
|
|
Ok(_) => {
|
|
log::info!("Database file {} created.", DATABASE_PATH);
|
|
save_to_file(pasta_data);
|
|
}
|
|
Err(err) => {
|
|
log::error!(
|
|
"Failed to create database file {}: {}!",
|
|
&DATABASE_PATH,
|
|
&err
|
|
);
|
|
panic!("Failed to create database file {}: {}!", DATABASE_PATH, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn load_from_file() -> io::Result<Vec<Pasta>> {
|
|
let file = File::open(DATABASE_PATH);
|
|
match file {
|
|
Ok(_) => {
|
|
let reader = BufReader::new(file.unwrap());
|
|
let data: Vec<Pasta> = serde_json::from_reader(reader).unwrap();
|
|
Ok(data)
|
|
}
|
|
Err(_) => {
|
|
log::info!("Database file {} not found!", DATABASE_PATH);
|
|
save_to_file(&Vec::<Pasta>::new());
|
|
|
|
log::info!("Database file {} created.", DATABASE_PATH);
|
|
load_from_file()
|
|
}
|
|
}
|
|
}
|