create the config dir if it does not exist

This commit is contained in:
lovasoa
2023-10-03 12:04:33 +02:00
parent 87494ca4ff
commit 9ea59120fb
5 changed files with 12 additions and 7 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 102 KiB

After

Width:  |  Height:  |  Size: 105 KiB

@@ -103,7 +103,8 @@ CREATE TABLE users ( id INTEGER PRIMARY KEY, name TEXT NOT NULL );
Connect to a custom database
============================
By default, SQLPage uses a [SQLite](https://www.sqlite.org/about.html) database stored in a file named `sqlpage.db` in your website''s root folder.
By default, SQLPage uses a [SQLite](https://www.sqlite.org/about.html) database stored in a file named `sqlpage.db`
in the `sqlpage` configuration folder.
You can change this by creating a file named `sqlpage.json` in a folder called `sqlpage`.
So, if your website''s root folder is `/mysite`, you should create a file at `/mysite/sqlpage/sqlpage.json`.
Binary file not shown.
+1 -1
View File
@@ -1,4 +1,4 @@
{
"database_url": "sqlite://./sqlpage.db?mode=rwc",
"database_url": "sqlite://./sqlpage/sqlpage.db?mode=rwc",
"listen_on": "localhost:8080"
}
+9 -5
View File
@@ -100,11 +100,15 @@ fn default_database_url() -> String {
} else if let Ok(true) = default_db_path.try_exists() {
log::debug!("Using the default datbase file in {default_db_path:?}.");
return prefix + default_db_path.to_str().unwrap();
} else if let Ok(tmp_file) = std::fs::File::create(&default_db_path) {
log::info!("No DATABASE_URL provided, {default_db_path:?} is writable, creating a new database file.");
drop(tmp_file);
std::fs::remove_file(&default_db_path).expect("removing temp file");
return prefix + default_db_path.to_str().unwrap() + "?mode=rwc";
} else {
// Create the default database file if we can
let _ = std::fs::create_dir_all(&default_db_path.parent().unwrap()); // may already exist
if let Ok(tmp_file) = std::fs::File::create(&default_db_path) {
log::info!("No DATABASE_URL provided, {default_db_path:?} is writable, creating a new database file.");
drop(tmp_file);
std::fs::remove_file(&default_db_path).expect("removing temp file");
return prefix + default_db_path.to_str().unwrap() + "?mode=rwc";
}
}
}