Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 1 addition & 42 deletions reference-apps/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use actix_web::{web, App, HttpResponse, HttpServer, Responder, middleware};

Check warning on line 1 in reference-apps/rust/src/main.rs

View workflow job for this annotation

GitHub Actions / Rust Lint (Clippy)

Diff in /home/runner/work/devstack-core/devstack-core/reference-apps/rust/src/main.rs
use actix_cors::Cors;
use serde::{Deserialize, Serialize};
use std::env;
Expand Down Expand Up @@ -111,23 +111,23 @@
message: String,
}

// Prometheus metrics

Check warning on line 114 in reference-apps/rust/src/main.rs

View workflow job for this annotation

GitHub Actions / Rust Lint (Clippy)

Diff in /home/runner/work/devstack-core/devstack-core/reference-apps/rust/src/main.rs
lazy_static! {
static ref REGISTRY: Registry = Registry::new();

static ref HTTP_REQUESTS_TOTAL: CounterVec = CounterVec::new(
Opts::new("http_requests_total", "Total HTTP requests"),
&["method", "endpoint", "status"]
).unwrap();

Check warning on line 121 in reference-apps/rust/src/main.rs

View workflow job for this annotation

GitHub Actions / Rust Lint (Clippy)

Diff in /home/runner/work/devstack-core/devstack-core/reference-apps/rust/src/main.rs

static ref HTTP_REQUEST_DURATION: HistogramVec = HistogramVec::new(
prometheus::HistogramOpts::new("http_request_duration_seconds", "HTTP request latency"),
&["method", "endpoint"]
).unwrap();

Check warning on line 126 in reference-apps/rust/src/main.rs

View workflow job for this annotation

GitHub Actions / Rust Lint (Clippy)

Diff in /home/runner/work/devstack-core/devstack-core/reference-apps/rust/src/main.rs
}

fn register_metrics() {
REGISTRY.register(Box::new(HTTP_REQUESTS_TOTAL.clone())).ok();

Check warning on line 130 in reference-apps/rust/src/main.rs

View workflow job for this annotation

GitHub Actions / Rust Lint (Clippy)

Diff in /home/runner/work/devstack-core/devstack-core/reference-apps/rust/src/main.rs
REGISTRY.register(Box::new(HTTP_REQUEST_DURATION.clone())).ok();
}

Expand Down Expand Up @@ -203,7 +203,7 @@
}

async fn health_vault() -> impl Responder {
let vault_addr = get_env_or("VAULT_ADDR", "http://vault:8200");

Check warning on line 206 in reference-apps/rust/src/main.rs

View workflow job for this annotation

GitHub Actions / Rust Lint (Clippy)

Diff in /home/runner/work/devstack-core/devstack-core/reference-apps/rust/src/main.rs

match reqwest::get(format!("{}/v1/sys/health", vault_addr)).await {
Ok(resp) if resp.status().is_success() => {
Expand Down Expand Up @@ -233,7 +233,7 @@
Err(response) => HttpResponse::ServiceUnavailable().json(response),
}
}

Check warning on line 236 in reference-apps/rust/src/main.rs

View workflow job for this annotation

GitHub Actions / Rust Lint (Clippy)

Diff in /home/runner/work/devstack-core/devstack-core/reference-apps/rust/src/main.rs
async fn check_postgres_health() -> Result<HealthResponse, HealthResponse> {
// Get credentials from Vault
let creds = get_vault_secret("postgres").await.map_err(|e| HealthResponse {
Expand Down Expand Up @@ -298,7 +298,7 @@
Ok(response) => HttpResponse::Ok().json(response),
Err(response) => HttpResponse::ServiceUnavailable().json(response),
}
}

Check warning on line 301 in reference-apps/rust/src/main.rs

View workflow job for this annotation

GitHub Actions / Rust Lint (Clippy)

Diff in /home/runner/work/devstack-core/devstack-core/reference-apps/rust/src/main.rs

async fn check_mysql_health() -> Result<HealthResponse, HealthResponse> {
let creds = get_vault_secret("mysql").await.map_err(|e| HealthResponse {
Expand All @@ -320,7 +320,7 @@
.tcp_port(port)
.user(Some(user))
.pass(Some(password))
.db_name(Some(database));

Check warning on line 323 in reference-apps/rust/src/main.rs

View workflow job for this annotation

GitHub Actions / Rust Lint (Clippy)

Diff in /home/runner/work/devstack-core/devstack-core/reference-apps/rust/src/main.rs

match mysql_async::Conn::new(opts).await {
Ok(mut conn) => {
Expand Down Expand Up @@ -372,7 +372,7 @@
Ok(response) => HttpResponse::Ok().json(response),
Err(response) => HttpResponse::ServiceUnavailable().json(response),
}
}

Check warning on line 375 in reference-apps/rust/src/main.rs

View workflow job for this annotation

GitHub Actions / Rust Lint (Clippy)

Diff in /home/runner/work/devstack-core/devstack-core/reference-apps/rust/src/main.rs

async fn check_mongodb_health() -> Result<HealthResponse, HealthResponse> {
let creds = get_vault_secret("mongodb").await.map_err(|e| HealthResponse {
Expand Down Expand Up @@ -1340,45 +1340,4 @@
}

#[cfg(test)]
mod tests {
use super::*;
use actix_web::{test, App};

#[actix_web::test]
async fn test_root_endpoint() {
let app = test::init_service(App::new().route("/", web::get().to(root))).await;
let req = test::TestRequest::get().uri("/").to_request();
let resp = test::call_service(&app, req).await;

assert!(resp.status().is_success());

let body: ApiInfo = test::read_body_json(resp).await;
assert_eq!(body.name, "DevStack Core Reference API");
assert_eq!(body.version, "1.0.0");
assert_eq!(body.language, "Rust");
assert_eq!(body.framework, "Actix-web");
}

#[actix_web::test]
async fn test_health_endpoint() {
let app = test::init_service(App::new().route("/health/", web::get().to(health_simple))).await;
let req = test::TestRequest::get().uri("/health/").to_request();
let resp = test::call_service(&app, req).await;

assert!(resp.status().is_success());

let body: HealthResponse = test::read_body_json(resp).await;
assert_eq!(body.status, "healthy");
assert!(body.timestamp.is_some());
}

#[actix_web::test]
async fn test_metrics_endpoint() {
register_metrics();
let app = test::init_service(App::new().route("/metrics", web::get().to(metrics))).await;
let req = test::TestRequest::get().uri("/metrics").to_request();
let resp = test::call_service(&app, req).await;

assert!(resp.status().is_success());
}
}
mod tests; // Comprehensive test suite in tests.rs
Loading
Loading