@@ -2,16 +2,16 @@ use http::{HeaderMap, HeaderValue, Method, StatusCode};
22use reqwest:: Response ;
33use serde_json:: { json, Value as JsonValue } ;
44use std:: { env, fs} ;
5+ use utoipa:: openapi:: OpenApi ;
56
6- use crate :: api :: {
7+ use app :: {
78 requests:: engage_many_plain_requests,
8- responses:: { resolve_external_service_bad_response, APIRoutingError , APIRoutingResponse } ,
9- utils :: { get_cors_response_headers , get_default_headers , get_plain_headers , ParsedRequest } ,
9+ responses:: { resolve_external_service_bad_response, APIResponse , APIRoutingResponse } ,
10+ router :: ParsedRequest ,
1011} ;
12+ use utils:: api:: { get_cors_response_headers, get_default_headers, get_plain_headers} ;
1113
12- pub async fn cors_preflight_response (
13- _request : ParsedRequest ,
14- ) -> Result < APIRoutingResponse , APIRoutingError > {
14+ pub async fn cors_preflight_response ( _request : ParsedRequest ) -> APIResponse {
1515 Ok ( APIRoutingResponse :: new (
1616 StatusCode :: OK ,
1717 "" ,
@@ -20,7 +20,7 @@ pub async fn cors_preflight_response(
2020}
2121
2222#[ utoipa:: path( get, path = "/" , responses( ( status = 303 , description = "Redirect to /docs" ) ) ) ]
23- pub async fn index ( _request : ParsedRequest ) -> Result < APIRoutingResponse , APIRoutingError > {
23+ pub async fn index ( _request : ParsedRequest ) -> APIResponse {
2424 Ok ( APIRoutingResponse :: new (
2525 StatusCode :: TEMPORARY_REDIRECT ,
2626 "Redirecting to /docs" ,
@@ -37,7 +37,7 @@ pub async fn index(_request: ParsedRequest) -> Result<APIRoutingResponse, APIRou
3737 path = "/docs" ,
3838 responses( ( status = 200 , description = "API documentation" , content_type = "text/html" ) )
3939) ]
40- pub async fn docs ( _request : ParsedRequest ) -> Result < APIRoutingResponse , APIRoutingError > {
40+ pub async fn docs ( _request : ParsedRequest ) -> APIResponse {
4141 let body =
4242 fs:: read_to_string ( "./openapi/index.html" ) . expect ( "Unable to read index.html file" ) ;
4343 Ok ( APIRoutingResponse :: new ( StatusCode :: OK , body. as_ref ( ) , {
@@ -47,10 +47,20 @@ pub async fn docs(_request: ParsedRequest) -> Result<APIRoutingResponse, APIRout
4747 } ) )
4848}
4949
50- pub async fn openapi_spec ( json_spec : String ) -> Result < APIRoutingResponse , APIRoutingError > {
50+ pub async fn openapi_spec ( openapi : OpenApi ) -> APIResponse {
51+ let json_spec_orig_serialized = openapi. to_json ( ) . expect ( "Failed to parse openapi spec" ) ;
52+
53+ // Inject the app specific securitySchemas to the spec as a workaround for utoipa::OpenApi zeroing the parse_meta output of derived OpenApi structs when using the security attribute
54+ let mut json_spec: JsonValue =
55+ serde_json:: from_str ( & json_spec_orig_serialized) . expect ( "Failed to parse openapi spec" ) ;
56+ json_spec[ "components" ] [ "securitySchemes" ] = json ! ( {
57+ "BearerAuth" : { "scheme" : "bearer" , "type" : "http" }
58+ } ) ;
59+ let json_spec_serialized = json_spec. to_string ( ) ;
60+
5161 Ok ( APIRoutingResponse :: new (
5262 StatusCode :: OK ,
53- json_spec . as_ref ( ) ,
63+ json_spec_serialized . as_ref ( ) ,
5464 {
5565 let mut headers = HeaderMap :: new ( ) ;
5666 headers. insert ( "Content-Type" , HeaderValue :: from_static ( "application/json" ) ) ;
@@ -70,17 +80,15 @@ pub async fn openapi_spec(json_spec: String) -> Result<APIRoutingResponse, APIRo
7080 example = json!( "OK" ) ,
7181 ) )
7282) ]
73- pub async fn health_check (
74- _request : ParsedRequest ,
75- ) -> Result < APIRoutingResponse , APIRoutingError > {
83+ pub async fn health_check ( _request : ParsedRequest ) -> APIResponse {
7684 Ok ( APIRoutingResponse :: new (
7785 StatusCode :: OK ,
7886 "OK" ,
7987 get_plain_headers ( ) ,
8088 ) )
8189}
8290
83- pub async fn not_found ( _request : ParsedRequest ) -> Result < APIRoutingResponse , APIRoutingError > {
91+ pub async fn not_found ( _request : ParsedRequest ) -> APIResponse {
8492 Ok ( APIRoutingResponse {
8593 status_code : StatusCode :: NOT_FOUND ,
8694 body : json ! ( {
@@ -91,9 +99,7 @@ pub async fn not_found(_request: ParsedRequest) -> Result<APIRoutingResponse, AP
9199 } )
92100}
93101
94- pub async fn get_external_service_bad_response (
95- response : Response ,
96- ) -> Result < APIRoutingResponse , APIRoutingError > {
102+ pub async fn get_external_service_bad_response ( response : Response ) -> APIResponse {
97103 let status_code = response. status ( ) ;
98104 let response_body = response. text ( ) . await ?;
99105 resolve_external_service_bad_response ( status_code, response_body)
@@ -115,9 +121,7 @@ pub async fn get_external_service_bad_response(
115121 } ) ,
116122 ) )
117123) ]
118- pub async fn wake_up_external_services (
119- _request : ParsedRequest ,
120- ) -> Result < APIRoutingResponse , APIRoutingError > {
124+ pub async fn wake_up_external_services ( _request : ParsedRequest ) -> APIResponse {
121125 let endpoints = vec ! [
122126 format!(
123127 "{}/health" ,
0 commit comments