mirror of
https://github.com/stoatchat/rust-okapi.git
synced 2026-07-01 21:44:49 -04:00
af52ba1c39
- Allow customization of OpenApi object. - Allow merging of OpenApi objects. - Retrieve OpenApi object after generating. (Closes: #28) - Create `mount_endpoints_and_merged_docs` marco in order to streamline code structure for bigger projects. (Closes: #30) - Added new example for structuring bigger projects. - Allowed changing path where OpenApi file is hosted. - Added `log v0.4` as a dependency.
99 lines
3.3 KiB
Rust
99 lines
3.3 KiB
Rust
use okapi::openapi3::OpenApi;
|
|
use rocket::{Build, Rocket};
|
|
use rocket_okapi::settings::UrlObject;
|
|
use rocket_okapi::{mount_endpoints_and_merged_docs, rapidoc::*, swagger_ui::*};
|
|
|
|
mod error;
|
|
mod message;
|
|
mod post;
|
|
|
|
pub type Result<T> = std::result::Result<rocket::serde::json::Json<T>, error::Error>;
|
|
pub type DataResult<'a, T> =
|
|
std::result::Result<rocket::serde::json::Json<T>, rocket::serde::json::Error<'a>>;
|
|
|
|
#[rocket::main]
|
|
async fn main() {
|
|
let launch_result = create_server().launch().await;
|
|
match launch_result {
|
|
Ok(()) => println!("Rocket shut down gracefully."),
|
|
Err(err) => println!("Rocket had an error: {}", err),
|
|
};
|
|
}
|
|
|
|
pub fn create_server() -> Rocket<Build> {
|
|
let mut building_rocket = rocket::build()
|
|
.mount(
|
|
"/swagger-ui/",
|
|
make_swagger_ui(&SwaggerUIConfig {
|
|
url: "../v1/openapi.json".to_owned(),
|
|
..Default::default()
|
|
}),
|
|
)
|
|
.mount(
|
|
"/rapidoc/",
|
|
make_rapidoc(&RapiDocConfig {
|
|
general: GeneralConfig {
|
|
spec_urls: vec![UrlObject::new("General", "../v1/openapi.json")],
|
|
..Default::default()
|
|
},
|
|
hide_show: HideShowConfig {
|
|
allow_spec_url_load: false,
|
|
allow_spec_file_load: false,
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
}),
|
|
);
|
|
|
|
let openapi_settings = rocket_okapi::settings::OpenApiSettings::default();
|
|
let custom_route_spec = (vec![], custom_openapi_spec());
|
|
mount_endpoints_and_merged_docs! {
|
|
building_rocket, "/v1/".to_owned(), openapi_settings,
|
|
"/v1/" => custom_route_spec,
|
|
"/v1/post" => post::get_routes_and_docs(),
|
|
"/v1/message" => message::get_routes_and_docs(),
|
|
};
|
|
|
|
building_rocket
|
|
}
|
|
|
|
fn custom_openapi_spec() -> OpenApi {
|
|
use okapi::openapi3::*;
|
|
OpenApi {
|
|
openapi: OpenApi::default_version(),
|
|
info: Info {
|
|
title: "The best API ever".to_owned(),
|
|
description: Some("This is the best API every, please use me!".to_owned()),
|
|
terms_of_service: Some(
|
|
"https://github.com/GREsau/okapi/blob/master/LICENSE".to_owned(),
|
|
),
|
|
contact: Some(Contact {
|
|
name: Some("okapi example".to_owned()),
|
|
url: Some("https://github.com/GREsau/okapi".to_owned()),
|
|
email: None,
|
|
..Default::default()
|
|
}),
|
|
license: Some(License {
|
|
name: "MIT".to_owned(),
|
|
url: Some("https://github.com/GREsau/okapi/blob/master/LICENSE".to_owned()),
|
|
..Default::default()
|
|
}),
|
|
version: env!("CARGO_PKG_VERSION").to_owned(),
|
|
..Default::default()
|
|
},
|
|
servers: vec![
|
|
Server {
|
|
url: "http://127.0.0.1:8000/".to_owned(),
|
|
description: Some("Localhost".to_owned()),
|
|
..Default::default()
|
|
},
|
|
Server {
|
|
url: "https://example.com/".to_owned(),
|
|
description: Some("Possible Remote".to_owned()),
|
|
..Default::default()
|
|
},
|
|
],
|
|
..Default::default()
|
|
}
|
|
}
|