Skip to content

Commit

Permalink
feat: fixing config setup
Browse files Browse the repository at this point in the history
we need to untangle ports
  • Loading branch information
djavorek committed Oct 13, 2023
1 parent cc44c6f commit dfd4618
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG client_url=http://localhost:3000
ARG client_url=http://localhost:4000
ARG backend_url=http://veryrezsi_server:8000/api

# Stage BUILD
Expand Down
19 changes: 9 additions & 10 deletions server/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ pub mod routes;

#[tokio::main]
pub async fn start() {
let (_main_server, _metrics_server) = tokio::join!(start_main_server(), start_metrics_server());
let config = AppConfig::init();
let (_main_server, _metrics_server) = tokio::join!(start_main_server(&config), start_metrics_server(&config));
}

pub async fn start_main_server() {
let (server_address, router) = init().await;
pub async fn start_main_server(config: &AppConfig) {
let (server_address, router) = init(config).await;
info!("Server is listening on {}...", server_address);
let _ = Server::bind(&server_address)
.serve(router.into_make_service())
Expand All @@ -25,8 +26,8 @@ pub async fn start_main_server() {
info!("Shutting down...");
}

pub async fn start_metrics_server() {
let (metrics_address, metrics_router) = metrics_init();
pub async fn start_metrics_server(config: &AppConfig) {
let (metrics_address, metrics_router) = metrics_init(config);
info!("Metrics is listening on {}...", metrics_address);
axum::Server::bind(&metrics_address)
.serve(metrics_router.into_make_service())
Expand All @@ -35,9 +36,8 @@ pub async fn start_metrics_server() {
}

/// Initializes every part of the application.
async fn init() -> (SocketAddr, Router) {
async fn init(config: &AppConfig) -> (SocketAddr, Router) {
print_logo();
let config = AppConfig::init();

info!("Initializing logging...");
tracing_subscriber::fmt()
Expand Down Expand Up @@ -68,12 +68,11 @@ async fn init() -> (SocketAddr, Router) {
(config.server_address, router)
}

fn metrics_init() -> (SocketAddr, Router) {
let config = AppConfig::init();
fn metrics_init(config: &AppConfig) -> (SocketAddr, Router) {
let recorder_handle = setup_metrics_recorder();
let router = Router::new().route("/metrics", get(move || ready(recorder_handle.render())));

(config.metrics_address, router)
(config.metrics_config.metrics_address, router)
}

fn setup_metrics_recorder() -> PrometheusHandle {
Expand Down
10 changes: 8 additions & 2 deletions server/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use tracing::metadata::LevelFilter;
pub struct AppConfig {
#[config(env = "SERVER_ADDRESS")]
pub server_address: SocketAddr,
#[config(env = "METRICS_ADDRESS")]
pub metrics_address: SocketAddr,
#[config(env = "DATABASE_URL")]
pub database_url: String,
#[config(env = "COOKIE_KEY")]
Expand All @@ -17,6 +15,8 @@ pub struct AppConfig {
pub log_level: LogLevel,
#[config(nested)]
pub mail_config: MailConfig,
#[config(nested)]
pub metrics_config: MetricsConfig,
}

#[derive(Debug, Clone, Config)]
Expand All @@ -31,6 +31,12 @@ pub struct MailConfig {
pub smtp_password: String,
}

#[derive(Debug, Clone, Config)]
pub struct MetricsConfig {
#[config(env = "METRICS_ADDRESS")]
pub metrics_address: SocketAddr,
}

impl AppConfig {
#[must_use]
pub fn init() -> Self {
Expand Down
3 changes: 3 additions & 0 deletions server/docker/config/app-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ smtp_address = "veryrezsi_smtp"
smtp_port = 1025
smtp_username = ""
smtp_password = ""

[metrics_config]
metrics_address = "127.0.0.1:3001"
4 changes: 3 additions & 1 deletion server/resources/app-config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
server_address = "127.0.0.1:8000"
metrics_address = "127.0.0.1:3001"
database_url = "mysql://veryrezsi:password@localhost:3306/veryrezsi"
cookie_key = "tQ2FiMTuOFcBHyrEyWcqlRmliXXDhX0V9SH4LSFUxkvYPFuRgscJ1g2wDLq4qdeB"
log_level = "debug"
Expand All @@ -10,3 +9,6 @@ smtp_port = 1025
# Credentials are not in use on dev environment
smtp_username = ""
smtp_password = ""

[metrics_config]
metrics_address = "127.0.0.1:3001"

0 comments on commit dfd4618

Please sign in to comment.