Skip to content

Commit

Permalink
Updated default config example
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrasingh committed Aug 23, 2024
1 parent c82638e commit 7e2eed8
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 38 deletions.
2 changes: 1 addition & 1 deletion contrib/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Check the respective registry for available tags.

> For complete settings and configuration details, refer to the [Geoprox CLI README](../../geoprox/README.md#configuration).
To use a custom configuration file, simply mount your configuration file into the container at `/etc/geoprox`. Geoprox will automatically detect and parse configuration files in formats like `YAML`, `TOML`, `JSON`, or `INI` if they are named `geoprox.yaml`, `geoprox.toml`, `geoprox.json`, or `geoprox.ini`, respectively.
To use a custom configuration file, simply mount your configuration file into the container at `/var/lib/geoprox`. Geoprox will automatically detect and parse configuration files in formats like `YAML`, `TOML`, `JSON`, or `INI` if they are named `geoprox.yaml`, `geoprox.toml`, `geoprox.json`, or `geoprox.ini`, respectively.

For example, if you have a configuration file named `geoprox.yaml` in your current directory, you can run the container with the following command:

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
openapi.json
/snapshots
17 changes: 10 additions & 7 deletions contrib/docker/quickstart/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ services:
geoprox:
# I recommend pinning the tag to a specific version.
# Available tags can be found at https://hub.docker.com/repository/docker/ezrasingh/geoprox/tags
image: ezrasingh/geoprox:latest
image: ezrasingh/geoprox:${IMAGE_TAG:-latest}
restart: on-failure:3

# This is optional, demonstrating how to specify your config path.
# The default path is /etc/geoprox/
# This is optional, demonstrating how to specify env vars
# These are all the defaults
environment:
GEOPROX_CONFIG: /etc/geoprox/geoprox.toml
GEOPROX_CONFIG: /var/lib/geoprox/geoprox.toml
GEOPROX_HTTP_ADDR: 0.0.0.0
GEOPROX_HTTP_PORT: 5000

ports:
- 5000:5000
- ${HOST_PORT:-5000}:5000
volumes:
- ./geoprox.toml:/etc/geoprox/geoprox.toml:ro
- ./geoprox.toml:/var/lib/geoprox/geoprox.toml:ro
- ./snapshots/snapshot.bin:/var/lib/geoprox/snapshot.bin:z

# Here is an example to generate just the OpenAPI spec
geoprox-spec:
image: ezrasingh/geoprox:latest
image: ezrasingh/geoprox:${IMAGE_TAG:-latest}
entrypoint: ['geoprox', 'spec' ]
command: --destination /tmp --filename openapi.json --pretty
volumes:
Expand Down
16 changes: 14 additions & 2 deletions contrib/docker/quickstart/geoprox.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
http_addr = '0.0.0.0'
# The port the server will listen on
http_port = 5000
# Timeout duration in seconds
timeout = 10
# Request timeout
timeout = '10s'

[shard]
# Determines the default geohash length for inserts
insert_depth = 6
# Determines the default geohash length for searches
search_depth = 6
# Specifies the default number of results returned in range queries
default_count = 100
# Toggles the default sorting behavior for query results
default_sorted = false

[server.snapshots]
# Toggle snapshot usage
disabled = false
# Directory where snapshots will be stored
path = '/var/lib/geoprox'
# How often snapshots will be taken
every = '30s'
1 change: 1 addition & 0 deletions geoprox-server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added support for TTL and key-expiration ([#15](https://github.com/ezrasingh/geoprox/issues/15)).
- Added support for snapshots and persistence ([#17](https://github.com/ezrasingh/geoprox/issues/17)).
- Using [`duration-string`](https://crates.io/crates/duration-string) crate to handle `Duration` configuration

# 0.4.2

Expand Down
14 changes: 9 additions & 5 deletions geoprox-server/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ pub fn routes(app_state: AppState) -> Router {

// ? background tasks
purge_expirations(Arc::clone(&state), Duration::from_secs(1));
persist_snapshot(
Arc::clone(&state),
server_config.snapshot.every.unwrap_or_default().into(),
);

if !server_config.snapshots.disabled {
persist_snapshot(
Arc::clone(&state),
server_config.snapshots.every.unwrap_or_default().into(),
);
}

Router::new()
.nest(
Expand Down Expand Up @@ -164,7 +167,8 @@ mod test {
fn setup() -> TestServer {
let app = AppState::new(
ServerConfig {
snapshot: SnapshotConfig {
snapshots: SnapshotConfig {
disabled: false,
path: Some(current_dir().unwrap()),
every: None,
},
Expand Down
4 changes: 2 additions & 2 deletions geoprox-server/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct AppState {

impl AppState {
pub fn new(server_config: ServerConfig, shard_config: GeoShardConfig) -> Self {
match fs::read(server_config.snapshot.bin_path()) {
match fs::read(server_config.snapshots.bin_path()) {
Ok(state) => Self {
geoshard: bincode::deserialize(&state).expect("could not restore snapshot"),
server_config,
Expand All @@ -36,7 +36,7 @@ impl AppState {

pub fn store_snapshot(&self) {
fs::write(
self.server_config.snapshot.bin_path(),
self.server_config.snapshots.bin_path(),
bincode::serialize(&self.geoshard).unwrap(),
)
.expect("could not store snapshot");
Expand Down
12 changes: 8 additions & 4 deletions geoprox-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ use std::{
time::Duration,
};

pub const DEFAULT_CONFIG_PATH: &str = "/etc/geoprox";
pub const DEFAULT_CONFIG_PATH: &str = "/var/lib/geoprox";

#[derive(Clone, Debug, Deserialize)]
pub struct SnapshotConfig {
/// Directory where snapshots will be stored and read from (default /etc/geoprox/)
/// Toggles snapshot usage
#[serde(default)]
pub disabled: bool,
/// Directory where snapshots will be stored and read from (default /var/lib/geoprox)
pub path: Option<PathBuf>,
/// Determines how often snapshots will be taken (default 60s)
pub every: Option<DurationString>,
Expand All @@ -29,6 +32,7 @@ impl SnapshotConfig {
impl Default for SnapshotConfig {
fn default() -> Self {
Self {
disabled: false,
path: Some(PathBuf::from(DEFAULT_CONFIG_PATH)),
every: Some(DurationString::default()),
}
Expand All @@ -44,7 +48,7 @@ pub struct ServerConfig {
/// Timeout duration in seconds (default 10s)
pub timeout: Option<DurationString>,
/// Determines how snapshots will be handled
pub snapshot: SnapshotConfig,
pub snapshots: SnapshotConfig,
}

impl ServerConfig {
Expand All @@ -58,7 +62,7 @@ impl Default for ServerConfig {
http_addr: Some(Self::DEFAULT_ADDR),
http_port: Some(Self::DEFAULT_PORT),
timeout: Some(DurationString::new(Duration::from_secs(10))),
snapshot: SnapshotConfig::default(),
snapshots: SnapshotConfig::default(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions geoprox/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Improved default config behavior

## 0.4.2

- Added support for environment variables ([#6](https://github.com/ezrasingh/geoprox/issues/6)).
Expand Down
24 changes: 16 additions & 8 deletions geoprox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ geoprox help encode

## Configuration

Geoprox allows specifying a configuration file using the `-c` or `--config` option or set the `GEOPROX_CONFIG` environment variable. This file can contain various settings to customize the behavior of the Geoprox server and commands. The configuration can be provided in any common format such as `YAML`, `TOML`, `JSON`, or `INI`.
Geoprox allows specifying a configuration file using the `-c` or `--config` option or set the `GEOPROX_CONFIG` environment variable. This file can contain various settings to customize the behavior of the Geoprox server and commands. The configuration can be provided in any common format such as `YAML`, `TOML`, `JSON`, or `INI`. By default, configurations are expected to be found under `/var/lib/geoprox`.

### Example Config

Expand All @@ -78,8 +78,8 @@ Here's an example configuration file in `TOML` format:
http_addr = '0.0.0.0'
# The port the server will listen on
http_port = 5000
# Timeout duration in seconds
timeout = 10
# Request timeout
timeout = '10s'

[shard]
# Determines the default geohash length for inserts
Expand All @@ -90,17 +90,25 @@ search_depth = 6
default_count = 100
# Toggles the default sorting behavior for query results
default_sorted = false

[server.snapshots]
# Toggle snapshot usage
disabled = false
# Directory where snapshots will be stored
path = '/var/lib/geoprox'
# How often snapshots will be taken
every = '30s'
```

#### Environment Variables

These are the currently supported environment variables. They will take precedence over settings defined in the configuration file.

| Environment Variable | Description | Default Value |
| -------------------- | -------------------------------------- | --------------------------- |
| `GEOPROX_CONFIG` | Specifies the configuration file path. | `/etc/geoprox/geoprox.toml` |
| `GEOPROX_HTTP_ADDR` | The address the server will bind to. | `0.0.0.0` |
| `GEOPROX_HTTP_PORT` | The port the server will listen on. | `5000` |
| Environment Variable | Description | Default Value |
| -------------------- | -------------------------------------- | --------------------------------------------------------- |
| `GEOPROX_CONFIG` | Specifies the configuration file path. | `/var/lib/geoprox/geoprox.{toml,yaml,json,ini,ron,json5}` |
| `GEOPROX_HTTP_ADDR` | The address the server will bind to. | `0.0.0.0` |
| `GEOPROX_HTTP_PORT` | The port the server will listen on. | `5000` |

## Fine Tuning

Expand Down
19 changes: 12 additions & 7 deletions geoprox/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use clap::{ArgAction, Parser, Subcommand};
use config::{Config, ConfigError};
use geoprox_core::models::GeoShardConfig;
use geoprox_server::config::ServerConfig;
use geoprox_server::config::{ServerConfig, DEFAULT_CONFIG_PATH};
use serde::Deserialize;
use std::net::IpAddr;
use std::path::PathBuf;

#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
/// Specify a config file
/// Specify a config file (defaults /var/lib/geoprox/geoprox.{toml,yaml,json,ini,ron,json5})
#[arg(short, long, value_name = "CONFIG", env = "GEOPROX_CONFIG")]
config: Option<PathBuf>,

Expand All @@ -22,11 +22,11 @@ pub enum Commands {
/// Start Geoprox server
Run {
/// The address the server will bind to
#[arg(short, long, default_value = "0.0.0.0", env = "GEOPROX_HTTP_ADDR")]
#[arg(short, long, default_value_t = ServerConfig::DEFAULT_ADDR, env = "GEOPROX_HTTP_ADDR")]
addr: IpAddr,

/// The port the server will listen on
#[arg(short, long, default_value_t = 5000, env = "GEOPROX_HTTP_PORT")]
#[arg(short, long, default_value_t = ServerConfig::DEFAULT_PORT, env = "GEOPROX_HTTP_PORT")]
port: u16,
},

Expand Down Expand Up @@ -59,8 +59,8 @@ pub enum Commands {
destination: Option<PathBuf>,

/// Name of the output file (defaults to openapi.json)
#[arg(short, long)]
filename: Option<String>,
#[arg(short, long, default_value = "openapi.json")]
filename: String,

/// Output the API specification to standard output (stdout)
#[arg(short, long, action=ArgAction::SetTrue)]
Expand All @@ -87,7 +87,12 @@ pub fn runtime() -> Result<(Option<Commands>, GeoproxConfig), ConfigError> {
.build()?
.try_deserialize()?,
None => Config::builder()
.add_source(config::File::with_name("/etc/geoprox/geoprox"))
.add_source(config::File::with_name(&format!(
// ? look for geoprox.{toml,yaml,json,ini,ron,json5}
// ? config under default config path
"{}/geoprox",
DEFAULT_CONFIG_PATH
)))
.build()
.unwrap_or_default()
.try_deserialize()
Expand Down
3 changes: 1 addition & 2 deletions geoprox/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ fn main() {
.as_deref() // Convert &Option<PathBuf> to Option<&Path>
.unwrap_or(&cwd);

let file_name = filename.as_deref().unwrap_or("openapi.json");
dir.join(file_name)
dir.join(filename)
};
// ? create the file and write the spec JSON to it
match std::fs::File::create(&file_path) {
Expand Down

0 comments on commit 7e2eed8

Please sign in to comment.