Skip to content

Commit

Permalink
feat: server and location support grpc web module
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Nov 10, 2024
1 parent 7199e3c commit e353aa6
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/config/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ pub struct LocationConf {
pub plugins: Option<Vec<String>>,
pub client_max_body_size: Option<ByteSize>,
pub includes: Option<Vec<String>>,
pub grpc_web: Option<bool>,
pub remark: Option<String>,
}

Expand Down Expand Up @@ -427,6 +428,7 @@ pub struct ServerConf {
pub prometheus_metrics: Option<String>,
pub otlp_exporter: Option<String>,
pub includes: Option<Vec<String>>,
pub modules: Option<Vec<String>>,
pub remark: Option<String>,
}

Expand Down
2 changes: 2 additions & 0 deletions src/proxy/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub struct Location {
pub accepted: AtomicU64,
pub processing: AtomicI32,
pub upstream: String,
pub grpc_web: bool,
client_max_body_size: usize,
}

Expand Down Expand Up @@ -212,6 +213,7 @@ impl Location {
plugins: conf.plugins.clone(),
accepted: AtomicU64::new(0),
processing: AtomicI32::new(0),
grpc_web: conf.grpc_web.unwrap_or_default(),
proxy_add_headers: format_headers(&conf.proxy_add_headers)?,
proxy_set_headers: format_headers(&conf.proxy_set_headers)?,
client_max_body_size: conf
Expand Down
29 changes: 29 additions & 0 deletions src/proxy/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ use pingora::cache::{
use pingora::http::{RequestHeader, ResponseHeader};
use pingora::listeners::TcpSocketOptions;
use pingora::modules::http::compression::ResponseCompression;
use pingora::modules::http::grpc_web::{GrpcWeb, GrpcWebBridge};
use pingora::modules::http::HttpModules;
use pingora::protocols::http::error_resp;
use pingora::protocols::Digest;
use pingora::protocols::TimingDigest;
Expand Down Expand Up @@ -154,6 +156,7 @@ pub struct Server {
prometheus_metrics: String,
#[cfg(feature = "full")]
enabled_otel: bool,
modules: Option<Vec<String>>,
}

pub struct ServerServices {
Expand Down Expand Up @@ -219,6 +222,7 @@ impl Server {
prometheus_metrics,
#[cfg(feature = "full")]
prometheus,
modules: conf.modules.clone(),
};
Ok(s)
}
Expand Down Expand Up @@ -420,6 +424,16 @@ impl ProxyHttp for Server {
fn new_ctx(&self) -> Self::CTX {
State::new()
}
fn init_downstream_modules(&self, modules: &mut HttpModules) {
let Some(value) = &self.modules else {
return;
};
for item in value.iter() {
if item == "grpc-web" {
modules.add_module(Box::new(GrpcWeb));
}
}
}
async fn early_request_filter(
&self,
session: &mut Session,
Expand Down Expand Up @@ -516,6 +530,21 @@ impl ProxyHttp for Server {
}
}
if let Some(location) = &ctx.location {
if location.grpc_web {
// Initialize gRPC module for this request
let grpc = session
.downstream_modules_ctx
.get_mut::<GrpcWebBridge>()
.ok_or_else(|| {
util::new_internal_error(
500,
"grpc web bridge module should be added"
.to_string(),
)
})?;
grpc.init();
}

ctx.location_accepted =
location.accepted.fetch_add(1, Ordering::Relaxed) + 1;
ctx.location_processing =
Expand Down
2 changes: 2 additions & 0 deletions src/proxy/server_conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct ServerConf {
pub enabled_h2: bool,
pub prometheus_metrics: Option<String>,
pub otlp_exporter: Option<String>,
pub modules: Option<Vec<String>>,
}

impl fmt::Display for ServerConf {
Expand Down Expand Up @@ -107,6 +108,7 @@ impl From<PingapConf> for Vec<ServerConf> {
tcp_fastopen: item.tcp_fastopen,
prometheus_metrics: item.prometheus_metrics,
otlp_exporter: item.otlp_exporter.clone(),
modules: item.modules.clone(),
error_template,
});
}
Expand Down
3 changes: 3 additions & 0 deletions web/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export default {
accessLog: "Access Log Format",
accessLogPlaceholder: "Input the format layout for access",
enabledH2: "Enable Http2(h2c)",
modules: "Http Modules",
moduleslaceholder: "Select http modules for server",
tlsCipherList: "Tls Cipher List",
tlsCipherListPlaceholder:
"Input the cipher list for protocols before tlsv1.3",
Expand Down Expand Up @@ -170,6 +172,7 @@ export default {
clientMaxBodySizePlaceholder: "Input the max body size(e.g. 1mb)",
plugins: "Plugins",
pluginsPlaceholder: "Select the plugins for location",
grpcWeb: "Grpc Web",
remark: "Remark",
},
upstream: {
Expand Down
3 changes: 3 additions & 0 deletions web/src/i18n/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export default {
accessLog: "访问日志格式化",
accessLogPlaceholder: "输入日志格式化模板",
enabledH2: "启用http2(h2c)",
modules: "Http模块",
moduleslaceholder: "选择要使用的http模块",
tlsCipherList: "tls密码套件列表",
tlsCipherListPlaceholder: "输入tls密码套件列表,早于tls1.3版本认证使用",
tlsCiphersuites: "tls密码套件列表",
Expand Down Expand Up @@ -162,6 +164,7 @@ export default {
clientMaxBodySizePlaceholder: "输入请求实体限制大小(如1mb)",
plugins: "插件列表",
pluginsPlaceholder: "选择location使用的相关插件",
grpcWeb: "Grpc Web",
remark: "备注",
},
upstream: {
Expand Down
17 changes: 15 additions & 2 deletions web/src/pages/Locations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import React from "react";
import { ExForm, ExFormItem } from "@/components/ex-form";
import { pascal } from "radash";
import { z } from "zod";
import { ExFormItemCategory, newStringOptions } from "@/constants";
import {
ExFormItemCategory,
newBooleanOptions,
newStringOptions,
} from "@/constants";
import { formatLabel, newZodBytes, omitEmptyArray } from "@/helpers/util";
import { useSearchParams } from "react-router-dom";
import { useEffect } from "react";
Expand Down Expand Up @@ -132,10 +136,19 @@ export default function Locations() {
label: i18n("includes"),
placeholder: i18n("includesPlaceholder"),
defaultValue: locationConfig.includes,
span: 6,
span: 3,
category: ExFormItemCategory.MULTI_SELECT,
options: newStringOptions(getIncludeOptions(), false),
},
{
name: "grpc_web",
label: locationI18n("grpcWeb"),
placeholder: "",
defaultValue: locationConfig.grpc_web,
span: 3,
category: ExFormItemCategory.RADIOS,
options: newBooleanOptions(),
},
{
name: "weight",
label: locationI18n("weight"),
Expand Down
11 changes: 10 additions & 1 deletion web/src/pages/Servers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,21 @@ export default function Servers() {
category: ExFormItemCategory.RADIOS,
options: newBooleanOptions(),
},
{
name: "modules",
label: serverI18n("modules"),
placeholder: serverI18n("moduleslaceholder"),
defaultValue: serverConfig.modules,
span: 3,
category: ExFormItemCategory.MULTI_SELECT,
options: newStringOptions(["grpc-web"], false),
},
{
name: "includes",
label: i18n("includes"),
placeholder: i18n("includesPlaceholder"),
defaultValue: serverConfig.includes,
span: 3,
span: 6,
category: ExFormItemCategory.MULTI_SELECT,
options: newStringOptions(getIncludeOptions(), false),
},
Expand Down
2 changes: 2 additions & 0 deletions web/src/states/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface Location {
client_max_body_size?: string;
plugins?: string[];
includes?: string[];
grpc_web?: boolean;
remark?: string;
}

Expand Down Expand Up @@ -85,6 +86,7 @@ export interface Server {
prometheus_metrics?: string;
otlp_exporter?: string;
includes?: string[];
modules?: string[];
remark?: string;
}

Expand Down

0 comments on commit e353aa6

Please sign in to comment.