Skip to content

Commit 8f2a734

Browse files
committed
refactor: adjust http proxy plugin handle
1 parent 5e6269e commit 8f2a734

21 files changed

+348
-80
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ reqwest = { version = "0.11.27", features = ["json"] }
4848
rust-embed = { version = "8.3.0", features = ["mime-guess", "compression"] }
4949
serde = "1.0.197"
5050
serde_json = "1.0.115"
51+
serde_repr = "0.1.19"
5152
snafu = "0.8.2"
5253
substring = "1.4.5"
5354
tempfile = "3.10.1"

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,25 @@ All toml configurations are as follows [pingap.toml](./conf/pingap.toml).
4040

4141
```mermaid
4242
graph TD;
43-
start("new request")-->server;
43+
start("New Request")-->server("HTTP Server");
4444
45-
server -- "host:HostA, Path:/api/*" --> locationA
45+
server -- "host:HostA, Path:/api/*" --> locationA("Location A")
4646
47-
server -- "Path:/rest/*"--> locationB
47+
server -- "Path:/rest/*"--> locationB("Location B")
4848
49-
locationA -- "10.0.0.1:8001" --> upstreamA1 --> response
49+
locationA -- "Exec Plugins" --> locationPluginListA("Plugin List A")
5050
51-
locationA -- "10.0.0.2:8001" --> upstreamA2 --> response
51+
locationB -- "Exec Plugins" --> locationPluginListB("Plugin List B")
5252
53-
locationB -- "10.0.0.1:8002" --> upstreamB1 --> response
53+
locationPluginListA -- "10.0.0.1:8001" --> upstreamA1("Upstream A1") --> response
5454
55-
locationB -- "10.0.0.2:8002" --> upstreamB2 --> response
55+
locationPluginListA -- "10.0.0.2:8001" --> upstreamA2("Upstream A2") --> response
5656
57-
response --> stop("logging");
57+
locationPluginListB -- "10.0.0.1:8002" --> upstreamB1("Upstream B1") --> response
58+
59+
locationPluginListB -- "10.0.0.2:8002" --> upstreamB2("Upstream B2") --> response
60+
61+
response("HTTP Response") --> stop("Logging");
5862
```
5963

6064
## Performance

build.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/introduction_zh.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ description: Pingap 简述
55
Pingap是基于[pingora](https://github.com/cloudflare/pingora)开发的,pingora提供了各类模块便于rust开发者使用,但并不方便非rust开发者使用,因此pingap提供了以toml的形式配置简单易用的反向代理,实现支持多location代理转发。特性如下:
66

77
- 支持多location配置,可通过请求的路径与域名筛选
8-
- 支持静态文件目录处理,简单方便的chunk的形式响应静态文件
9-
- 支持mock的响应配置,方便测试或应急使用
108
- 支持HTTP1与HTTP2两种协议
119
- 无中断请求的配置更新,方便实时更新应用配置
1210
- 模板式的请求日志输出,可按模板指定各种输出
1311
- 提供Web界面式的配置,简化操作
12+
- 支持各种插件形式,根据需要灵活配置各种特性:如静态文件目录、服务性能指标、WEB后台配置应用等
1413

1514
[Pingap处理流程](./phase_chart_zh.md)
1615

src/config/load.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use base64::{engine::general_purpose::STANDARD, Engine};
1717
use glob::glob;
1818
use http::HeaderValue;
1919
use serde::{Deserialize, Serialize};
20+
use serde_repr::{Deserialize_repr, Serialize_repr};
2021
use snafu::{ensure, ResultExt, Snafu};
2122
use std::collections::HashMap;
2223
use std::path::Path;
@@ -64,7 +65,8 @@ pub enum Error {
6465
}
6566
type Result<T, E = Error> = std::result::Result<T, E>;
6667

67-
#[derive(PartialEq, Debug, Default, Deserialize, Clone, Serialize)]
68+
#[derive(PartialEq, Debug, Default, Deserialize_repr, Clone, Serialize_repr)]
69+
#[repr(u8)]
6870
pub enum ProxyPluginCategory {
6971
#[default]
7072
Stats,
@@ -75,10 +77,19 @@ pub enum ProxyPluginCategory {
7577
Mock,
7678
}
7779

80+
#[derive(PartialEq, Debug, Default, Deserialize_repr, Clone, Copy, Serialize_repr)]
81+
#[repr(u8)]
82+
pub enum ProxyPluginStep {
83+
#[default]
84+
RequestFilter,
85+
ProxyUpstreamFilter,
86+
}
87+
7888
#[derive(Debug, Default, Deserialize, Clone, Serialize)]
7989
pub struct ProxyPluginConf {
8090
pub value: String,
8191
pub category: ProxyPluginCategory,
92+
pub step: Option<ProxyPluginStep>,
8293
pub remark: Option<String>,
8394
}
8495

src/config/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ mod load;
1818

1919
pub use load::{
2020
load_config, save_config, LocationConf, PingapConf, ProxyPluginCategory, ProxyPluginConf,
21-
ServerConf, UpstreamConf, CATEGORY_LOCATION, CATEGORY_PROXY_PLUGIN, CATEGORY_SERVER,
22-
CATEGORY_UPSTREAM,
21+
ProxyPluginStep, ServerConf, UpstreamConf, CATEGORY_LOCATION, CATEGORY_PROXY_PLUGIN,
22+
CATEGORY_SERVER, CATEGORY_UPSTREAM,
2323
};
2424

2525
static CONFIG_PATH: OnceCell<String> = OnceCell::new();

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ fn run() -> Result<(), Box<dyn Error>> {
200200
value: format!("/ {authorization}"),
201201
category: ProxyPluginCategory::Admin,
202202
remark: Some("Admin serve".to_string()),
203+
step: None,
203204
},
204205
));
205206
server_conf_list.push(ServerConf {

src/plugin/admin.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
// limitations under the License.
1414

1515
use super::{ProxyPlugin, Result};
16-
use crate::config::{self, save_config, LocationConf, ProxyPluginConf, ServerConf, UpstreamConf};
16+
use crate::config::{
17+
self, save_config, LocationConf, ProxyPluginCategory, ProxyPluginConf, ProxyPluginStep,
18+
ServerConf, UpstreamConf,
19+
};
1720
use crate::config::{
1821
PingapConf, CATEGORY_LOCATION, CATEGORY_PROXY_PLUGIN, CATEGORY_SERVER, CATEGORY_UPSTREAM,
1922
};
@@ -86,16 +89,18 @@ impl From<EmbeddedStaticFile> for HttpResponse {
8689
pub struct AdminServe {
8790
pub path: String,
8891
pub authorization: String,
92+
pub proxy_step: ProxyPluginStep,
8993
}
9094
impl AdminServe {
91-
pub fn new(value: &str) -> Result<Self> {
95+
pub fn new(value: &str, proxy_step: ProxyPluginStep) -> Result<Self> {
9296
let arr: Vec<&str> = value.split(' ').collect();
9397
let mut authorization = "".to_string();
9498
if arr.len() >= 2 {
9599
authorization = arr[1].trim().to_string();
96100
}
97101
Ok(Self {
98102
path: arr[0].trim().to_string(),
103+
proxy_step,
99104
authorization,
100105
})
101106
}
@@ -278,6 +283,14 @@ fn get_method_path(session: &Session) -> (Method, String) {
278283

279284
#[async_trait]
280285
impl ProxyPlugin for AdminServe {
286+
#[inline]
287+
fn step(&self) -> ProxyPluginStep {
288+
self.proxy_step
289+
}
290+
#[inline]
291+
fn category(&self) -> ProxyPluginCategory {
292+
ProxyPluginCategory::Admin
293+
}
281294
async fn handle(&self, session: &mut Session, ctx: &mut State) -> pingora::Result<bool> {
282295
if !session.req_header().uri.path().starts_with(&self.path) {
283296
return Ok(false);

src/plugin/compression.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use super::ProxyPlugin;
1616
use super::{Error, Result};
17+
use crate::config::{ProxyPluginCategory, ProxyPluginStep};
1718
use crate::state::State;
1819
use async_trait::async_trait;
1920
use http::HeaderValue;
@@ -29,10 +30,11 @@ pub struct Compression {
2930
br_level: u32,
3031
zstd_level: u32,
3132
support_compression: bool,
33+
proxy_step: ProxyPluginStep,
3234
}
3335

3436
impl Compression {
35-
pub fn new(value: &str) -> Result<Self> {
37+
pub fn new(value: &str, proxy_step: ProxyPluginStep) -> Result<Self> {
3638
let mut levels: [u32; 3] = [0, 0, 0];
3739
let mut support_compression = false;
3840
for (index, item) in value.split(' ').enumerate() {
@@ -48,6 +50,7 @@ impl Compression {
4850
}
4951
}
5052
Ok(Self {
53+
proxy_step,
5154
gzip_level: levels[0],
5255
br_level: levels[1],
5356
zstd_level: levels[2],
@@ -58,6 +61,14 @@ impl Compression {
5861

5962
#[async_trait]
6063
impl ProxyPlugin for Compression {
64+
#[inline]
65+
fn step(&self) -> ProxyPluginStep {
66+
self.proxy_step
67+
}
68+
#[inline]
69+
fn category(&self) -> ProxyPluginCategory {
70+
ProxyPluginCategory::Compression
71+
}
6172
#[inline]
6273
async fn handle(&self, session: &mut Session, _ctx: &mut State) -> pingora::Result<bool> {
6374
if !self.support_compression {

0 commit comments

Comments
 (0)