From 07cfae0d89ba2285060c9749a357604a312e3761 Mon Sep 17 00:00:00 2001 From: w-sodalite Date: Fri, 19 Jan 2024 14:32:33 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=80=E6=9C=89?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E7=9A=84=E4=BD=BF=E7=94=A8=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 116 ++++++++++-------- docs/discovery/builtin.md | 35 ++++++ docs/layers/concurrency_limit.md | 35 ++++++ docs/layers/cors.md | 43 +++++++ docs/layers/keep_host_header.md | 31 +++++ docs/layers/path_strip.md | 35 ++++++ docs/layers/rate_limit.md | 43 +++++++ docs/layers/request_body_limit.md | 35 ++++++ docs/layers/rewrite_path.md | 39 ++++++ docs/layers/set_request_header.md | 39 ++++++ docs/layers/set_response_header.md | 39 ++++++ docs/layers/set_status.md | 35 ++++++ docs/layers/x_forward.md | 35 ++++++ docs/load_balance/ip_hash.md | 67 ++++++++++ docs/load_balance/random.md | 61 +++++++++ docs/load_balance/sequential.md | 61 +++++++++ docs/load_balance/stand_by.md | 61 +++++++++ docs/load_balance/weight.md | 70 +++++++++++ docs/matchers/cookie.md | 42 +++++++ docs/matchers/header.md | 42 +++++++ docs/matchers/host.md | 39 ++++++ docs/matchers/method.md | 37 ++++++ docs/matchers/path.md | 40 ++++++ docs/matchers/pattern.md | 53 ++++++++ docs/matchers/query.md | 42 +++++++ docs/matchers/remote_addr.md | 39 ++++++ docs/matchers/time.md | 39 ++++++ docs/satex.md | 0 docs/services/echo.md | 28 +++++ docs/services/proxy.md | 32 +++++ docs/services/static.md | 32 +++++ examples/discovery/builtin.yaml | 21 ---- examples/discovery/load_balance/ip_hash.yaml | 36 ------ examples/discovery/load_balance/random.yaml | 21 ---- .../discovery/load_balance/sequential.yaml | 21 ---- examples/discovery/load_balance/standby.yaml | 21 ---- examples/discovery/load_balance/weight.yaml | 21 ---- examples/layer/cors.yaml | 32 ----- examples/layer/keep_host_header.yaml | 17 --- examples/layer/path_strip.yaml | 23 ---- examples/layer/rate_limit.yaml | 29 ----- examples/layer/rewrite_path.yaml | 23 ---- examples/layer/set_header.yaml | 26 ---- examples/layer/x_forward.yaml | 17 --- examples/matcher/cookie.yaml | 26 ---- examples/matcher/header.yaml | 23 ---- examples/matcher/host.yaml | 22 ---- examples/matcher/method.yaml | 20 --- examples/matcher/path.yaml | 26 ---- examples/matcher/query.yaml | 23 ---- examples/matcher/remote_addr.yaml | 23 ---- examples/matcher/time.yaml | 20 --- examples/satex.yaml | 5 +- examples/service/echo.yaml | 14 --- examples/service/proxy.yaml | 20 --- examples/service/static.yaml | 7 -- examples/static.yaml | 12 ++ examples/tls/{tls.yaml => static.yaml} | 13 +- satex-discovery/src/make/builtin/make.rs | 13 +- satex-discovery/src/make/builtin/mod.rs | 4 +- satex-layer/src/make/request_body_limit.rs | 4 +- satex-matcher/src/make/host/make.rs | 13 +- satex-matcher/src/make/remote_addr/make.rs | 7 +- satex-matcher/src/make/remote_addr/mod.rs | 2 +- satex-service/src/make/static.rs | 4 +- 65 files changed, 1337 insertions(+), 617 deletions(-) create mode 100644 docs/discovery/builtin.md create mode 100644 docs/layers/concurrency_limit.md create mode 100644 docs/layers/cors.md create mode 100644 docs/layers/keep_host_header.md create mode 100644 docs/layers/path_strip.md create mode 100644 docs/layers/rate_limit.md create mode 100644 docs/layers/request_body_limit.md create mode 100644 docs/layers/rewrite_path.md create mode 100644 docs/layers/set_request_header.md create mode 100644 docs/layers/set_response_header.md create mode 100644 docs/layers/set_status.md create mode 100644 docs/layers/x_forward.md create mode 100644 docs/load_balance/ip_hash.md create mode 100644 docs/load_balance/random.md create mode 100644 docs/load_balance/sequential.md create mode 100644 docs/load_balance/stand_by.md create mode 100644 docs/load_balance/weight.md create mode 100644 docs/matchers/cookie.md create mode 100644 docs/matchers/header.md create mode 100644 docs/matchers/host.md create mode 100644 docs/matchers/method.md create mode 100644 docs/matchers/path.md create mode 100644 docs/matchers/pattern.md create mode 100644 docs/matchers/query.md create mode 100644 docs/matchers/remote_addr.md create mode 100644 docs/matchers/time.md create mode 100644 docs/satex.md create mode 100644 docs/services/echo.md create mode 100644 docs/services/proxy.md create mode 100644 docs/services/static.md delete mode 100644 examples/discovery/builtin.yaml delete mode 100644 examples/discovery/load_balance/ip_hash.yaml delete mode 100644 examples/discovery/load_balance/random.yaml delete mode 100644 examples/discovery/load_balance/sequential.yaml delete mode 100644 examples/discovery/load_balance/standby.yaml delete mode 100644 examples/discovery/load_balance/weight.yaml delete mode 100644 examples/layer/cors.yaml delete mode 100644 examples/layer/keep_host_header.yaml delete mode 100644 examples/layer/path_strip.yaml delete mode 100644 examples/layer/rate_limit.yaml delete mode 100644 examples/layer/rewrite_path.yaml delete mode 100644 examples/layer/set_header.yaml delete mode 100644 examples/layer/x_forward.yaml delete mode 100644 examples/matcher/cookie.yaml delete mode 100644 examples/matcher/header.yaml delete mode 100644 examples/matcher/host.yaml delete mode 100644 examples/matcher/method.yaml delete mode 100644 examples/matcher/path.yaml delete mode 100644 examples/matcher/query.yaml delete mode 100644 examples/matcher/remote_addr.yaml delete mode 100644 examples/matcher/time.yaml delete mode 100644 examples/service/echo.yaml delete mode 100644 examples/service/proxy.yaml delete mode 100644 examples/service/static.yaml create mode 100644 examples/static.yaml rename examples/tls/{tls.yaml => static.yaml} (52%) diff --git a/README.md b/README.md index e80fd66..1cb313c 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,16 @@ [![Build Status](https://github.com/w-sodalite/satex/actions/workflows/rust.yml/badge.svg?branch=master)](https://github.com/w-sodalite/satex/actions/workflows/rust.yml) [![Crates.io](https://img.shields.io/crates/v/satex)](https://crates.io/crates/satex) -## 优势 +## 特点 - 纯异步IO实现 - 丰富的路由组件以及灵活的路由配置 - 内置服务发现、健康检查以及多种负载均衡策略 - 完全兼容[tower](https://crates.io/crates/tower)和[tower-http](https://crates.io/crates/tower-http)的生态,包括中间件、服务和工具等。 -## 路由结构 +## 路由 + +### 逻辑结构 - **matcher**:负责接收并解析进入的HTTP请求。通过精确匹配算法,它能快速地识别出请求的目标路径、方法以及其他相关参数。 @@ -21,66 +23,78 @@ - **service**:负责处理经过`layer`层筛选的请求。你可以在这一层实现具体的业务逻辑,例如调用后端服务、处理数据等。 -## 内置组件 - -- 路由组件 - - - **Matcher** - * `Method`:根据请求方法(如GET、POST等)进行匹配。 - * `Query`:根据请求的查询参数进行匹配。 - * `Header`:根据请求头信息进行匹配。 - * `Host`:根据请求的主机名进行匹配。 - * `Path`:根据请求路径进行匹配,使用[`path-tree`](https://docs.rs/path-tree)进行处理,支持变量绑定。 - * `RemoteAddr`:根据客户端的IP地址进行匹配。 - * `Cookie`: 根据请求的Cookie进行匹配。 - * `Time`:根据请求时间进行匹配。 - - - **Layer** - * `Cors`:处理跨域请求。 - * `KeepHostHeader`:保持原始的Host请求头。 - * `PathStrip`:从请求路径中删除或替换特定部分。 - * `RateLimit`:限制来自单个IP的请求频率。 - * `SetHeader`:设置请求头和响应头信息。 - * `ConcurrentcyLimit`:限制同时处理的请求数量。 - * `RequestBodyLimit`:限制请求体的最大大小。 - * `SetStatus`:设置响应状态码。 - * `RewritePath`: 重写请求的接口地址,支持使用`Path`匹配路由绑定的变量。 - - - **Service** - * `Echo`:简单地返回接收到的请求内容。 - * `Static`:提供静态文件服务。 - * `Proxy`:反向代理服务,代理请求到另一个服务或地址。 - - -- 反向代理支持 - - - 服务发现(Discovery) - * `Builtin`:内置的服务发现,通过配置的方式注册服务集合,内部会定时检测服务节点的可用性。 - - - 负载均衡(LoadBalance) - * `IpHash`:IP哈希负载策略使用客户端的IP地址进行哈希计算,根据哈希值将请求分配给后端服务器。这样可以确保来自同一IP地址的请求始终被发送到同一台服务器,这有助于保持会话和状态信息的持续性。 - * `Random`:随机负载策略随机选择一台服务器将请求发送过去。这种策略简单且易于实现,适用于没有特殊需求的情况。 - * `Sequential`:顺序负载策略按照服务器列表的顺序依次将请求发送过去。这种策略适用于服务器性能基本一致的情况。 - * `StandBy`:备用负载策略在主服务器故障时,将请求切换到备用服务器上。这种策略可以提高系统的可用性和可靠性。 - * `Weight`:权重负载策略根据服务器的性能或权重值来分配请求。权重值高的服务器将获得更多的请求,而权重值低的服务器将获得较少的请求。这种策略可以帮助平衡服务器的负载,提高系统的性能和效率。 - -## 配置示例 - -[examples](./examples) +### 内置组件 + +- #### Matcher + + | 名称 | 描述 | 使用文档 | + |:-------------|------------------------|:----------------------------------:| + | `Method` | 根据请求方法(如GET、POST等)进行匹配 | [文档](docs/matchers/method.md) | + | `Query` | 根据请求的查询参数进行匹配 | [文档](docs/matchers/query.md) | + | `Header` | 根据请求头信息进行匹配 | [文档](docs/matchers/header.md) | + | `Host` | 根据请求的主机名进行匹配 | [文档](docs/matchers/host.md) | + | `Path` | 根据请求路径进行匹配 | [文档](docs/matchers/path.md) | + | `RemoteAddr` | 根据客户端的IP地址进行匹配 | [文档](docs/matchers/remote_addr.md) | + | `Cookie` | 根据请求的Cookie进行匹配 | [文档](docs/matchers/cookie.md) | + | `Time` | 根据请求时间进行匹配 | [文档](docs/matchers/time.md) | + +- #### Layer + + | 名称 | 描述 | 使用文档 | + |:--------------------|--------------|:----------------------------------:| + | `Cors` | 处理跨域请求 | [文档](docs/layers/cors.md) | + | `KeepHostHeader` | 保持原始的Host请求头 | [文档](docs/matchers/query.md) | + | `PathStrip` | 从请求路径中删除特定部分 | [文档](docs/matchers/header.md) | + | `RateLimit` | 限制请求频率 | [文档](docs/matchers/host.md) | + | `RewritePath` | 重写请求的接口地址 | [文档](docs/matchers/time.md) | + | `SetRequestHeader` | 设置请求头信息 | [文档](docs/matchers/path.md) | + | `SetResponseHeader` | 设置响应头信息 | [文档](docs/matchers/path.md) | + | `XForward` | 设置XForward信息 | [文档](docs/matchers/path.md) | + | `ConcurrentcyLimit` | 限制同时处理的请求数量 | [文档](docs/matchers/remote_addr.md) | + | `RequestBodyLimit` | 限制请求体的最大大小 | [文档](docs/matchers/cookie.md) | + | `SetStatus` | 设置响应状态码 | [文档](docs/matchers/time.md) | + +- #### Service + + | 名称 | 描述 | 使用文档 | + |:---------|-----------------------|:-----------------------------:| + | `Echo` | 简单地返回接收到的请求内容 | [文档](docs/services/echo.md) | + | `Static` | 提供静态文件服务 | [文档](docs/services/static.md) | + | `Proxy` | 反向代理服务,代理请求到另一个服务或地址。 | [文档](docs/services/proxy.md) | + +- #### Discovery + + | 名称 | 描述 | 使用文档 | + |:----------|----------------------------------------|:-------------------------------:| + | `Builtin` | 内置的服务发现,通过配置的方式注册服务集合,内部会定时检测服务节点的可用性。 | [文档](docs/discovery/builtin.md) | + +- #### LoadBalance + + | 名称 | 描述 | 使用文档 | + |:-------------|--------------------------------------------------------------------------------------------|:-------------------------------------:| + | `IpHash` | IP哈希负载策略使用客户端的IP地址进行哈希计算,根据哈希值将请求分配给后端服务器。这样可以确保来自同一IP地址的请求始终被发送到同一台服务器,这有助于保持会话和状态信息的持续性。 | [文档](docs/load_balance/ip_hash.md) | + | `Random` | 随机负载策略随机选择一台服务器将请求发送过去。这种策略简单且易于实现,适用于没有特殊需求的情况。 | [文档](docs/load_balance/random.md) | + | `Sequential` | 顺序负载策略按照服务器列表的顺序依次将请求发送过去。这种策略适用于服务器性能基本一致的情况。 | [文档](docs/load_balance/sequential.md) | + | `StandBy` | 备用负载策略在主服务器故障时,将请求切换到备用服务器上。这种策略可以提高系统的可用性和可靠性。 | [文档](docs/load_balance/stand_by.md) | + | `Weight` | 权重负载策略根据服务器的性能或权重值来分配请求。权重值高的服务器将获得更多的请求,而权重值低的服务器将获得较少的请求。这种策略可以帮助平衡服务器的负载,提高系统的性能和效率。 | [文档](docs/load_balance/weight.md) | + +## 配置文件 + +应用启动配置文件为`satex.yaml`,具体内容见[示例配置](examples/satex.yaml)。 ## 构建和启动 -- ### 构建 +- ### 1.构建 - 安装`RUST`环境,按照[官方文档](https://www.rust-lang.org/zh-CN/learn/get-started)初始化环境。 - > 国内可以使用[rsproxy](https://rsproxy.cn/#getStarted)镜像加速下载。 + > 国内可以使用[rsproxy](https://rsproxy.cn/#getStarted)镜像加速下载。 - 使用`cargo`安装,执行命令:`cargo install satex`即可安装最新版本的`satex`。 - 或者使用源码进行构建,下载源码到本地,在根目录执行`cargo build --release`,编译成功后在`target/release` 目录下可以找到`satex`。 -- ### 启动 +- ### 2.启动 > 使用`-c`指定配置文件`satex.yaml`的路径 diff --git a/docs/discovery/builtin.md b/docs/discovery/builtin.md new file mode 100644 index 0000000..3fccfdf --- /dev/null +++ b/docs/discovery/builtin.md @@ -0,0 +1,35 @@ +# Builtin + +> 内置的服务发现,通过配置的方式注册服务集合,内部会定时检测服务节点的健康状态。 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:-------------|:-------:|:----:|:---:|:--------------------| +| server | `str` | `Y` | | 服务名称 | +| uris | `[str]` | `Y` | | 服务节点列表 | +| interval | `u32` | `Y` | | 服务节点健康状态检查间隔时间,单位:秒 | +| load_balance | `str` | `Y` | | 负载均衡策略 | + +## 配置示例 + +```yaml +server: + port: 9000 + +discovery: + - kind: Builtin + args: + server: server1 + uris: + - 127.0.0.1:9000 + - 127.0.0.1:9001 + - 127.0.0.1:9002 + interval: 10 + load_balance: Random + +router: + routes: + - id: discovery-route + service: Proxy=http://server1 +``` \ No newline at end of file diff --git a/docs/layers/concurrency_limit.md b/docs/layers/concurrency_limit.md new file mode 100644 index 0000000..047a4e1 --- /dev/null +++ b/docs/layers/concurrency_limit.md @@ -0,0 +1,35 @@ +# ConcurrencyLimit + +> 限制请求的同时并发数量 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:----|:-----:|:----:|:---:|:----------| +| max | `u32` | `Y` | | 允许的最大并发数量 | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: concurrency-limit-shortcut + layers: + - ConcurrencyLimit=10 + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: concurrency-limit-complete + layers: + - kind: ConcurrencyLimit + args: + max: 10 + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/layers/cors.md b/docs/layers/cors.md new file mode 100644 index 0000000..12c36fb --- /dev/null +++ b/docs/layers/cors.md @@ -0,0 +1,43 @@ +# Cors + +> 用于处理跨域请求 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:----------------------|:-------:|:----:|:-----:|:-----------| +| allow_credentials | `bool` | `N` | false | 是否允许携带认证信息 | +| allow_headers | `[str]` | `N` | [] | 允许携带的头 | +| allow_methods | `[str]` | `N` | [GET] | 允许请求的方法 | +| allow_origin | `[str]` | `N` | [] | 允许的域 | +| allow_private_network | `bool` | `N` | false | 是否允许私有网络 | +| expose_headers | `[str]` | `N` | [] | 允许返回的头 | +| max_age | `u32` | `N` | 0 | 最大有效期 | +| vary | `[str]` | `N` | [] | vary列表 | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: cors-shortcut + layers: + - Cors + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: rewrite-path-complete + layers: + - kind: Cors + args: + allow_credentials: false + allow_origin: '*' + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/layers/keep_host_header.md b/docs/layers/keep_host_header.md new file mode 100644 index 0000000..411bb17 --- /dev/null +++ b/docs/layers/keep_host_header.md @@ -0,0 +1,31 @@ +# KeepHostHeader + +> 通用用在反向代理时,转发到后端的请求仍然保持客户端传递的`Host`信息。 + +## 参数 + +无 + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: cors-shortcut + layers: + - KeepHostHeader + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: rewrite-path-complete + layers: + - kind: KeepHostHeader + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/layers/path_strip.md b/docs/layers/path_strip.md new file mode 100644 index 0000000..9d73e4e --- /dev/null +++ b/docs/layers/path_strip.md @@ -0,0 +1,35 @@ +# PathStrip + +> 从请求路径中删除特定部分 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:------|:-----:|:----:|:---:|:------| +| level | `u32` | `Y` | | 截取的级别 | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: path-strip-shortcut + layers: + - PathStrip=1 + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: rewrite-path-complete + layers: + - kind: PathStrip + args: + level: 1 + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/layers/rate_limit.md b/docs/layers/rate_limit.md new file mode 100644 index 0000000..5c86bc4 --- /dev/null +++ b/docs/layers/rate_limit.md @@ -0,0 +1,43 @@ +# RateLimit + +> 使用令牌桶的来限制请求的速率 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:---------|:------:|:----:|:-----:|:------------------------------------------------------| +| policy | `str` | `Y` | | 超过限制后的策略:`Await`:超过限制后等待直到重新填充。`Break`:超过限制后直接中断请求失败。 | +| max | `u32` | `Y` | | 最大Token数 | +| refill | `u32` | `Y` | | 重新填充的Token数 | +| interval | `u32` | `N` | 1 | 填充Token间隔时间,单位:秒。 | +| fair | `bool` | `N` | false | 是否使用公平模式 | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: rate-limit-shortcut + layers: + - RateLimit=Await,10,10,1,false + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: rate-limit-complete + layers: + - kind: RateLimit + args: + policy: Await + max: 10 + refill: 10 + interval: 1 + fair: false + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/layers/request_body_limit.md b/docs/layers/request_body_limit.md new file mode 100644 index 0000000..18ca50b --- /dev/null +++ b/docs/layers/request_body_limit.md @@ -0,0 +1,35 @@ +# RequestBodyLimit + +> 限制请求的包体大小 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:----|:-----:|:----:|:---:|:----------------| +| max | `u32` | `Y` | | 允许的最大包体大小,单位:字节 | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: request-body-limit-shortcut + layers: + - RequestBodyLimit=10240 + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: request-body-limit-complete + layers: + - kind: RequestBodyLimit + args: + max: 10240 + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/layers/rewrite_path.md b/docs/layers/rewrite_path.md new file mode 100644 index 0000000..73b3d82 --- /dev/null +++ b/docs/layers/rewrite_path.md @@ -0,0 +1,39 @@ +# RewritePath + +> 用于重写请求接口的URL地址,支持使用在[`Matcher - Path`](../matchers/path.md)中绑定的变量。 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:-----|:-------:|:----:|:---:|:------| +| path | `[str]` | `Y` | | 重写的路径 | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: rewrite-path-shortcut + matchers: + - Path=/a/:x+ + layers: + - RewritePath=/b/{x} + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: rewrite-path-complete + matchers: + - Path=/a/:x+ + layers: + - kind: RewritePath + args: + path: /b/{x} + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/layers/set_request_header.md b/docs/layers/set_request_header.md new file mode 100644 index 0000000..2eec2f4 --- /dev/null +++ b/docs/layers/set_request_header.md @@ -0,0 +1,39 @@ +# SetRequestHeader + +> 为请求添加特定的请求头信息 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:------|:-----:|:----:|:--------:|:-------------------------------------------| +| name | `str` | `Y` | | 请求头名称 | +| value | `str` | `Y` | | 请求头值 | +| mode | `str` | `N` | Override | 设置模式,支持:`Append`、`IfNotPresent`、`Override` | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: set-request-header-shortcut + layers: + - SetRequestHeader=k1,v1,IfNotPresent + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: set-request-header-complete + layers: + - kind: SetRequestHeader + args: + name: k1 + value: v1 + mode: IfNotPresent + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/layers/set_response_header.md b/docs/layers/set_response_header.md new file mode 100644 index 0000000..9abbac7 --- /dev/null +++ b/docs/layers/set_response_header.md @@ -0,0 +1,39 @@ +# SetResponseHeader + +> 为响应添加特定的响应头信息 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:------|:-----:|:----:|:--------:|:-------------------------------------------| +| name | `str` | `Y` | | 请求头名称 | +| value | `str` | `Y` | | 请求头值 | +| mode | `str` | `N` | Override | 设置模式,支持:`Append`、`IfNotPresent`、`Override` | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: set-response-header-shortcut + layers: + - SetResponseHeader=k1,v1,IfNotPresent + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: set-response-header-complete + layers: + - kind: SetResponseHeader + args: + name: k1 + value: v1 + mode: IfNotPresent + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/layers/set_status.md b/docs/layers/set_status.md new file mode 100644 index 0000000..3c74c91 --- /dev/null +++ b/docs/layers/set_status.md @@ -0,0 +1,35 @@ +# SetStatus + +> 设置响应的状态码 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:-------|:-----:|:----:|:---:|:----| +| status | `u16` | `Y` | | 状态码 | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: set-status-shortcut + layers: + - SetStatus=404 + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: request-body-limit-complete + layers: + - kind: SetStatus + args: + status: 404 + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/layers/x_forward.md b/docs/layers/x_forward.md new file mode 100644 index 0000000..61f2e10 --- /dev/null +++ b/docs/layers/x_forward.md @@ -0,0 +1,35 @@ +# XForward + +> 用于反向代理中添加`x-forward`请求头,保留请求经过的代理节点信息。 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:-----|:-----:|:----:|:--------:|:----------------------------| +| mode | `str` | `N` | Override | 设置模式,支持:`Append`、`Override` | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: x-forward-shortcut + layers: + - XForward + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: x-forward-complete + layers: + - kind: XForward + args: + mode: Append + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/load_balance/ip_hash.md b/docs/load_balance/ip_hash.md new file mode 100644 index 0000000..ba74c6f --- /dev/null +++ b/docs/load_balance/ip_hash.md @@ -0,0 +1,67 @@ +# IpHash + +> IP哈希负载策略使用客户端的IP地址进行哈希计算,根据哈希值将请求分配给后端服务器。这样可以确保来自同一IP地址的请求始终被发送到同一台服务器,这有助于保持会话和状态信息的持续性。 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:---------|:-----:|:----:|:----:|:---------------------| +| timeout | `u32` | `N` | 1800 | IP对应的节点缓存的超时时间,单位:秒 | +| interval | `u32` | `N` | 10 | 清理超时的IP节点缓存间隔时间,单位:秒 | + +## 配置示例 + +- **简单模式** + +## 配置示例 + +- ### 简单模式 + + ```yaml + server: + port: 9000 + + discovery: + - kind: Builtin + args: + server: server1 + uris: + - 127.0.0.1:9000 + - 127.0.0.1:9001 + - 127.0.0.1:9002 + interval: 10 + load_balance: IpHash=1800,10 + + router: + routes: + - id: ip-hash-route + service: Proxy=http://server1 + ``` + + +- ### 完整模式 + + ```yaml + server: + port: 9000 + + discovery: + - kind: Builtin + args: + server: server1 + uris: + - 127.0.0.1:9000 + - 127.0.0.1:9001 + - 127.0.0.1:9002 + interval: 10 + load_balance: + kind: IpHash + args: + timeout: 1800 + interval: 10 + + router: + routes: + - id: ip-hash-route + service: Proxy=http://server1 + ``` \ No newline at end of file diff --git a/docs/load_balance/random.md b/docs/load_balance/random.md new file mode 100644 index 0000000..cf0ce4d --- /dev/null +++ b/docs/load_balance/random.md @@ -0,0 +1,61 @@ +# IpHash + +> 随机负载策略随机选择一台服务器将请求发送过去。这种策略简单且易于实现,适用于没有特殊需求的情况。 + +## 参数 + +无 + +## 配置示例 + +- **简单模式** + +## 配置示例 + +- ### 简单模式 + + ```yaml + server: + port: 9000 + + discovery: + - kind: Builtin + args: + server: server1 + uris: + - 127.0.0.1:9000 + - 127.0.0.1:9001 + - 127.0.0.1:9002 + interval: 10 + load_balance: Random + + router: + routes: + - id: random-route + service: Proxy=http://server1 + ``` + + +- ### 完整模式 + + ```yaml + server: + port: 9000 + + discovery: + - kind: Builtin + args: + server: server1 + uris: + - 127.0.0.1:9000 + - 127.0.0.1:9001 + - 127.0.0.1:9002 + interval: 10 + load_balance: + kind: Random + + router: + routes: + - id: random-route + service: Proxy=http://server1 + ``` \ No newline at end of file diff --git a/docs/load_balance/sequential.md b/docs/load_balance/sequential.md new file mode 100644 index 0000000..8a2d2c9 --- /dev/null +++ b/docs/load_balance/sequential.md @@ -0,0 +1,61 @@ +# Sequential + +> 顺序负载策略按照服务器列表的顺序依次将请求发送过去。这种策略适用于服务器性能基本一致的情况。 + +## 参数 + +无 + +## 配置示例 + +- **简单模式** + +## 配置示例 + +- ### 简单模式 + + ```yaml + server: + port: 9000 + + discovery: + - kind: Builtin + args: + server: server1 + uris: + - 127.0.0.1:9000 + - 127.0.0.1:9001 + - 127.0.0.1:9002 + interval: 10 + load_balance: Sequential + + router: + routes: + - id: sequential-route + service: Proxy=http://server1 + ``` + + +- ### 完整模式 + + ```yaml + server: + port: 9000 + + discovery: + - kind: Builtin + args: + server: server1 + uris: + - 127.0.0.1:9000 + - 127.0.0.1:9001 + - 127.0.0.1:9002 + interval: 10 + load_balance: + kind: Sequential + + router: + routes: + - id: sequential-route + service: Proxy=http://server1 + ``` \ No newline at end of file diff --git a/docs/load_balance/stand_by.md b/docs/load_balance/stand_by.md new file mode 100644 index 0000000..dba2159 --- /dev/null +++ b/docs/load_balance/stand_by.md @@ -0,0 +1,61 @@ +# StandBy + +> 备用负载策略在主服务器故障时,将请求切换到备用服务器上。这种策略可以提高系统的可用性和可靠性。 + +## 参数 + +无 + +## 配置示例 + +- **简单模式** + +## 配置示例 + +- ### 简单模式 + + ```yaml + server: + port: 9000 + + discovery: + - kind: Builtin + args: + server: server1 + uris: + - 127.0.0.1:9000 + - 127.0.0.1:9001 + - 127.0.0.1:9002 + interval: 10 + load_balance: StandBy + + router: + routes: + - id: stand-by-route + service: Proxy=http://server1 + ``` + + +- ### 完整模式 + + ```yaml + server: + port: 9000 + + discovery: + - kind: Builtin + args: + server: server1 + uris: + - 127.0.0.1:9000 + - 127.0.0.1:9001 + - 127.0.0.1:9002 + interval: 10 + load_balance: + kind: StandBy + + router: + routes: + - id: stand-by-route + service: Proxy=http://server1 + ``` \ No newline at end of file diff --git a/docs/load_balance/weight.md b/docs/load_balance/weight.md new file mode 100644 index 0000000..dd83c35 --- /dev/null +++ b/docs/load_balance/weight.md @@ -0,0 +1,70 @@ +# IpHash + +> IP哈希负载策略使用客户端的IP地址进行哈希计算,根据哈希值将请求分配给后端服务器。这样可以确保来自同一IP地址的请求始终被发送到同一台服务器,这有助于保持会话和状态信息的持续性。 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:-------|:-------:|:----:|:---:|:----------| +| ratios | `[u32]` | `Y` | | 所有节点的权重信息 | + +备注:这里权重信息的个数应该和节点个数保持一致。 + +## 配置示例 + +- **简单模式** + +## 配置示例 + +- ### 简单模式 + + ```yaml + server: + port: 9000 + + discovery: + - kind: Builtin + args: + server: server1 + uris: + - 127.0.0.1:9000 + - 127.0.0.1:9001 + - 127.0.0.1:9002 + interval: 10 + load_balance: Weight=1,1,2 + + router: + routes: + - id: ip-hash-route + service: Proxy=http://server1 + ``` + + +- ### 完整模式 + + ```yaml + server: + port: 9000 + + discovery: + - kind: Builtin + args: + server: server1 + uris: + - 127.0.0.1:9000 + - 127.0.0.1:9001 + - 127.0.0.1:9002 + interval: 10 + load_balance: + kind: Weight + args: + ratios: + - 1 + - 1 + - 2 + + router: + routes: + - id: ip-hash-route + service: Proxy=http://server1 + ``` \ No newline at end of file diff --git a/docs/matchers/cookie.md b/docs/matchers/cookie.md new file mode 100644 index 0000000..f63dd6a --- /dev/null +++ b/docs/matchers/cookie.md @@ -0,0 +1,42 @@ +# Cookie + +> 根据请求`Cookie`中的值来匹配是否需要使用此路由 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:-------|-------------|:----:|:---:|:---------| +| name | `str` | `Y` | | Cookie名称 | +| values | `[pattern]` | `Y` | | Cookie值 | + +备注:[`pattern`类型说明](./pattern.md) + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: cookie-shortcut + matchers: + - Cookie=k1,Exact,v1 + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: cookie-complete + matchers: + - kind: Cookie + args: + name: k1 + values: + - mode: Exact + value: v1 + sensitive: false + service: Static=examples/resources + ``` diff --git a/docs/matchers/header.md b/docs/matchers/header.md new file mode 100644 index 0000000..5b1644a --- /dev/null +++ b/docs/matchers/header.md @@ -0,0 +1,42 @@ +# Header + +> 根据请求`Header`中的值来匹配是否需要使用此路由 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:-------|-------------|:----:|:---:|:------| +| name | `str` | `Y` | | 请求头名称 | +| values | `[pattern]` | `Y` | | 请求头值 | + +备注:[`pattern`类型说明](./pattern.md) + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: header-shortcut + matchers: + - Header=k1,Exact,v1 + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: header-complete + matchers: + - kind: Header + args: + name: k1 + values: + - mode: Exact + value: v1 + sensitive: false + service: Static=examples/resources + ``` diff --git a/docs/matchers/host.md b/docs/matchers/host.md new file mode 100644 index 0000000..0796f41 --- /dev/null +++ b/docs/matchers/host.md @@ -0,0 +1,39 @@ +# Host + +> 根据请求`Host`中的值来匹配是否需要使用此路由 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:-------|-------------|:----:|:---:|:------| +| values | `[pattern]` | `Y` | | Host值 | + +备注:[`pattern`类型说明](./pattern.md) + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: host-shortcut + matchers: + - Host=Exact,www.satex.com + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: header-complete + matchers: + - kind: Host + args: + values: + - mode: Exact + value: www.satex.com + service: Static=examples/resources + ``` diff --git a/docs/matchers/method.md b/docs/matchers/method.md new file mode 100644 index 0000000..031a5b3 --- /dev/null +++ b/docs/matchers/method.md @@ -0,0 +1,37 @@ +# Method + +> 根据请求的`Method`来匹配是否需要使用此路由 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:--------|---------|:----:|:---:|:----| +| methods | `[str]` | `Y` | | 方法名 | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: method-shortcut + matchers: + - Method=GET,POST + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: method-complete + matchers: + - kind: Method + args: + methods: + - GET + - POST + service: Static=examples/resources + ``` diff --git a/docs/matchers/path.md b/docs/matchers/path.md new file mode 100644 index 0000000..6baaf52 --- /dev/null +++ b/docs/matchers/path.md @@ -0,0 +1,40 @@ +# Path + +> 根据请求的地址来匹配是否需要使用此路由 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:---------|---------|:----:|:---:|:-----| +| patterns | `[str]` | `Y` | | 接口地址 | + +备注:这里的路径支持`path-tree`模式,[使用方式见文档](https://github.com/viz-rs/path-tree/blob/main/README.md) +。可以在路径中绑定变量,然后在`Layer - RewritePath`中使用。 + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: path-shortcut + matchers: + - Path=/satex1/:v+,/satex2/:v+ + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: path-complete + matchers: + - kind: Path + args: + patterns: + - /satex1/:v+ + - /satex2/:v+ + service: Static=examples/resources + ``` \ No newline at end of file diff --git a/docs/matchers/pattern.md b/docs/matchers/pattern.md new file mode 100644 index 0000000..7097e3a --- /dev/null +++ b/docs/matchers/pattern.md @@ -0,0 +1,53 @@ +`Pattern`是一个通用的匹配规则描述,支持多种模式的匹配规则使用相同的方式进行描述。 + +### 结构定义 + +| 参数 | 类型 | 描述 | +|:----------|--------|:--------------------| +| mode | `str` | 匹配模式 | +| value | `str` | 匹配值,部分模式不需要该字段。 | +| sensitive | `bool` | 是否大小写敏感,部分模式不需要该字段。 | + +### 支持的模式: + +| 匹配逻辑 | 模式(`mode`) | 是否支持值(`value`) | 支持支持大小写敏感(`sensitive`) | +|:------|:---------------|:--------------:|:----------------------:| +| 精确匹配 | `Exact` | `Y` | `Y` | +| 前缀匹配 | `StartsWith` | `Y` | `Y` | +| 后缀匹配 | `EndsWith` | `Y` | `Y` | +| 是否包含 | `Contains` | `Y` | `Y` | +| 是否不包含 | `NotContaions` | `Y` | `Y` | +| 是否存在 | `Exists` | `N` | `N` | +| 是否不存在 | `NotExists` | `N` | `N` | +| 正则表达式 | `Regex` | `Y` | `N` | + +### 示例配置 + +- **简单配置** + + ```yaml + + router: + routes: + - id: pattern-example + matchers: + - Header=k1,Exact,v1,false + ``` + +- **完整配置** + + ```yaml + + router: + routes: + - id: pattern-example + matchers: + - kind: Header + args: + name: k1 + values: + - mode: Exact + value: v1 + sensitive: true + ``` + diff --git a/docs/matchers/query.md b/docs/matchers/query.md new file mode 100644 index 0000000..58e80ed --- /dev/null +++ b/docs/matchers/query.md @@ -0,0 +1,42 @@ +# Query + +> 根据请求`Query`中的值来匹配是否需要使用此路由 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:-------|-------------|:----:|:---:|:--------| +| name | `str` | `Y` | | Query名称 | +| values | `[pattern]` | `Y` | | Query头值 | + +备注:[`pattern`类型说明](./pattern.md) + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: query-shortcut + matchers: + - Query=k1,Exact,v1 + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: query-complete + matchers: + - kind: Query + args: + name: k1 + values: + - mode: Exact + value: v1 + sensitive: false + service: Static=examples/resources + ``` diff --git a/docs/matchers/remote_addr.md b/docs/matchers/remote_addr.md new file mode 100644 index 0000000..6202dbb --- /dev/null +++ b/docs/matchers/remote_addr.md @@ -0,0 +1,39 @@ +# RemoteAddr + +> 根据发送请求客户端的地址来匹配是否需要使用此路由 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:--------|---------|:----:|:---:|:-----------------------| +| sources | `[str]` | `Y` | | 客户端源地址,支持配置CIDR。 | +| policy | `bool` | `Y` | | 处理策略:`Accept`、`Reject` | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: remote-addr-shortcut + matchers: + - RemoteAddr=127.0.0.1,127.0.1.1/24,Accept + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: remote-addr-complete + matchers: + - kind: RemoteAddr + args: + sources: + - 127.0.0.1 + - 127.0.1.1/24 + policy: Accept + service: Static=examples/resources + ``` diff --git a/docs/matchers/time.md b/docs/matchers/time.md new file mode 100644 index 0000000..7b293cc --- /dev/null +++ b/docs/matchers/time.md @@ -0,0 +1,39 @@ +# Time + +> 根据当前系统时间来匹配是否需要使用此路由 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:-----|-------|:----:|:---:|:----------------------------| +| mode | `str` | `Y` | | 模式:`Before`、`After` | +| time | `str` | `Y` | | 目标时间,格式为`%Y-%m-%d %H:%M:%S` | + +备注:这里时间使用的是本地时区。 + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: time-shortcut + matchers: + - Time=Before,2024-12-31 23:59:59 + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: time-complete + matchers: + - kind: Time + args: + mode: Before + time: 2024-12-31 23:59:59 + service: Static=examples/resources + ``` diff --git a/docs/satex.md b/docs/satex.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/services/echo.md b/docs/services/echo.md new file mode 100644 index 0000000..416c9f1 --- /dev/null +++ b/docs/services/echo.md @@ -0,0 +1,28 @@ +# Echo + +> 简单地返回接收到的请求内容,主要用于测试。 + +## 参数 + +无 + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: echo-shortcut + service: Echo + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: echo-complete + service: + kind: Echo + ``` \ No newline at end of file diff --git a/docs/services/proxy.md b/docs/services/proxy.md new file mode 100644 index 0000000..799d5da --- /dev/null +++ b/docs/services/proxy.md @@ -0,0 +1,32 @@ +# Proxy + +> 反向代理服务,代理请求到另一个服务或地址。 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:----|:-----:|:----:|:---:|:-------| +| uri | `str` | `Y` | | 后端服务地址 | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: proxy-shortcut + service: Proxy=https://www.satex.com + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: proxy-complete + service: + kind: Proxy + args: + uri: https://www.satex.com + ``` \ No newline at end of file diff --git a/docs/services/static.md b/docs/services/static.md new file mode 100644 index 0000000..d398e38 --- /dev/null +++ b/docs/services/static.md @@ -0,0 +1,32 @@ +# Echo + +> 用于返回静态文件的服务 + +## 参数 + +| 参数 | 类型 | 是否必须 | 默认值 | 描述 | +|:-----|:-----:|:----:|:---:|:-------| +| path | `str` | `Y` | | 静态资源路径 | + +## 配置示例 + +- ### 简单模式 + + ```yaml + router: + routes: + - id: proxy-shortcut + service: Static=examples/resources + ``` + +- ### 完整模式 + + ```yaml + router: + routes: + - id: proxy-complete + service: + kind: Static + args: + path: examples/resources + ``` \ No newline at end of file diff --git a/examples/discovery/builtin.yaml b/examples/discovery/builtin.yaml deleted file mode 100644 index 0fb2556..0000000 --- a/examples/discovery/builtin.yaml +++ /dev/null @@ -1,21 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -discovery: - - kind: Builtin - args: - server: server1 - uris: - - 127.0.0.1:9000 - - 127.0.0.1:9001 - - 127.0.0.1:9002 - interval: 10 - lb: Random - -router: - routes: - - id: proxy - service: Proxy=http://server1 \ No newline at end of file diff --git a/examples/discovery/load_balance/ip_hash.yaml b/examples/discovery/load_balance/ip_hash.yaml deleted file mode 100644 index 2e33440..0000000 --- a/examples/discovery/load_balance/ip_hash.yaml +++ /dev/null @@ -1,36 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -discovery: - - kind: Builtin - args: - - server: server1 - uris: - - 127.0.0.1:9000 - - 127.0.0.1:9001 - - 127.0.0.1:9002 - interval: 10 - lb: IpHash=1800,10 - - - server: server2 - uris: - - 127.0.0.1:8000 - - 127.0.0.1:8001 - - 127.0.0.1:8002 - interval: 10 - lb: - kind: IpHash - args: - timeout: 1800 - interval: 10 - -router: - routes: - - id: proxy1 - service: Proxy=http://server1 - - - id: proxy2 - service: Proxy=http://server2 \ No newline at end of file diff --git a/examples/discovery/load_balance/random.yaml b/examples/discovery/load_balance/random.yaml deleted file mode 100644 index 0fb2556..0000000 --- a/examples/discovery/load_balance/random.yaml +++ /dev/null @@ -1,21 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -discovery: - - kind: Builtin - args: - server: server1 - uris: - - 127.0.0.1:9000 - - 127.0.0.1:9001 - - 127.0.0.1:9002 - interval: 10 - lb: Random - -router: - routes: - - id: proxy - service: Proxy=http://server1 \ No newline at end of file diff --git a/examples/discovery/load_balance/sequential.yaml b/examples/discovery/load_balance/sequential.yaml deleted file mode 100644 index 005c41e..0000000 --- a/examples/discovery/load_balance/sequential.yaml +++ /dev/null @@ -1,21 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -discovery: - - kind: Builtin - args: - server: server1 - uris: - - 127.0.0.1:9000 - - 127.0.0.1:9001 - - 127.0.0.1:9002 - interval: 10 - lb: Sequential - -router: - routes: - - id: proxy - service: Proxy=http://server1 \ No newline at end of file diff --git a/examples/discovery/load_balance/standby.yaml b/examples/discovery/load_balance/standby.yaml deleted file mode 100644 index a5dbf1d..0000000 --- a/examples/discovery/load_balance/standby.yaml +++ /dev/null @@ -1,21 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -discovery: - - kind: Builtin - args: - server: server1 - uris: - - 127.0.0.1:9000 - - 127.0.0.1:9001 - - 127.0.0.1:9002 - interval: 10 - lb: StandBy - -router: - routes: - - id: proxy - service: Proxy=http://server1 \ No newline at end of file diff --git a/examples/discovery/load_balance/weight.yaml b/examples/discovery/load_balance/weight.yaml deleted file mode 100644 index fca380e..0000000 --- a/examples/discovery/load_balance/weight.yaml +++ /dev/null @@ -1,21 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -discovery: - - kind: Builtin - args: - server: server1 - uris: - - 127.0.0.1:9000 - - 127.0.0.1:9001 - - 127.0.0.1:9002 - interval: 10 - lb: Weight=1,1,8 - -router: - routes: - - id: proxy - service: Proxy=http://server1 \ No newline at end of file diff --git a/examples/layer/cors.yaml b/examples/layer/cors.yaml deleted file mode 100644 index 0589ae6..0000000 --- a/examples/layer/cors.yaml +++ /dev/null @@ -1,32 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: cors-shortcut - layers: - # 快捷配置不支持配置参数 - - Cors - service: Static=examples/resources - - - id: cors-complete - layers: - - kind: Cors - args: - allow_credentials: true - # allow_headers: '*' - allow_methods: - - GET - - POST - allow_origin: - - 127.0.0.1 - allow_private_network: true - expose_headers: - - k1 - max_age: 0 - vary: - - k2 - service: Static=examples/resources \ No newline at end of file diff --git a/examples/layer/keep_host_header.yaml b/examples/layer/keep_host_header.yaml deleted file mode 100644 index dfa497d..0000000 --- a/examples/layer/keep_host_header.yaml +++ /dev/null @@ -1,17 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: keep-host-header-shortcut - layers: - - KeepHostHeader - service: Static=examples/resources - - - id: KeepHostHeader - layers: - - kind: KeepHostHeader - service: Static=examples/resources \ No newline at end of file diff --git a/examples/layer/path_strip.yaml b/examples/layer/path_strip.yaml deleted file mode 100644 index 934bb4f..0000000 --- a/examples/layer/path_strip.yaml +++ /dev/null @@ -1,23 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: path-strip-shortcut - matchers: - - Path=@/static1 - layers: - - PathStrip=1 - service: Static=examples/resources - - - id: path-strip-complete - matchers: - - Path=@/static2 - layers: - - kind: PathStrip - args: - level: 1 - service: Static=examples/resources \ No newline at end of file diff --git a/examples/layer/rate_limit.yaml b/examples/layer/rate_limit.yaml deleted file mode 100644 index a4a9971..0000000 --- a/examples/layer/rate_limit.yaml +++ /dev/null @@ -1,29 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: rate-limit-shortcut - matchers: - - Path=/a - layers: - - PathStrip=1 - - RateLimit=Break,1,1,10 - service: Static=examples/resources - - - id: rate-limit-complete - matchers: - - Path=/b - layers: - - PathStrip=1 - - kind: RateLimit - args: - policy: Await - max: 1 - refill: 1 - interval: 10 - fair: false - service: Static=examples/resources \ No newline at end of file diff --git a/examples/layer/rewrite_path.yaml b/examples/layer/rewrite_path.yaml deleted file mode 100644 index a46c41c..0000000 --- a/examples/layer/rewrite_path.yaml +++ /dev/null @@ -1,23 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: rewrite-path-shortcut - matchers: - - Path=/a/:x+ - layers: - - RewritePath=/b/{x} - service: Static=examples/resources - - - id: rewrite-path-complete - matchers: - - Path=/a/:x+ - layers: - - kind: RewritePath - args: - path: /b/{x} - service: Static=examples/resources \ No newline at end of file diff --git a/examples/layer/set_header.yaml b/examples/layer/set_header.yaml deleted file mode 100644 index 90e03fe..0000000 --- a/examples/layer/set_header.yaml +++ /dev/null @@ -1,26 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: set-request-header - matchers: - - Path=@/a - layers: - - PathStrip=1 - - SetRequestHeader=k1,v1 - service: Static=examples/resources - - - id: set-response-header - matchers: - - Path=@/b - layers: - - PathStrip=1 - - kind: SetResponseHeader - args: - name: k1 - value: v1 - service: Static=examples/resources \ No newline at end of file diff --git a/examples/layer/x_forward.yaml b/examples/layer/x_forward.yaml deleted file mode 100644 index e07dbd0..0000000 --- a/examples/layer/x_forward.yaml +++ /dev/null @@ -1,17 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: x-forward-shortcut - layers: - - XForward - service: Static=examples/resources - - - id: set-path-complete - layers: - - kind: XForward - service: Static=examples/resources \ No newline at end of file diff --git a/examples/matcher/cookie.yaml b/examples/matcher/cookie.yaml deleted file mode 100644 index 1a9cad1..0000000 --- a/examples/matcher/cookie.yaml +++ /dev/null @@ -1,26 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: cookie-shortcut - matchers: - - Cookie=k1,Exact,v1 - service: Static=examples/resources - - - id: cookie-complete - matchers: - - kind: Cookie - args: - name: k2 - values: - - mode: Exact - value: v2 - sensitive: false - - mode: StartsWith - value: v3 - sensitive: true - service: Static=examples/resources \ No newline at end of file diff --git a/examples/matcher/header.yaml b/examples/matcher/header.yaml deleted file mode 100644 index c93c696..0000000 --- a/examples/matcher/header.yaml +++ /dev/null @@ -1,23 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: header-shortcut - matchers: - - Header=k1,Exact,v1 - service: Static=examples/resources - - - id: header-complete - matchers: - - kind: Header - args: - name: k2 - values: - - mode: Exists - - mode: Regex - value: [a-zA-Z]+ - service: Static=examples/resources \ No newline at end of file diff --git a/examples/matcher/host.yaml b/examples/matcher/host.yaml deleted file mode 100644 index 3297ed7..0000000 --- a/examples/matcher/host.yaml +++ /dev/null @@ -1,22 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: host-shortcut - matchers: - - Host=Exact,127.0.0.1 - service: Static=examples/resources - - - id: host-complete - matchers: - - kind: Host - args: - - mode: Exact - value: 127.0.0.1 - - mode: StartsWith - value: 127.0.0 - service: Static=examples/resources \ No newline at end of file diff --git a/examples/matcher/method.yaml b/examples/matcher/method.yaml deleted file mode 100644 index d91364a..0000000 --- a/examples/matcher/method.yaml +++ /dev/null @@ -1,20 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: method-shortcut - matchers: - - Method=GET - service: Static=examples/resources - - - id: method-complete - matchers: - - kind: Method - args: - methods: - - POST - service: Static=examples/resources \ No newline at end of file diff --git a/examples/matcher/path.yaml b/examples/matcher/path.yaml deleted file mode 100644 index 6b62769..0000000 --- a/examples/matcher/path.yaml +++ /dev/null @@ -1,26 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: path-shortcut - matchers: - # 使用:x绑定变量 +匹配多层 - - Path=/a,/b/:x,/b/:x+ - layers: - # 重写路径 - - RewritePath=/a/b/{x} - service: Static=examples/resources - - - id: path-complete - matchers: - - kind: Path - args: - patterns: - - /a - - /b/:x - - /c/:x+ - service: Static=examples/resources \ No newline at end of file diff --git a/examples/matcher/query.yaml b/examples/matcher/query.yaml deleted file mode 100644 index 5ecb315..0000000 --- a/examples/matcher/query.yaml +++ /dev/null @@ -1,23 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: query-shortcut - matchers: - - Query=k1,Exact,v1 - service: Static=examples/resources - - - id: query-complete - matchers: - - kind: Query - args: - name: k2 - values: - - mode: StartsWith - value: v2 - sensitive: false - service: Static=examples/resources \ No newline at end of file diff --git a/examples/matcher/remote_addr.yaml b/examples/matcher/remote_addr.yaml deleted file mode 100644 index ab0a380..0000000 --- a/examples/matcher/remote_addr.yaml +++ /dev/null @@ -1,23 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: remote-addr-shortcut - matchers: - # 最后的参数true/false分别对应Accept/Reject - - RemoteAddr=127.0.0.0/8,true - service: Static=examples/resources - - - id: remote-addr-complete - matchers: - - kind: RemoteAddr - args: - sources: - - 127.0.0.1/8 - # Accept/Reject - policy: Accept - service: Static=examples/resources diff --git a/examples/matcher/time.yaml b/examples/matcher/time.yaml deleted file mode 100644 index 5951954..0000000 --- a/examples/matcher/time.yaml +++ /dev/null @@ -1,20 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: time-shortcut - matchers: - - Time=After,2024-01-05 00:00:00 - service: Static=examples/resources - - - id: time-complete - matchers: - - kind: Time - args: - mode: Before - time: "2024-12-31 23:59:59" - service: Static=examples/resources diff --git a/examples/satex.yaml b/examples/satex.yaml index de0adcf..9b7804c 100644 --- a/examples/satex.yaml +++ b/examples/satex.yaml @@ -16,6 +16,5 @@ satex: # 服务配置 serves: - - examples/service/static.yaml - - examples/service/proxy.yaml - - examples/tls/tls.yaml + - examples/static.yaml + - examples/tls/static.yaml diff --git a/examples/service/echo.yaml b/examples/service/echo.yaml deleted file mode 100644 index 9cf0d98..0000000 --- a/examples/service/echo.yaml +++ /dev/null @@ -1,14 +0,0 @@ -server: - port: 9000 - -tracing: - max_level: debug - -router: - routes: - - id: echo-shortcut - service: Echo - - - id: echo-complete - service: - kind: Echo \ No newline at end of file diff --git a/examples/service/proxy.yaml b/examples/service/proxy.yaml deleted file mode 100644 index a28f8fd..0000000 --- a/examples/service/proxy.yaml +++ /dev/null @@ -1,20 +0,0 @@ -server: - port: 9001 - -router: - routes: - - id: static - matchers: - - Path=/static/+ - layers: - - RewritePath=/index.html - service: Static=examples/resources - - - id: proxy-shortcut - service: Proxy=http://127.0.0.1:9001/static - - - id: proxy-complete - service: - kind: Proxy - args: - uri: http://127.0.0.1:9001/static \ No newline at end of file diff --git a/examples/service/static.yaml b/examples/service/static.yaml deleted file mode 100644 index 1ca991e..0000000 --- a/examples/service/static.yaml +++ /dev/null @@ -1,7 +0,0 @@ -server: - port: 9000 - -router: - routes: - - id: static-shortcut - service: Static=examples/resources \ No newline at end of file diff --git a/examples/static.yaml b/examples/static.yaml new file mode 100644 index 0000000..8d651b4 --- /dev/null +++ b/examples/static.yaml @@ -0,0 +1,12 @@ +# 服务配置 +server: + # 服务端口 + port: 8000 + +# 路由配置 +router: + # 路由表 + routes: + # 静态文件 + - id: static + service: Static=examples/resources \ No newline at end of file diff --git a/examples/tls/tls.yaml b/examples/tls/static.yaml similarity index 52% rename from examples/tls/tls.yaml rename to examples/tls/static.yaml index 30051b3..172ea64 100644 --- a/examples/tls/tls.yaml +++ b/examples/tls/static.yaml @@ -1,13 +1,20 @@ +# 服务配置 server: - port: 9002 + # 服务端口 + port: 8001 + # TLS信息 tls: + # 是否开启TLS enabled: true + # 证书地址 certs: examples/tls/cert.pem + # 密钥地址 private_key: examples/tls/private-key.pem +# 路由配置 router: + # 路由表 routes: + # 静态文件 - id: static - matchers: - - Method=GET service: Static=examples/resources \ No newline at end of file diff --git a/satex-discovery/src/make/builtin/make.rs b/satex-discovery/src/make/builtin/make.rs index 3e990ea..dd82ef6 100644 --- a/satex-discovery/src/make/builtin/make.rs +++ b/satex-discovery/src/make/builtin/make.rs @@ -20,7 +20,7 @@ __make_discovery! { server: String, uris: Vec, interval: Option, - lb: Option, + load_balance: Option, } fn make(args: Args) -> Result { @@ -31,13 +31,12 @@ fn make(args: Args) -> Result { let capacity = configs.len(); let mut items = HashMap::with_capacity(capacity); configs.into_iter().try_for_each(|config| { - match config.lb { - Some(lb) => { - MakeLoadBalanceRegistry::get(lb.kind()).and_then(|make| make.make(lb.args())) - } + match config.load_balance { + Some(load_balance) => MakeLoadBalanceRegistry::get(load_balance.kind()) + .and_then(|make| make.make(load_balance.args())), None => Ok(NamedLoadBalance::default()), } - .map(|lb| { + .map(|load_balance| { let server = config.server; items.insert( server.clone(), @@ -47,7 +46,7 @@ fn make(args: Args) -> Result { config.uris, Duration::from_secs(config.interval.unwrap_or(DEFAULT_INTERVAL)), ), - lb, + load_balance, ), ); }) diff --git a/satex-discovery/src/make/builtin/mod.rs b/satex-discovery/src/make/builtin/mod.rs index 627a488..422cd02 100644 --- a/satex-discovery/src/make/builtin/mod.rs +++ b/satex-discovery/src/make/builtin/mod.rs @@ -41,12 +41,12 @@ impl ServerDiscovery for BuiltinDiscovery { server: &str, ) -> Result, Error> { match self.items.get(server) { - Some((selector, lb)) => { + Some((selector, load_balance)) => { let endpoints = selector.select().await?; match endpoints.len() { 0 => Ok(None), 1 => Ok(endpoints.into_iter().next().map(Endpoint::from)), - _ => lb.choose(essential, endpoints).await, + _ => load_balance.choose(essential, endpoints).await, } } None => { diff --git a/satex-layer/src/make/request_body_limit.rs b/satex-layer/src/make/request_body_limit.rs index 908d338..ef27d59 100644 --- a/satex-layer/src/make/request_body_limit.rs +++ b/satex-layer/src/make/request_body_limit.rs @@ -11,7 +11,7 @@ const DEFAULT_REQUEST_BODY_LIMIT: u64 = 10 * 1024; __make_layer! { RequestBodyLimit, #[serde(deserialize_with = "satex_core::serde::tot::as_u64", default = "Config::default_limit")] - limit: u64, + max: u64, } impl Config { @@ -21,5 +21,5 @@ impl Config { } fn make(args: Args) -> Result { - Config::try_from(args).map(|config| RequestBodyLimitLayer::new(config.limit as usize)) + Config::try_from(args).map(|config| RequestBodyLimitLayer::new(config.max as usize)) } diff --git a/satex-matcher/src/make/host/make.rs b/satex-matcher/src/make/host/make.rs index 102f4d1..c744cc9 100644 --- a/satex-matcher/src/make/host/make.rs +++ b/satex-matcher/src/make/host/make.rs @@ -1,4 +1,4 @@ -use satex_core::config::args::{Args, GatherMode}; +use satex_core::config::args::Args; use satex_core::pattern::Pattern; use satex_core::Error; @@ -7,17 +7,12 @@ use crate::{MakeRouteMatcher, __make_matcher}; __make_matcher! { Host, - values:Vec + values: Vec } fn make(args: Args<'_>) -> Result { - match args { - Args::Shortcut(shortcut) => shortcut - .deserialize::(&["values"], GatherMode::TailSequence) - .map(|config| config.values), - Args::Complete(complete) => complete.deserialize::>(), - } - .map(|patterns| HostMatcher::new(patterns)) + let config = Config::try_from(args)?; + Ok(HostMatcher::new(config.values)) } #[cfg(test)] diff --git a/satex-matcher/src/make/remote_addr/make.rs b/satex-matcher/src/make/remote_addr/make.rs index 15313e0..f55462d 100644 --- a/satex-matcher/src/make/remote_addr/make.rs +++ b/satex-matcher/src/make/remote_addr/make.rs @@ -12,7 +12,7 @@ __make_matcher! { RemoteAddr, SequenceFlag, sources: Vec, - policy: Option + policy: Option } fn make(args: Args) -> Result { @@ -40,10 +40,7 @@ fn make(args: Args) -> Result { parse(&source).map(|source| sources.apply(|sources| sources.push(source))) })?; Ok(RemoteAddrMatcher::new( - config - .policy - .map(|flag| if flag { Policy::Accept } else { Policy::Reject }) - .unwrap_or_default(), + config.policy.unwrap_or_default(), sources, )) } diff --git a/satex-matcher/src/make/remote_addr/mod.rs b/satex-matcher/src/make/remote_addr/mod.rs index 64f449a..86ba70a 100644 --- a/satex-matcher/src/make/remote_addr/mod.rs +++ b/satex-matcher/src/make/remote_addr/mod.rs @@ -23,7 +23,7 @@ impl Default for Policy { } impl Policy { - pub fn as_bool(&self) -> bool { + fn as_bool(&self) -> bool { match self { Policy::Accept => true, Policy::Reject => false, diff --git a/satex-service/src/make/static.rs b/satex-service/src/make/static.rs index 1c5e6d2..68f3e5e 100644 --- a/satex-service/src/make/static.rs +++ b/satex-service/src/make/static.rs @@ -10,9 +10,9 @@ type StaticService = ServeDir; __make_service! { Static, - directory: String, + path: String, } fn make(args: Args) -> Result { - Config::try_from(args).map(|config| ServeDir::new(config.directory)) + Config::try_from(args).map(|config| ServeDir::new(config.path)) } From 226d050bd552120b74288f1676aa3337a21a065f Mon Sep 17 00:00:00 2001 From: w-sodalite <70255432+w-sodalite@users.noreply.github.com> Date: Fri, 19 Jan 2024 14:35:34 +0800 Subject: [PATCH 2/3] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复组件表格显示错误的问题 --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1cb313c..740b703 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ - #### Matcher | 名称 | 描述 | 使用文档 | - |:-------------|------------------------|:----------------------------------:| + |:-------------|------------------------|:----------------------------------:| | `Method` | 根据请求方法(如GET、POST等)进行匹配 | [文档](docs/matchers/method.md) | | `Query` | 根据请求的查询参数进行匹配 | [文档](docs/matchers/query.md) | | `Header` | 根据请求头信息进行匹配 | [文档](docs/matchers/header.md) | @@ -41,7 +41,7 @@ - #### Layer | 名称 | 描述 | 使用文档 | - |:--------------------|--------------|:----------------------------------:| + |:--------------------|--------------|:----------------------------------:| | `Cors` | 处理跨域请求 | [文档](docs/layers/cors.md) | | `KeepHostHeader` | 保持原始的Host请求头 | [文档](docs/matchers/query.md) | | `PathStrip` | 从请求路径中删除特定部分 | [文档](docs/matchers/header.md) | @@ -57,7 +57,7 @@ - #### Service | 名称 | 描述 | 使用文档 | - |:---------|-----------------------|:-----------------------------:| + |:---------|-----------------------|:-----------------------------:| | `Echo` | 简单地返回接收到的请求内容 | [文档](docs/services/echo.md) | | `Static` | 提供静态文件服务 | [文档](docs/services/static.md) | | `Proxy` | 反向代理服务,代理请求到另一个服务或地址。 | [文档](docs/services/proxy.md) | @@ -65,13 +65,13 @@ - #### Discovery | 名称 | 描述 | 使用文档 | - |:----------|----------------------------------------|:-------------------------------:| + |:----------|----------------------------------------|:-------------------------------:| | `Builtin` | 内置的服务发现,通过配置的方式注册服务集合,内部会定时检测服务节点的可用性。 | [文档](docs/discovery/builtin.md) | - #### LoadBalance | 名称 | 描述 | 使用文档 | - |:-------------|--------------------------------------------------------------------------------------------|:-------------------------------------:| + |:-------------|--------------------------------------------------------------------------------------------|:-------------------------------------:| | `IpHash` | IP哈希负载策略使用客户端的IP地址进行哈希计算,根据哈希值将请求分配给后端服务器。这样可以确保来自同一IP地址的请求始终被发送到同一台服务器,这有助于保持会话和状态信息的持续性。 | [文档](docs/load_balance/ip_hash.md) | | `Random` | 随机负载策略随机选择一台服务器将请求发送过去。这种策略简单且易于实现,适用于没有特殊需求的情况。 | [文档](docs/load_balance/random.md) | | `Sequential` | 顺序负载策略按照服务器列表的顺序依次将请求发送过去。这种策略适用于服务器性能基本一致的情况。 | [文档](docs/load_balance/sequential.md) | From 8688a2f1ef7a1db2508635257c90b6e91454ae7f Mon Sep 17 00:00:00 2001 From: w-sodalite Date: Fri, 19 Jan 2024 14:44:33 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8DHost=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=87=BA=E9=94=99=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- satex-matcher/src/make/host/make.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/satex-matcher/src/make/host/make.rs b/satex-matcher/src/make/host/make.rs index c744cc9..923797c 100644 --- a/satex-matcher/src/make/host/make.rs +++ b/satex-matcher/src/make/host/make.rs @@ -7,6 +7,7 @@ use crate::{MakeRouteMatcher, __make_matcher}; __make_matcher! { Host, + TailSequence, values: Vec }