1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 04:52:28 +00:00

add server data for openapi

This commit is contained in:
Dominic 2019-09-30 18:41:18 +02:00
parent 4ceb1ef302
commit 6cf4b05447
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
3 changed files with 15 additions and 9 deletions

View file

@ -127,7 +127,7 @@ fn main()
); );
gotham::start(ADDR, build_router(chain, pipelines, |route| { gotham::start(ADDR, build_router(chain, pipelines, |route| {
route.with_openapi("Users Example", "0.0.1", |mut route| { route.with_openapi("Users Example", "0.0.1", ADDR, |mut route| {
route.resource::<Users, _>("users"); route.resource::<Users, _>("users");
route.get_openapi("openapi"); route.get_openapi("openapi");
}); });

View file

@ -14,7 +14,7 @@ use gotham::{
use indexmap::IndexMap; use indexmap::IndexMap;
use log::error; use log::error;
use mime::{APPLICATION_JSON, TEXT_PLAIN}; use mime::{APPLICATION_JSON, TEXT_PLAIN};
use openapiv3::{MediaType, OpenAPI, Operation, PathItem, Paths, ReferenceOr, ReferenceOr::Item, Response, Responses, StatusCode}; use openapiv3::{MediaType, OpenAPI, Operation, PathItem, Paths, ReferenceOr, ReferenceOr::Item, Response, Responses, Server, StatusCode};
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use std::panic::RefUnwindSafe; use std::panic::RefUnwindSafe;
@ -22,7 +22,7 @@ pub struct OpenapiRouter(OpenAPI);
impl OpenapiRouter impl OpenapiRouter
{ {
pub fn new<Title : ToString, Version : ToString>(title : Title, version : Version) -> Self pub fn new<Title : ToString, Version : ToString, Url : ToString>(title : Title, version : Version, server_url : Url) -> Self
{ {
Self(OpenAPI { Self(OpenAPI {
openapi: "3.0.2".to_string(), openapi: "3.0.2".to_string(),
@ -34,7 +34,11 @@ impl OpenapiRouter
license: None, license: None,
version: version.to_string() version: version.to_string()
}, },
servers: Vec::new(), servers: vec![Server {
url: server_url.to_string(),
description: None,
variables: None
}],
paths: Paths::new(), paths: Paths::new(),
components: None, components: None,
security: Vec::new(), security: Vec::new(),

View file

@ -34,11 +34,12 @@ struct PathExtractor<ID : RefUnwindSafe + Send + 'static>
#[cfg(feature = "openapi")] #[cfg(feature = "openapi")]
pub trait WithOpenapi<D> pub trait WithOpenapi<D>
{ {
fn with_openapi<F, Title, Version>(&mut self, title : Title, version : Version, block : F) fn with_openapi<F, Title, Version, Url>(&mut self, title : Title, version : Version, server_url : Url, block : F)
where where
F : FnOnce((&mut D, &mut OpenapiRouter)), F : FnOnce((&mut D, &mut OpenapiRouter)),
Title : ToString, Title : ToString,
Version : ToString; Version : ToString,
Url : ToString;
} }
/// This trait adds the `resource` method to gotham's routing. It allows you to register /// This trait adds the `resource` method to gotham's routing. It allows you to register
@ -235,13 +236,14 @@ macro_rules! implDrawResourceRoutes {
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static, C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
P : RefUnwindSafe + Send + Sync + 'static P : RefUnwindSafe + Send + Sync + 'static
{ {
fn with_openapi<F, Title, Version>(&mut self, title : Title, version : Version, block : F) fn with_openapi<F, Title, Version, Url>(&mut self, title : Title, version : Version, server_url : Url, block : F)
where where
F : FnOnce((&mut Self, &mut OpenapiRouter)), F : FnOnce((&mut Self, &mut OpenapiRouter)),
Title : ToString, Title : ToString,
Version : ToString Version : ToString,
Url : ToString
{ {
let mut router = OpenapiRouter::new(title, version); let mut router = OpenapiRouter::new(title, version, server_url);
block((self, &mut router)); block((self, &mut router));
} }
} }