Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get server for Openapi specification #236

Merged
merged 17 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .config/dictionaries/project.dic
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@ nextest
testcov
testdocs
fmtchk
fmtfix
fmtfix
gethostname
afinet
netifas
55 changes: 55 additions & 0 deletions catalyst-gateway/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions catalyst-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ tokio = "1"

dotenvy = "0.15"

local-ip-address = "0.5.7"
gethostname = "0.4.3"

[workspace.lints.rust]
warnings = "deny"
missing_docs = "deny"
Expand Down
4 changes: 3 additions & 1 deletion catalyst-gateway/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,6 @@ dotenvy = { workspace = true }
panic-message = { workspace = true }
cpu-time = { workspace = true }
ulid = { workspace = true, features = ["serde", "uuid"] }
rust-embed = { workspace = true }
rust-embed = { workspace = true }
local-ip-address.workspace = true
gethostname.workspace = true
35 changes: 34 additions & 1 deletion catalyst-gateway/bin/src/service/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
//!
//! This defines all endpoints for the Catalyst Gateway API.
//! It however does NOT contain any processing for them, that is defined elsewhere.
use std::net::IpAddr;

use gethostname::gethostname;
use health::HealthApi;
use local_ip_address::list_afinet_netifas;
use poem_openapi::{ContactObject, LicenseObject, OpenApiService, ServerObject};
use registration::RegistrationApi;
use test_endpoints::TestApi;
Expand All @@ -23,6 +27,9 @@ const API_TITLE: &str = "Catalyst Gateway";
/// The version of the API
const API_VERSION: &str = "1.2.0";

/// Port
const PORT: &str = "5432";
bkioshn marked this conversation as resolved.
Show resolved Hide resolved

/// Get the contact details for inquiring about the API
fn get_api_contact() -> ContactObject {
ContactObject::new()
Expand Down Expand Up @@ -72,8 +79,34 @@ pub(crate) fn mk_api(

// Add all the hosts where this API should be reachable.
for host in hosts {
service = service.server(ServerObject::new(host));
service = service.server(ServerObject::new(host).description("API Host"));
}

// Get localhost name
if let Ok(hostname) = gethostname().into_string() {
let hostname_with_port = format!("{hostname}:{PORT}");
service = service
.server(ServerObject::new(hostname_with_port).description("Server at localhost name"));
}

// Get local IP address v4 and v6
let network_interfaces = list_afinet_netifas();
if let Ok(network_interfaces) = network_interfaces {
for (name, ip) in &network_interfaces {
if *name == "en0" {
let (ip_with_port, desc) = match ip {
IpAddr::V4(_) => {
let ip_str = format!("{ip}:{PORT}");
(ip_str, "Server at local IPv4 address")
},
IpAddr::V6(_) => {
let ip_str = format!("[{ip}]:{PORT}");
(ip_str, "Server at local IPv6 address")
},
};
service = service.server(ServerObject::new(ip_with_port).description(desc));
}
}
}
bkioshn marked this conversation as resolved.
Show resolved Hide resolved
service
}
Loading