Skip to content

Commit

Permalink
Support for global http request headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
gudaoxuri committed Jul 26, 2022
1 parent 3a27188 commit 0dfb3c4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/multi-apps/config/conf-default.toml
Expand Up @@ -14,6 +14,7 @@ port = 8089
[fw.web_server.modules.doc]
name = "文档应用示例"
doc_urls = [["test env", "http://localhost:8089/"]]
req_headers = [["Token", "token header"],["App-Id", "app id header"]]
[fw.web_server.modules.tag]
name = "标签应用示例"
doc_urls = [["test env", "http://localhost:8089/"]]
Expand Down
10 changes: 10 additions & 0 deletions src/basic/config.rs
Expand Up @@ -245,6 +245,10 @@ pub struct WebServerConfig {
///
/// Formatted as ``[(environment identifier, request path)]`` / 格式为 ``[(环境标识,请求路径)]``
pub doc_urls: Vec<(String, String)>,
/// Common request headers for ``OpenAPI`` / 公共请求头信息,用于 ``OpenAPI``
///
/// Formatted as ``[(header name, header description)]`` / 格式为 ``[(请求头名称,请求头说明)]``
pub req_headers: Vec<(String, String)>,
/// ``OpenAPI`` UI path / 模``OpenAPI`` UI路径
pub ui_path: Option<String>,
/// ``OpenAPI`` information path / ``OpenAPI`` 信息路径
Expand Down Expand Up @@ -312,6 +316,10 @@ pub struct WebServerModuleConfig {
///
/// Formatted as ``[(environment identifier, request path)]`` / 格式为 ``[(环境标识,请求路径)]``
pub doc_urls: Vec<(String, String)>,
/// Module common request headers for ``OpenAPI`` / 模块公共请求头信息,用于 ``OpenAPI``
///
/// Formatted as ``[(header name, header description)]`` / 格式为 ``[(请求头名称,请求头说明)]``
pub req_headers: Vec<(String, String)>,
/// Module ``OpenAPI`` UI path / 模块 ``OpenAPI`` UI路径
pub ui_path: Option<String>,
/// Module ``OpenAPI`` information path / 模块 ``OpenAPI`` 信息路径
Expand Down Expand Up @@ -339,6 +347,7 @@ impl Default for WebServerConfig {
security_hide_err_msg: false,
context_conf: WebServerContextConfig::default(),
doc_urls: [("test env".to_string(), "http://localhost:8080/".to_string())].to_vec(),
req_headers: vec![],
ui_path: Some("ui".to_string()),
spec_path: Some("spec".to_string()),
modules: Default::default(),
Expand All @@ -352,6 +361,7 @@ impl Default for WebServerModuleConfig {
name: "Tardis-based application".to_string(),
version: "1.0.0".to_string(),
doc_urls: [("test env".to_string(), "http://localhost:8080/".to_string())].to_vec(),
req_headers: vec![],
ui_path: Some("ui".to_string()),
spec_path: Some("spec".to_string()),
}
Expand Down
6 changes: 5 additions & 1 deletion src/web/web_server.rs
Expand Up @@ -2,7 +2,7 @@ use futures_util::lock::Mutex;
use poem::listener::{Listener, RustlsCertificate, RustlsConfig, TcpListener};
use poem::middleware::Cors;
use poem::{EndpointExt, Route};
use poem_openapi::{OpenApi, OpenApiService, ServerObject};
use poem_openapi::{ExtraHeader, OpenApi, OpenApiService, ServerObject};
use tokio::time::Duration;

use crate::basic::config::{FrameworkConfig, WebServerConfig, WebServerModuleConfig};
Expand Down Expand Up @@ -43,6 +43,7 @@ impl TardisWebServer {
name: self.app_name.clone(),
version: self.version.clone(),
doc_urls: self.config.doc_urls.clone(),
req_headers: self.config.req_headers.clone(),
ui_path: self.config.ui_path.clone(),
spec_path: self.config.spec_path.clone(),
};
Expand Down Expand Up @@ -78,6 +79,9 @@ impl TardisWebServer {
let url = if !url.ends_with('/') { format!("{}/{}", url, code) } else { format!("{}{}", url, code) };
api_serv = api_serv.server(ServerObject::new(url).description(env));
}
for (name, desc) in &module.req_headers {
api_serv = api_serv.extra_request_header::<String, _>(ExtraHeader::new(name).description(desc));
}
let ui_serv = api_serv.rapidoc();
let spec_serv = api_serv.spec();
let mut route = Route::new();
Expand Down
2 changes: 1 addition & 1 deletion tests/test_web_server.rs
Expand Up @@ -181,7 +181,7 @@ async fn test_basic(url: &str) -> TardisResult<()> {

async fn test_validate(url: &str) -> TardisResult<()> {
let response = TardisFuns::web_client().get::<TardisResp<TodoResp>>(format!("{}/todo/todos/ss", url).as_str(), None).await?.body.unwrap();
assert_eq!(response.code, TardisError::not_found("", "").code);
assert_eq!(response.code, TardisError::bad_request("", "").code);
assert_eq!(
response.msg,
r#"[Tardis.WebServer] Process error: failed to parse path `id`: failed to parse "integer(int64)": invalid digit found in string"#
Expand Down

0 comments on commit 0dfb3c4

Please sign in to comment.