Skip to content

Commit

Permalink
Merge pull request #4970 from nymtech/feat/nym-data-observatory-docker
Browse files Browse the repository at this point in the history
feat: add clap and env vars through clap, add Dockerfile
  • Loading branch information
farbanas authored Oct 16, 2024
2 parents bccbc99 + 5d445d6 commit b88bc18
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 53 deletions.
1 change: 1 addition & 0 deletions nym-data-observatory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ readme.workspace = true
anyhow = { workspace = true }
axum = { workspace = true, features = ["tokio"] }
chrono = { workspace = true }
clap = { workspace = true, features = ["derive", "env"] }
nym-bin-common = { path = "../common/bin-common" }
nym-network-defaults = { path = "../common/network-defaults" }
nym-task = { path = "../common/task" }
Expand Down
19 changes: 19 additions & 0 deletions nym-data-observatory/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM rust:latest AS builder

COPY ./ /usr/src/nym
WORKDIR /usr/src/nym/nym-data-observatory

RUN cargo build --release

FROM ubuntu:24.04

RUN echo "Acquire::http::Pipeline-Depth 0;" > /etc/apt/apt.conf.d/99custom && \
echo "Acquire::http::No-Cache true;" >> /etc/apt/apt.conf.d/99custom && \
echo "Acquire::BrokenProxy true;" >> /etc/apt/apt.conf.d/99custom

RUN apt update && apt install -yy curl

WORKDIR /nym

COPY --from=builder /usr/src/nym/target/release/nym-data-observatory ./
ENTRYPOINT [ "/nym/nym-data-observatory" ]
16 changes: 14 additions & 2 deletions nym-data-observatory/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ services:
postgres:
image: postgres:13
container_name: nym-data-observatory-pg
env_file:
- .env
environment:
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data

data-observatory:
depends_on:
- postgres
image: nym-data-observatory:latest
build:
context: ../
dockerfile: nym-data-observatory/Dockerfile
container_name: nym-data-observatory
environment:
NYM_DATA_OBSERVATORY_CONNECTION_URL: "postgres://postgres:password@postgres:5432"
NYM_DATA_OBSERVATORY_HTTP_PORT: 8000

volumes:
pgdata:
7 changes: 3 additions & 4 deletions nym-data-observatory/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use std::str::FromStr;
pub(crate) mod models;
pub(crate) mod queries;

pub(crate) const DATABASE_URL_ENV_VAR: &str = "DATABASE_URL";

static MIGRATOR: Migrator = sqlx::migrate!("./migrations");

pub(crate) type DbPool = PgPool;
Expand All @@ -16,8 +14,9 @@ pub(crate) struct Storage {
}

impl Storage {
pub async fn init() -> Result<Self> {
let connection_url = std::env::var(DATABASE_URL_ENV_VAR).map_err(anyhow::Error::from)?;
pub async fn init(connection_url: Option<String>) -> Result<Self> {
let connection_url =
connection_url.ok_or_else(|| anyhow!("Missing the connection url for database!"))?;
let connect_options = {
let mut connect_options = PgConnectOptions::from_str(&connection_url)?;
let connect_options = connect_options.disable_statement_logging();
Expand Down
23 changes: 0 additions & 23 deletions nym-data-observatory/src/http/config.rs

This file was deleted.

1 change: 0 additions & 1 deletion nym-data-observatory/src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub(crate) mod api;
pub(crate) mod api_docs;
pub(crate) mod config;
pub(crate) mod error;
pub(crate) mod server;
pub(crate) mod state;
47 changes: 24 additions & 23 deletions nym-data-observatory/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
use clap::Parser;
use nym_network_defaults::setup_env;
use nym_task::signal::wait_for_signal;

use crate::http::config;

mod background_task;
mod db;
mod http;
mod logging;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Port to listen on
#[arg(long, default_value_t = 8000, env = "NYM_DATA_OBSERVATORY_HTTP_PORT")]
http_port: u16,

/// Path to the environment variables file. If you don't provide one, variables for the mainnet will be used.
#[arg(short, long, default_value = None, env = "NYM_DATA_OBSERVATORY_ENV_FILE")]
env_file: Option<String>,

/// DB connection url
#[arg(short, long, default_value = None, env = "NYM_DATA_OBSERVATORY_CONNECTION_URL")]
connection_url: Option<String>,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
logging::setup_tracing_logger();

// if dotenv file is present, load its values
// otherwise, default to mainnet
setup_env(Some(".env"));
let args = Args::parse();

let conf = config::Config::from_env();
tracing::debug!("Using config:\n{:?}", conf);
setup_env(args.env_file); // Defaults to mainnet if empty

let storage = db::Storage::init().await?;
let storage = db::Storage::init(args.connection_url).await?;
let db_pool = storage.pool_owned().await;
tokio::spawn(async move {
background_task::spawn_in_background(db_pool).await;
tracing::info!("Started task");
});

let shutdown_handles =
http::server::start_http_api(storage.pool_owned().await, conf.http_port())
.await
.expect("Failed to start server");
tracing::info!("Started HTTP server on port {}", conf.http_port());
let shutdown_handles = http::server::start_http_api(storage.pool_owned().await, args.http_port)
.await
.expect("Failed to start server");
tracing::info!("Started HTTP server on port {}", args.http_port);

wait_for_signal().await;

Expand All @@ -40,13 +51,3 @@ async fn main() -> anyhow::Result<()> {

Ok(())
}

// TODO dz move this to common
fn read_env_var(env_var: &str) -> anyhow::Result<String> {
std::env::var(env_var)
.map_err(|_| anyhow::anyhow!("You need to set {}", env_var))
.map(|value| {
tracing::trace!("{}={}", env_var, value);
value
})
}

0 comments on commit b88bc18

Please sign in to comment.