Create default config in config dirs
This commit is contained in:
parent
a3c0b194e1
commit
2141c992d7
105
Cargo.lock
generated
105
Cargo.lock
generated
@ -310,6 +310,9 @@ dependencies = [
|
||||
"askama_web",
|
||||
"axum",
|
||||
"camino",
|
||||
"dirs",
|
||||
"log",
|
||||
"pretty_env_logger",
|
||||
"sea-orm",
|
||||
"serde",
|
||||
"snafu",
|
||||
@ -595,6 +598,27 @@ dependencies = [
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "6.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e"
|
||||
dependencies = [
|
||||
"dirs-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs-sys"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"option-ext",
|
||||
"redox_users",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "display_full_error"
|
||||
version = "1.1.0"
|
||||
@ -627,6 +651,19 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580"
|
||||
dependencies = [
|
||||
"humantime",
|
||||
"is-terminal",
|
||||
"log",
|
||||
"regex",
|
||||
"termcolor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.2"
|
||||
@ -867,6 +904,12 @@ version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
|
||||
|
||||
[[package]]
|
||||
name = "hex"
|
||||
version = "0.4.3"
|
||||
@ -945,6 +988,12 @@ version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "135b12329e5e3ce057a9f972339ea52bc954fe1e9358ef27f95e89716fbc5424"
|
||||
|
||||
[[package]]
|
||||
name = "hyper"
|
||||
version = "1.8.1"
|
||||
@ -1135,6 +1184,17 @@ dependencies = [
|
||||
"syn 2.0.114",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "is-terminal"
|
||||
version = "0.4.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.14.0"
|
||||
@ -1361,6 +1421,12 @@ version = "1.21.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
|
||||
[[package]]
|
||||
name = "option-ext"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
|
||||
|
||||
[[package]]
|
||||
name = "ordered-float"
|
||||
version = "4.6.0"
|
||||
@ -1520,6 +1586,16 @@ dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pretty_env_logger"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c"
|
||||
dependencies = [
|
||||
"env_logger",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-crate"
|
||||
version = "3.4.0"
|
||||
@ -1662,6 +1738,17 @@ dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_users"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4e608c6638b9c18977b00b475ac1f28d14e84b27d8d42f70e0bf1e3dec127ac"
|
||||
dependencies = [
|
||||
"getrandom 0.2.17",
|
||||
"libredox",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.12.2"
|
||||
@ -2440,6 +2527,15 @@ version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "2.0.18"
|
||||
@ -2826,6 +2922,15 @@ dependencies = [
|
||||
"wasite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-core"
|
||||
version = "0.62.2"
|
||||
|
||||
@ -24,3 +24,6 @@ camino = { version = "1.2.2", features = [ "serde1" ] }
|
||||
toml = { version = "0.9.11", features = ["preserve_order"] }
|
||||
snafu = "0.8.9"
|
||||
xdg = "3.0.0"
|
||||
dirs = "6.0.0"
|
||||
pretty_env_logger = "0.5.0"
|
||||
log = "0.4.29"
|
||||
|
||||
@ -8,7 +8,7 @@ function getCookie(name) {
|
||||
|
||||
const DEFAULT_THEME = 'light'
|
||||
|
||||
window.addEventListener('load', (e) => {
|
||||
window.addEventListener('load', (_e) => {
|
||||
let theme = getCookie('theme');
|
||||
|
||||
if (theme) {
|
||||
|
||||
11
src/main.rs
11
src/main.rs
@ -1,4 +1,5 @@
|
||||
use bookforge::state::error::AppStateError;
|
||||
use snafu::ErrorCompat;
|
||||
use snafu::prelude::*;
|
||||
|
||||
use bookforge::build_app;
|
||||
@ -14,6 +15,8 @@ enum AppError {
|
||||
}
|
||||
|
||||
async fn main_inner() -> Result<(), AppError> {
|
||||
pretty_env_logger::init();
|
||||
|
||||
let app = build_app(AppState::new().await.context(StateSnafu)?);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:8000").await.unwrap();
|
||||
@ -25,10 +28,10 @@ async fn main_inner() -> Result<(), AppError> {
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() {
|
||||
if let Err(_errors) = main_inner().await {
|
||||
// for error in errors.iter_chain() {
|
||||
// println!("{}", error);
|
||||
// }
|
||||
if let Err(errors) = main_inner().await {
|
||||
for error in errors.iter_chain() {
|
||||
println!("{}", error);
|
||||
}
|
||||
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
@ -2,8 +2,8 @@ use snafu::prelude::*;
|
||||
use tokio::fs::read_to_string;
|
||||
|
||||
use camino::Utf8PathBuf;
|
||||
use dirs::config_dir;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use xdg::BaseDirectories;
|
||||
|
||||
#[derive(Snafu, Debug)]
|
||||
pub enum ConfigError {
|
||||
@ -17,6 +17,13 @@ pub enum ConfigError {
|
||||
path: Utf8PathBuf,
|
||||
source: toml::de::Error,
|
||||
},
|
||||
#[snafu(display("Failed parse config : {path}"))]
|
||||
IO {
|
||||
path: Utf8PathBuf,
|
||||
source: std::io::Error,
|
||||
},
|
||||
#[snafu(display("Config is empty: {path}"))]
|
||||
ConfigEmpty { path: Utf8PathBuf },
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize, Debug)]
|
||||
@ -25,11 +32,42 @@ pub struct AppConfig {
|
||||
pub database_path: Utf8PathBuf,
|
||||
}
|
||||
|
||||
impl Default for AppConfig {
|
||||
fn default() -> Self {
|
||||
AppConfig {
|
||||
database_path: Self::default_sqlite_path(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AppConfig {
|
||||
pub async fn new() -> Result<Self, ConfigError> {
|
||||
// TODO: Remove this
|
||||
let path = Utf8PathBuf::from("/home/torrpenn/projects/Gabatxo1312/bookforge/config.toml");
|
||||
let path = Self::config_file_path();
|
||||
|
||||
let file_exist = tokio::fs::try_exists(&path)
|
||||
.await
|
||||
.context(IOSnafu { path: path.clone() })?;
|
||||
|
||||
if file_exist {
|
||||
Self::parse(path).await
|
||||
} else {
|
||||
// Create parent and create an empty config file
|
||||
let parent_path = path.parent().unwrap();
|
||||
tokio::fs::create_dir_all(&parent_path)
|
||||
.await
|
||||
.context(IOSnafu { path: parent_path })?;
|
||||
tokio::fs::write(
|
||||
path.clone(),
|
||||
toml::to_string(&AppConfig::default()).unwrap(),
|
||||
)
|
||||
.await
|
||||
.context(IOSnafu { path: path.clone() })?;
|
||||
|
||||
Ok(AppConfig::default())
|
||||
}
|
||||
}
|
||||
|
||||
async fn parse(path: Utf8PathBuf) -> Result<Self, ConfigError> {
|
||||
let content = read_to_string(&path).await.context(FailedReadConfigSnafu {
|
||||
path: path.to_path_buf(),
|
||||
})?;
|
||||
@ -39,13 +77,18 @@ impl AppConfig {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn xdg_base_directories() -> BaseDirectories {
|
||||
BaseDirectories::with_prefix("bookforge")
|
||||
fn config_path() -> Utf8PathBuf {
|
||||
let mut config_dir = Utf8PathBuf::from_path_buf(config_dir().unwrap()).unwrap();
|
||||
config_dir.push("bookforge");
|
||||
|
||||
return config_dir;
|
||||
}
|
||||
|
||||
fn config_file_path() -> Utf8PathBuf {
|
||||
Self::config_path().join("BookForge.toml")
|
||||
}
|
||||
|
||||
pub fn default_sqlite_path() -> Utf8PathBuf {
|
||||
let config_dir = Self::xdg_base_directories().get_config_home().unwrap();
|
||||
|
||||
Utf8PathBuf::from_path_buf(config_dir).unwrap()
|
||||
Self::config_path().join("db.sqlite")
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,4 +29,4 @@
|
||||
<script src="/assets/js/script.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user