Skip to content

Commit 3745511

Browse files
committed
new configuration parameter: web_root
see #87
1 parent ce75ba0 commit 3745511

File tree

6 files changed

+53
-20
lines changed

6 files changed

+53
-20
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
- Better support for connection options in mssql.
55
- New icons (see https://tabler-icons.io/changelog)
66
- New version of the CSS library (see https://preview.tabler.io/changelog.html)
7+
- configurable web root (see [configuration.md](./configuration.md))
8+
- new welcome message
9+
- ```
10+
SQLPage is now running on http://127.0.0.1:8080/
11+
You can write your code in .sql files in /path/to/your/website/directory.
12+
```
713
814
## 0.10.3 (2023-09-14)
915

configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ on a [JSON](https://en.wikipedia.org/wiki/JSON) file placed in `sqlpage/sqlpage.
1414
| `database_connection_retries` | 6 | Database connection attempts before giving up. Retries will happen every 5 seconds. |
1515
| `database_connection_acquire_timeout_seconds` | 10 | How long to wait when acquiring a database connection from the pool before giving up and returning an error. |
1616
| `sqlite_extensions` | | An array of SQLite extensions to load, such as `mod_spatialite` |
17+
| `web_root` | `.` | The root directory of the web server, where the `index.sql` file is located. |
1718

1819
You can find an example configuration file in [`sqlpage/sqlpage.json`](./sqlpage/sqlpage.json).
1920

src/app_config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use config::Config;
33
use serde::de::Error;
44
use serde::{Deserialize, Deserializer};
55
use std::net::{SocketAddr, ToSocketAddrs};
6+
use std::path::PathBuf;
67

78
#[cfg(not(feature = "lambda-web"))]
89
const DEFAULT_DATABASE_FILE: &str = "sqlpage.db";
@@ -32,6 +33,9 @@ pub struct AppConfig {
3233
/// pool. The default is 10 seconds.
3334
#[serde(default = "default_database_connection_acquire_timeout_seconds")]
3435
pub database_connection_acquire_timeout_seconds: f64,
36+
37+
#[serde(default = "default_web_root")]
38+
pub web_root: PathBuf,
3539
}
3640

3741
pub fn load() -> anyhow::Result<AppConfig> {
@@ -102,6 +106,13 @@ fn default_database_connection_acquire_timeout_seconds() -> f64 {
102106
10.
103107
}
104108

109+
fn default_web_root() -> PathBuf {
110+
std::env::current_dir().unwrap_or_else(|e| {
111+
log::error!("Unable to get current directory: {}", e);
112+
PathBuf::from(&std::path::Component::CurDir)
113+
})
114+
}
115+
105116
#[cfg(test)]
106117
pub mod tests {
107118
use super::AppConfig;

src/lib.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub mod webserver;
1414
use crate::app_config::AppConfig;
1515
use crate::filesystem::FileSystem;
1616
use crate::webserver::database::{FileCache, ParsedSqlFile};
17-
use std::env;
1817
use std::net::SocketAddr;
1918
use std::path::PathBuf;
2019
use templates::AllTemplates;
@@ -35,9 +34,8 @@ impl AppState {
3534
// Connect to the database
3635
let db = Database::init(config).await?;
3736
let all_templates = AllTemplates::init()?;
38-
let web_root = get_web_root();
3937
let mut sql_file_cache = FileCache::new();
40-
let file_system = FileSystem::init(&web_root, &db).await;
38+
let file_system = FileSystem::init(&config.web_root, &db).await;
4139
sql_file_cache.add_static(
4240
PathBuf::from("index.sql"),
4341
ParsedSqlFile::new(&db, include_str!("../index.sql")).await,
@@ -51,13 +49,6 @@ impl AppState {
5149
}
5250
}
5351

54-
pub fn get_web_root() -> PathBuf {
55-
env::var("WEB_ROOT").map_or_else(
56-
|_| PathBuf::from(&std::path::Component::CurDir),
57-
PathBuf::from,
58-
)
59-
}
60-
6152
pub struct Config {
6253
pub listen_on: SocketAddr,
6354
}

src/main.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use sqlpage::{app_config, webserver, AppState, Config};
1+
use sqlpage::{
2+
app_config::{self, AppConfig},
3+
webserver, AppState, Config,
4+
};
25

36
#[actix_web::main]
47
async fn main() {
@@ -10,15 +13,36 @@ async fn main() {
1013
}
1114

1215
async fn start() -> anyhow::Result<()> {
13-
let config = app_config::load()?;
14-
log::debug!("Starting with the following configuration: {config:?}");
15-
let state = AppState::init(&config).await?;
16-
webserver::apply_migrations(&state.db).await?;
17-
let listen_on = config.listen_on;
16+
let app_config = app_config::load()?;
17+
log::debug!("Starting with the following configuration: {app_config:?}");
18+
let state = AppState::init(&app_config).await?;
19+
webserver::apply_migrations(&state.db, &app_config.web_root).await?;
20+
let listen_on = app_config.listen_on;
1821
log::info!("Starting server on {}", listen_on);
1922
let config = Config { listen_on };
20-
webserver::http::run_server(config, state).await?;
21-
Ok(())
23+
let (r, _) = tokio::join!(
24+
webserver::http::run_server(config, state),
25+
log_welcome_message(&app_config)
26+
);
27+
r
28+
}
29+
30+
async fn log_welcome_message(config: &AppConfig) {
31+
// Don't show 0.0.0.0 as the host, show the actual IP address
32+
let http_addr = config.listen_on.to_string().replace(
33+
"0.0.0.0",
34+
std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)
35+
.to_string()
36+
.as_str(),
37+
);
38+
39+
log::info!(
40+
"Server started successfully.
41+
SQLPage is now running on http://{}/
42+
You can write your website's code in .sql files in {}.",
43+
http_addr,
44+
config.web_root.display()
45+
);
2246
}
2347

2448
fn init_logging() {

src/webserver/database/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ impl Database {
4747
}
4848
}
4949

50-
pub async fn apply_migrations(db: &Database) -> anyhow::Result<()> {
51-
let migrations_dir = Path::new(MIGRATIONS_DIR);
50+
pub async fn apply_migrations(db: &Database, web_root: &Path) -> anyhow::Result<()> {
51+
let migrations_dir = web_root.join(MIGRATIONS_DIR);
5252
if !migrations_dir.exists() {
5353
log::info!(
5454
"Not applying database migrations because '{}' does not exist",

0 commit comments

Comments
 (0)