Skip to content

Commit

Permalink
Merge pull request #72 from pka/base-dir
Browse files Browse the repository at this point in the history
Use config file path as base directory
  • Loading branch information
pka committed Aug 9, 2024
2 parents d31daf4 + b0aebec commit e6adcf6
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 80 deletions.
18 changes: 11 additions & 7 deletions bbox-asset-server/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::runtime_templates::RuntimeTemplates;
use crate::service::{AssetService, PluginIndex};
use actix_files::{Files, NamedFile};
use actix_web::{web, HttpRequest, HttpResponse, Result};
use bbox_core::app_dir;
use bbox_core::config::app_dir;
use bbox_core::endpoints::{abs_req_baseurl, req_parent_path};
use bbox_core::service::ServiceEndpoints;
use log::{info, warn};
Expand Down Expand Up @@ -55,20 +55,21 @@ impl ServiceEndpoints for AssetService {

for static_dir in &service_cfg.static_ {
let dir = app_dir(&static_dir.dir);
if Path::new(&dir).is_dir() {
if dir.is_dir() {
info!(
"Serving static files from directory '{dir}' on '{}'",
"Serving static files from directory '{}' on '{}'",
dir.display(),
&static_dir.path
);
cfg.service(Files::new(&static_dir.path, &dir));
} else {
warn!("Static file directory '{dir}' not found");
warn!("Static file directory '{}' not found", dir.display(),);
}
}

let mut template_envs = RuntimeTemplates::default();
for template_dir in &service_cfg.template {
let dir = app_dir(&template_dir.dir);
let dir = app_dir(&template_dir.dir).to_string_lossy().to_string();
if Path::new(&dir).is_dir() {
let dest = &template_dir.path;
info!("Serving template files from directory '{dir}' on '{dest}'");
Expand All @@ -87,9 +88,12 @@ impl ServiceEndpoints for AssetService {

for repo in &service_cfg.repo {
let dir = app_dir(&repo.dir);
if Path::new(&dir).is_dir() {
if dir.is_dir() {
let xmldir = format!("{}/plugins.xml", repo.path);
info!("Serving QGIS plugin repository from directory '{dir}' on '{xmldir}'");
info!(
"Serving QGIS plugin repository from directory '{}' on '{xmldir}'",
dir.display()
);
cfg.service(Files::new(
&format!("{}/static", repo.path),
app_dir("bbox-asset-server/src/static"), // TODO: RustEmbed !
Expand Down
5 changes: 2 additions & 3 deletions bbox-asset-server/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::config::AssetServiceCfg;
use crate::qgis_plugins::plugin_files;
use async_trait::async_trait;
use bbox_core::app_dir;
use bbox_core::cli::{NoArgs, NoCommands};
use bbox_core::config::CoreServiceCfg;
use bbox_core::config::{app_dir, CoreServiceCfg};
use bbox_core::metrics::{no_metrics, NoMetrics};
use bbox_core::service::OgcApiService;
use log::{info, warn};
Expand All @@ -27,7 +26,7 @@ impl OgcApiService for AssetService {
async fn create(service_cfg: &Self::Config, _core_cfg: &CoreServiceCfg) -> Self {
let mut plugins_index = PluginIndex::new();
for repo in &service_cfg.repo {
let dir = app_dir(&repo.dir);
let dir = app_dir(&repo.dir).to_string_lossy().to_string();
if Path::new(&dir).is_dir() {
info!("Scanning QGIS plugin repository directory '{dir}'");
let plugins = plugin_files(&dir);
Expand Down
51 changes: 46 additions & 5 deletions bbox-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,50 @@ pub fn app_config() -> &'static Figment {
env::var("BBOX_CONFIG").unwrap_or("bbox.toml".to_string()),
))
.merge(Env::prefixed("BBOX_").split("__"));
if let Some(meta) = config.metadata().next() {
if let Some(source) = &meta.source {
info!("Reading configuration from `{source}`");
}
if let Some(source) = config_source(&config) {
// Logger is not initialized yet
println!("Reading configuration from `{source}`");
info!("Reading configuration from `{source}`");
}
config
})
}

fn config_source(config: &Figment) -> &Option<figment::Source> {
if let Some(meta) = config.metadata().next() {
&meta.source
} else {
&None
}
}

/// Base directory for files referenced in configuration
pub fn base_dir() -> PathBuf {
let config = app_config();
if let Some(source) = config_source(config)
.as_ref()
.and_then(|source| source.file_path())
{
source
.parent()
.expect("absolute config file path")
.canonicalize()
.expect("absolute config file path")
} else {
env::current_dir().expect("current working dir")
}
}

/// Full path relative to application base directory
pub fn app_dir(path: impl Into<PathBuf>) -> PathBuf {
let path = path.into();
if path.is_relative() {
base_dir().join(path)
} else {
path
}
}

#[derive(thiserror::Error, Debug)]
pub enum ConfigError {
#[error("Configuration error")]
Expand Down Expand Up @@ -135,7 +170,7 @@ impl ServiceConfig for CoreServiceCfg {

impl CoreServiceCfg {
pub fn loglevel(&self) -> Option<Loglevel> {
self.webserver.clone().and_then(|cfg| cfg.loglevel)
self.webserver.as_ref().and_then(|cfg| cfg.loglevel.clone())
}
}

Expand Down Expand Up @@ -253,6 +288,12 @@ pub struct DsGpkgCfg {
// pub pool_max_connections(8)
}

impl DsGpkgCfg {
pub fn abs_path(&self) -> PathBuf {
app_dir(&self.path)
}
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct WmsHttpSourceProviderCfg {
Expand Down
22 changes: 0 additions & 22 deletions bbox-core/src/dir.rs

This file was deleted.

2 changes: 0 additions & 2 deletions bbox-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod api;
pub mod auth;
pub mod cli;
pub mod config;
mod dir;
pub mod endpoints;
pub mod file_search;
mod formats;
Expand All @@ -18,7 +17,6 @@ pub mod templates;
mod tile_response;
pub mod tls;

pub use dir::*;
pub use formats::*;
pub use service_utils::*;
pub use tile_response::*;
3 changes: 1 addition & 2 deletions bbox-core/src/tls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::app_dir;
use crate::config::{config_error_exit, error_exit};
use crate::config::{app_dir, config_error_exit, error_exit};
use rustls::{Certificate, PrivateKey, ServerConfig};
use rustls_pemfile::{certs, pkcs8_private_keys};
use std::{fs::File, io::BufReader};
Expand Down
4 changes: 2 additions & 2 deletions bbox-feature-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ url = "postgresql://t_rex:t_rex@127.0.0.1:5439/t_rex_tests"
[[datasource]]
name = "ne_extracts"
[datasource.gpkg]
path = "../assets/ne_extracts.gpkg"
path = "assets/ne_extracts.gpkg"
```

Collections with auto detection:
Expand All @@ -31,7 +31,7 @@ Collections with auto detection:
url = "postgresql://t_rex:t_rex@127.0.0.1:5439/t_rex_tests"

[[collections.directory]]
dir = "../assets"
dir = "assets"
```

Collections:
Expand Down
4 changes: 3 additions & 1 deletion bbox-feature-server/src/datasource/gpkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ pub struct SqliteDatasource {

impl SqliteDatasource {
pub async fn from_config(cfg: &DsGpkgCfg) -> Result<Self> {
Self::new_pool(cfg.path.as_os_str().to_str().unwrap()).await
let path = cfg.abs_path().to_string_lossy().to_string();
info!("Opening `{path}`");
Self::new_pool(&path).await
}
pub async fn new_pool(gpkg: &str) -> Result<Self> {
let conn_options = SqliteConnectOptions::new().filename(gpkg).read_only(true);
Expand Down
10 changes: 5 additions & 5 deletions bbox-map-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ Map server settings:
QGIS Server settings:
```toml
[mapserver.qgis_backend]
project_basedir = "../assets" # Base dir for project files (.qgs, .qgz)
qgs.path = "/qgis" # WMS URL base path
qgz.path = "/qgz" # WMS URL base path
project_basedir = "assets" # Base dir for project files (.qgs, .qgz)
qgs.path = "/qgis" # WMS URL base path
qgz.path = "/qgz" # WMS URL base path
```

UMN MapServer settings:
```toml
[mapserver.umn_backend]
project_basedir = "../assets" # Base dir for project files (.map)
path = "/wms/map" # WMS URL base path
project_basedir = "assets" # Base dir for project files (.map)
path = "/wms/map" # WMS URL base path
```

## Usage
Expand Down
12 changes: 8 additions & 4 deletions bbox-map-server/src/wms_fcgi_backend.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::config::*;
use crate::fcgi_process::FcgiProcessPool;
use crate::inventory::*;
use bbox_core::config::Loglevel;
use bbox_core::{app_dir, file_search};
use bbox_core::config::{app_dir, Loglevel};
use bbox_core::file_search;
use log::{info, warn};
use std::collections::{HashMap, HashSet};
use std::env;
Expand Down Expand Up @@ -36,7 +36,9 @@ pub struct QgisFcgiBackend {

impl QgisFcgiBackend {
pub(crate) fn new(config: QgisBackendCfg) -> Self {
let plugindir = app_dir("bbox-map-server/qgis/plugins");
let plugindir = app_dir("bbox-map-server/qgis/plugins")
.to_string_lossy()
.to_string();
QgisFcgiBackend { config, plugindir }
}
}
Expand Down Expand Up @@ -203,7 +205,9 @@ pub fn detect_backends(
for backend in backends {
if let Some(exe_path) = detect_fcgi(backend) {
let mut wms_inventory_files = HashMap::new();
let base = backend.project_basedir();
let base = &app_dir(backend.project_basedir())
.to_string_lossy()
.to_string();
let basedir = if config.search_projects {
info!("Searching project files with project_basedir: {base}");
let mut all_paths = HashSet::new();
Expand Down
2 changes: 1 addition & 1 deletion bbox-routing-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ GeoPackage line geometry table:
```toml
[[routing.service]]
profile = "railway"
gpkg = "../assets/railway-test.gpkg"
gpkg = "assets/railway-test.gpkg"
table = "flows"
geom = "geom"
```
Expand Down
26 changes: 25 additions & 1 deletion bbox-tile-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::config_t_rex as t_rex;
use crate::datasource::source_config_from_cli_arg;
use bbox_core::cli::CommonCommands;
use bbox_core::config::{
error_exit, from_config_root_or_exit, ConfigError, DatasourceCfg, DsPostgisCfg,
app_dir, error_exit, from_config_root_or_exit, ConfigError, DatasourceCfg, DsPostgisCfg,
NamedDatasourceCfg,
};
use bbox_core::service::ServiceConfig;
Expand Down Expand Up @@ -64,6 +64,12 @@ pub struct GridCfg {
pub json: String,
}

impl GridCfg {
pub fn abs_path(&self) -> PathBuf {
app_dir(&self.json)
}
}

/// Available tile grid with optional zoom levels
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
Expand Down Expand Up @@ -324,6 +330,12 @@ pub struct FileStoreCfg {
pub base_dir: PathBuf,
}

impl FileStoreCfg {
pub fn abs_path(&self) -> PathBuf {
app_dir(&self.base_dir)
}
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct S3StoreCfg {
Expand All @@ -339,12 +351,24 @@ pub struct MbtilesStoreCfg {
pub path: PathBuf,
}

impl MbtilesStoreCfg {
pub fn abs_path(&self) -> PathBuf {
app_dir(&self.path)
}
}

#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct PmtilesStoreCfg {
pub path: PathBuf,
}

impl PmtilesStoreCfg {
pub fn abs_path(&self) -> PathBuf {
app_dir(&self.path)
}
}

impl TileStoreCfg {
pub fn from_cli_args(cli: &ArgMatches) -> Option<Self> {
let Ok(Commands::Seed(args)) = Commands::from_arg_matches(cli) else {
Expand Down
2 changes: 1 addition & 1 deletion bbox-tile-server/src/mbtiles_ds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn mbtiles_from_path<P: AsRef<Path>>(filepath: P) -> Result<Mbtiles> {

impl MbtilesDatasource {
pub async fn from_config(ds: &MbtilesStoreCfg, metadata: Option<Metadata>) -> Result<Self> {
Self::new_pool(mbtiles_from_path(&ds.path)?, metadata).await
Self::new_pool(mbtiles_from_path(ds.abs_path())?, metadata).await
}

pub async fn new_pool(mbtiles: Mbtiles, metadata: Option<Metadata>) -> Result<Self> {
Expand Down
3 changes: 2 additions & 1 deletion bbox-tile-server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ impl OgcApiService for TileService {
// Register custom grids
let mut grids = tms().clone();
for grid in &config.grids {
let custom = TileMatrixSet::from_json_file(&grid.json).unwrap_or_else(error_exit);
let custom = TileMatrixSet::from_json_file(&grid.abs_path().to_string_lossy())
.unwrap_or_else(error_exit);
grids
.register(vec![custom], true)
.unwrap_or_else(error_exit);
Expand Down
3 changes: 1 addition & 2 deletions bbox-tile-server/src/store/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ impl FileStore {
tileset_name: &str,
format: &Format,
) -> Self {
let base_dir =
PathBuf::from_iter([cfg.base_dir.clone(), PathBuf::from(tileset_name)].iter());
let base_dir = cfg.abs_path().join(PathBuf::from(tileset_name));
let compression = compression.clone().unwrap_or(StoreCompressionCfg::None);
Self::new(base_dir, compression, *format)
}
Expand Down
4 changes: 2 additions & 2 deletions bbox-tile-server/src/store/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct MbtilesStore {

impl MbtilesStore {
pub async fn from_config(cfg: &MbtilesStoreCfg) -> Result<Self, MbtilesDsError> {
info!("Creating connection pool for {}", &cfg.path.display());
info!("Creating connection pool for {}", &cfg.abs_path().display());
let mbt = MbtilesDatasource::from_config(cfg, None).await?;
//let opt = SqliteConnectOptions::new().filename(file).read_only(true);
Ok(MbtilesStore { mbt })
Expand All @@ -27,7 +27,7 @@ impl MbtilesStore {
cfg: &MbtilesStoreCfg,
metadata: Metadata,
) -> Result<Self, MbtilesDsError> {
info!("Creating connection pool for {}", &cfg.path.display());
info!("Creating connection pool for {}", &cfg.abs_path().display());
let mbt = MbtilesDatasource::from_config(cfg, Some(metadata)).await?;
Ok(MbtilesStore { mbt })
}
Expand Down
Loading

0 comments on commit e6adcf6

Please sign in to comment.