diff --git a/examples/official-site/your-first-sql-website/first-sql-website-launch.png b/examples/official-site/your-first-sql-website/first-sql-website-launch.png index 0d8546e4..acf93e5e 100644 Binary files a/examples/official-site/your-first-sql-website/first-sql-website-launch.png and b/examples/official-site/your-first-sql-website/first-sql-website-launch.png differ diff --git a/examples/official-site/your-first-sql-website/index.sql b/examples/official-site/your-first-sql-website/index.sql index d36d815b..7d88eeaf 100644 --- a/examples/official-site/your-first-sql-website/index.sql +++ b/examples/official-site/your-first-sql-website/index.sql @@ -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`. diff --git a/sqlpage/sqlpage.db b/sqlpage/sqlpage.db new file mode 100644 index 00000000..25af6df8 Binary files /dev/null and b/sqlpage/sqlpage.db differ diff --git a/sqlpage/sqlpage.json b/sqlpage/sqlpage.json index 3f0f0b0b..550df0e9 100644 --- a/sqlpage/sqlpage.json +++ b/sqlpage/sqlpage.json @@ -1,4 +1,4 @@ { - "database_url": "sqlite://./sqlpage.db?mode=rwc", + "database_url": "sqlite://./sqlpage/sqlpage.db?mode=rwc", "listen_on": "localhost:8080" } \ No newline at end of file diff --git a/src/app_config.rs b/src/app_config.rs index 34e0b8a2..83930376 100644 --- a/src/app_config.rs +++ b/src/app_config.rs @@ -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"; + } } }