mirror of https://github.com/raftario/filite.git
Moved setup module to separate file
This commit is contained in:
parent
015e9c0d1a
commit
12a00a81b9
109
src/lib.rs
109
src/lib.rs
|
@ -11,114 +11,7 @@ use diesel::sqlite::SqliteConnection;
|
|||
pub mod models;
|
||||
pub mod queries;
|
||||
pub mod schema;
|
||||
pub mod setup;
|
||||
|
||||
/// SQLite database connection pool
|
||||
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||
|
||||
/// Functions used for the initial setup
|
||||
pub mod setup {
|
||||
use crate::Pool;
|
||||
|
||||
use diesel::r2d2::{self, ConnectionManager};
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
use dotenv;
|
||||
#[cfg(debug_assertions)]
|
||||
use std::env;
|
||||
|
||||
/// Returns a path to the directory storing application data
|
||||
pub fn get_data_dir() -> PathBuf {
|
||||
let mut dir = dirs::home_dir().expect("Can't find home directory.");
|
||||
dir.push(".filite");
|
||||
dir
|
||||
}
|
||||
|
||||
/// Returns a path to the configuration file
|
||||
fn get_config_path() -> PathBuf {
|
||||
let mut path = get_data_dir();
|
||||
path.push("config.toml");
|
||||
path
|
||||
}
|
||||
|
||||
/// Application configuration
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct Config {
|
||||
/// SQLite database connection url
|
||||
database_url: String,
|
||||
/// SQLite database connection pool size
|
||||
pool_size: u32,
|
||||
/// Directory where to store static files
|
||||
files_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
let database_url = {
|
||||
cfg_if! {
|
||||
if #[cfg(debug_assertions)] {
|
||||
dotenv::dotenv().ok();
|
||||
env::var("DATABASE_URL").expect("Can't parse DATABASE_URL environment variable.")
|
||||
} else {
|
||||
let mut path = get_data_dir();
|
||||
path.push("database.db");
|
||||
path.to_str()
|
||||
.expect("Can't convert database path to string.")
|
||||
.to_owned()
|
||||
}
|
||||
}
|
||||
};
|
||||
let pool_size = num_cpus::get() as u32 / 2;
|
||||
let files_dir = {
|
||||
let mut path = get_data_dir();
|
||||
path.push("data");
|
||||
path
|
||||
};
|
||||
|
||||
Config {
|
||||
database_url,
|
||||
pool_size,
|
||||
files_dir,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Deserialize the config file
|
||||
pub fn read_file() -> Result<Self, &'static str> {
|
||||
let path = get_config_path();
|
||||
let contents = if let Ok(contents) = fs::read_to_string(&path) {
|
||||
contents
|
||||
} else {
|
||||
return Err("Can't read config file.");
|
||||
};
|
||||
let result = toml::from_str(&contents);
|
||||
match result {
|
||||
Ok(result) => Ok(result),
|
||||
Err(_) => Err("Invalid config file."),
|
||||
}
|
||||
}
|
||||
|
||||
/// Serialize the config file
|
||||
pub fn write_file(&self) -> Result<(), &'static str> {
|
||||
let path = get_config_path();
|
||||
let contents = toml::to_string(&self).expect("Can't serialize config.");
|
||||
match fs::write(&path, &contents) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err("Can't write config file."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a SQLite database connection pool
|
||||
pub fn create_pool(url: &str, size: u32) -> Pool {
|
||||
let manager = ConnectionManager::<SqliteConnection>::new(url);
|
||||
r2d2::Pool::builder()
|
||||
.max_size(size)
|
||||
.build(manager)
|
||||
.expect("Can't create pool.")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,106 @@
|
|||
//! Functions used for the initial setup
|
||||
|
||||
use crate::Pool;
|
||||
|
||||
use diesel::r2d2::{self, ConnectionManager};
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
use dotenv;
|
||||
#[cfg(debug_assertions)]
|
||||
use std::env;
|
||||
|
||||
/// Returns a path to the directory storing application data
|
||||
pub fn get_data_dir() -> PathBuf {
|
||||
let mut dir = dirs::home_dir().expect("Can't find home directory.");
|
||||
dir.push(".filite");
|
||||
dir
|
||||
}
|
||||
|
||||
/// Returns a path to the configuration file
|
||||
fn get_config_path() -> PathBuf {
|
||||
let mut path = get_data_dir();
|
||||
path.push("config.toml");
|
||||
path
|
||||
}
|
||||
|
||||
/// Application configuration
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct Config {
|
||||
/// SQLite database connection url
|
||||
database_url: String,
|
||||
/// SQLite database connection pool size
|
||||
pool_size: u32,
|
||||
/// Directory where to store static files
|
||||
files_dir: PathBuf,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
let database_url = {
|
||||
cfg_if! {
|
||||
if #[cfg(debug_assertions)] {
|
||||
dotenv::dotenv().ok();
|
||||
env::var("DATABASE_URL").expect("Can't parse DATABASE_URL environment variable.")
|
||||
} else {
|
||||
let mut path = get_data_dir();
|
||||
path.push("database.db");
|
||||
path.to_str()
|
||||
.expect("Can't convert database path to string.")
|
||||
.to_owned()
|
||||
}
|
||||
}
|
||||
};
|
||||
let pool_size = num_cpus::get() as u32 / 2;
|
||||
let files_dir = {
|
||||
let mut path = get_data_dir();
|
||||
path.push("data");
|
||||
path
|
||||
};
|
||||
|
||||
Config {
|
||||
database_url,
|
||||
pool_size,
|
||||
files_dir,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Deserialize the config file
|
||||
pub fn read_file() -> Result<Self, &'static str> {
|
||||
let path = get_config_path();
|
||||
let contents = if let Ok(contents) = fs::read_to_string(&path) {
|
||||
contents
|
||||
} else {
|
||||
return Err("Can't read config file.");
|
||||
};
|
||||
let result = toml::from_str(&contents);
|
||||
match result {
|
||||
Ok(result) => Ok(result),
|
||||
Err(_) => Err("Invalid config file."),
|
||||
}
|
||||
}
|
||||
|
||||
/// Serialize the config file
|
||||
pub fn write_file(&self) -> Result<(), &'static str> {
|
||||
let path = get_config_path();
|
||||
let contents = toml::to_string(&self).expect("Can't serialize config.");
|
||||
match fs::write(&path, &contents) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err("Can't write config file."),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a SQLite database connection pool
|
||||
pub fn create_pool(url: &str, size: u32) -> Pool {
|
||||
let manager = ConnectionManager::<SqliteConnection>::new(url);
|
||||
r2d2::Pool::builder()
|
||||
.max_size(size)
|
||||
.build(manager)
|
||||
.expect("Can't create pool.")
|
||||
}
|
Loading…
Reference in New Issue