Skip to content

Commit

Permalink
feat: host openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Aug 19, 2022
1 parent b35c5c7 commit 2fc0fef
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
File renamed without changes.
49 changes: 49 additions & 0 deletions backend/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::OpenIdClient;
use actix_web::{web, HttpRequest};
use anyhow::Context;
use drogue_cloud_service_api::endpoints::Endpoints;
use serde_json::{json, Value};
use std::borrow::Cow;

const SPEC: &str = include_str!("../api/index.yaml");

pub fn spec(
req: HttpRequest,
endpoints: &Endpoints,
client: web::Data<OpenIdClient>,
) -> anyhow::Result<Value> {
let mut api: Value = serde_yaml::from_str(SPEC).context("Failed to parse OpenAPI YAML")?;

let url = endpoints.api.as_ref().map(Cow::from).unwrap_or_else(|| {
let ci = req.connection_info();
Cow::from(format!("{}://{}", ci.scheme(), ci.host()))
});

// server

api["servers"] = json!([{ "url": url, "description": "Drogue Cloud API" }]);

// SSO

let url = client.client.config().authorization_endpoint.clone();

api["security"] = json!([{"Drogue Cloud SSO": []}]);
api["components"]["securitySchemes"] = json!({
"Drogue Cloud SSO": {
"type": "oauth2",
"description": "SSO",
"flows": {
"implicit": {
"authorizationUrl": url,
"scopes": {
"openid": "OpenID Connect",
}
}
}
},
});

// render

Ok(api)
}

0 comments on commit 2fc0fef

Please sign in to comment.