1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#![cfg(feature = "rocket")]

use std::{borrow::Cow, io::Cursor, sync::Arc};

use rocket::{
    http::{Header, Status},
    response::{status::NotFound, Responder as RocketResponder},
    route::{Handler, Outcome},
    serde::json::Json,
    Data as RocketData, Request, Response, Route,
};

use crate::{ApiDoc, Config, SwaggerFile, SwaggerUi};

impl From<SwaggerUi> for Vec<Route> {
    fn from(swagger_ui: SwaggerUi) -> Self {
        let mut routes =
            Vec::<Route>::with_capacity(swagger_ui.urls.len() + 1 + swagger_ui.external_urls.len());
        let mut api_docs =
            Vec::<Route>::with_capacity(swagger_ui.urls.len() + swagger_ui.external_urls.len());

        let urls = swagger_ui
            .urls
            .into_iter()
            .map(|(url, openapi)| (url, ApiDoc::Utoipa(openapi)))
            .chain(
                swagger_ui
                    .external_urls
                    .into_iter()
                    .map(|(url, api_doc)| (url, ApiDoc::Value(api_doc))),
            )
            .map(|(url, openapi)| {
                api_docs.push(Route::new(
                    rocket::http::Method::Get,
                    &url.url,
                    ServeApiDoc(openapi),
                ));
                url
            });

        routes.push(Route::new(
            rocket::http::Method::Get,
            swagger_ui.path.as_ref(),
            ServeSwagger(
                swagger_ui.path.clone(),
                Arc::new(if let Some(config) = swagger_ui.config {
                    if config.url.is_some() || !config.urls.is_empty() {
                        config
                    } else {
                        config.configure_defaults(urls)
                    }
                } else {
                    Config::new(urls)
                }),
            ),
        ));
        routes.extend(api_docs);

        routes
    }
}

#[derive(Clone)]
struct ServeApiDoc(ApiDoc);

#[rocket::async_trait]
impl Handler for ServeApiDoc {
    async fn handle<'r>(&self, request: &'r Request<'_>, _: RocketData<'r>) -> Outcome<'r> {
        Outcome::from(request, Json(self.0.clone()))
    }
}

#[derive(Clone)]
struct ServeSwagger(Cow<'static, str>, Arc<Config<'static>>);

#[rocket::async_trait]
impl Handler for ServeSwagger {
    async fn handle<'r>(&self, request: &'r Request<'_>, _: RocketData<'r>) -> Outcome<'r> {
        let mut base_path = self.0.as_ref();
        if let Some(index) = self.0.find('<') {
            base_path = &base_path[..index];
        }

        let request_path = request.uri().path().as_str();
        let request_path = match request_path.strip_prefix(base_path) {
            Some(stripped) => stripped,
            None => return Outcome::from(request, RedirectResponder(base_path.into())),
        };
        match super::serve(request_path, self.1.clone()) {
            Ok(swagger_file) => swagger_file
                .map(|file| Outcome::from(request, file))
                .unwrap_or_else(|| Outcome::from(request, NotFound("Swagger UI file not found"))),
            Err(error) => Outcome::from(
                request,
                rocket::response::status::Custom(Status::InternalServerError, error.to_string()),
            ),
        }
    }
}

impl<'r, 'o: 'r> RocketResponder<'r, 'o> for SwaggerFile<'o> {
    fn respond_to(self, _: &'r Request<'_>) -> rocket::response::Result<'o> {
        Ok(Response::build()
            .header(Header::new("Content-Type", self.content_type))
            .sized_body(self.bytes.len(), Cursor::new(self.bytes.to_vec()))
            .status(Status::Ok)
            .finalize())
    }
}

struct RedirectResponder(String);
impl<'r, 'a: 'r> RocketResponder<'r, 'a> for RedirectResponder {
    fn respond_to(self, _request: &'r Request<'_>) -> rocket::response::Result<'a> {
        Response::build()
            .status(Status::Found)
            .raw_header("Location", self.0)
            .ok()
    }
}