Skip to content

Commit

Permalink
Use absolute config paths
Browse files Browse the repository at this point in the history
  • Loading branch information
pka committed Aug 8, 2024
1 parent 5ab0227 commit b081bd7
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 30 deletions.
16 changes: 10 additions & 6 deletions bbox-asset-server/src/endpoints.rs
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion bbox-asset-server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,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
17 changes: 14 additions & 3 deletions bbox-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ pub fn base_dir() -> PathBuf {
}
}

/// Directory relative to application base directory
pub fn app_dir(subdir: &str) -> String {
format!("{}/{subdir}", &base_dir().to_string_lossy())
/// 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)]
Expand Down Expand Up @@ -283,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
2 changes: 1 addition & 1 deletion bbox-feature-server/src/datasource/gpkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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
Self::new_pool(cfg.abs_path().as_os_str().to_str().unwrap()).await
}
pub async fn new_pool(gpkg: &str) -> Result<Self> {
let conn_options = SqliteConnectOptions::new().filename(gpkg).read_only(true);
Expand Down
12 changes: 6 additions & 6 deletions bbox-map-server/src/wms_fcgi_backend.rs
Original file line number Diff line number Diff line change
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,11 +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 = if Path::new(backend.project_basedir()).is_relative() {
&app_dir(backend.project_basedir())
} else {
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
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
5 changes: 4 additions & 1 deletion bbox-tile-server/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ pub async fn store_reader_from_config(
Box::new(reader)
} else {
// We continue, because for seeding into a new file, the reader cannot be created and is not needed
warn!("Couldn't open PmtilesStoreReader {}", cfg.path.display());
warn!(
"Couldn't open PmtilesStoreReader {}",
cfg.abs_path().display()
);
Box::new(NoStore)
}
}
Expand Down
4 changes: 2 additions & 2 deletions bbox-tile-server/src/store/pmtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl PmtilesStoreReader {
Ok(Self { path, reader })
}
pub async fn from_config(cfg: &PmtilesStoreCfg) -> Result<Self, TileStoreError> {
Self::create_reader(cfg.path.clone()).await
Self::create_reader(cfg.abs_path()).await
}
pub fn config_from_cli_arg(file_or_url: &str) -> Option<PmtilesStoreCfg> {
match Path::new(file_or_url).extension().and_then(OsStr::to_str) {
Expand Down Expand Up @@ -172,6 +172,6 @@ impl PmtilesStoreWriter {
}
}
pub fn from_config(cfg: &PmtilesStoreCfg, metadata: Metadata, format: &Format) -> Self {
Self::new(cfg.path.clone(), metadata, format)
Self::new(cfg.abs_path(), metadata, format)
}
}
6 changes: 3 additions & 3 deletions bbox.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ fid_field = "fid"
# Static file serving
# Env var example: BBOX_ASSETS__STATIC='[{dir="assets",path="/assets"}]'
# ./assets/* -> http://localhost:8080/assets/
dir = "./assets"
dir = "assets"
path = "/assets"

[[assets.template]]
# Template file serving
# Env var example: BBOX_ASSETS__TEMPLATE='[{dir="templates",path="/html"}]'
# ./templates/name.html -> http://localhost:8080/html/name/param
dir = "./templates"
dir = "templates"
path = "/html"

[[assets.repo]]
# QGIS plugin repository
# Env var example: BBOX_ASSETS__REPO='[{dir="plugins",path="/qgisrepo"}]'
# ./plugins/*.zip -> http://localhost:8080/qgisrepo/plugins.xml
dir = "./plugins"
dir = "plugins"
path = "/qgisrepo"

[mapserver]
Expand Down

0 comments on commit b081bd7

Please sign in to comment.