Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a7f0693809 | ||
|
|
2e902c4ec8 | ||
|
|
8a2f76ffc9 | ||
|
|
1bf819281a | ||
|
|
9fd0ed8d5b | ||
|
|
3cde76a6b0 | ||
|
|
2b7e11e7d2 | ||
|
|
3925767d2e | ||
|
|
c207bdd400 | ||
|
|
8ed2986eb9 | ||
|
|
819b181084 | ||
|
|
fec9c40f93 | ||
|
|
0ff3b47156 | ||
|
|
449d8b0ad9 | ||
|
|
591da84018 | ||
|
|
571662116c |
7
.gitignore
vendored
7
.gitignore
vendored
@ -1 +1,8 @@
|
||||
.log.meta.json
|
||||
.ai/
|
||||
.geminiignore
|
||||
.gemini
|
||||
env.json
|
||||
env.yml
|
||||
env.yaml
|
||||
/CODE-FULL.md
|
||||
|
||||
347
.log.meta.json
347
.log.meta.json
@ -436,118 +436,6 @@
|
||||
"Hide": false
|
||||
}
|
||||
],
|
||||
"monitor": [
|
||||
{
|
||||
"Index": 0,
|
||||
"Name": "LogName",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "cyan",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 1,
|
||||
"Name": "LogType",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "magenta",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 2,
|
||||
"Name": "LogTime",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "time",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 3,
|
||||
"Name": "TraceId",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "gray",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": true,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 4,
|
||||
"Name": "Image",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 5,
|
||||
"Name": "Server",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 6,
|
||||
"Name": "Target",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 7,
|
||||
"Name": "Status",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 8,
|
||||
"Name": "Message",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 9,
|
||||
"Name": "Extra",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
}
|
||||
],
|
||||
"request": [
|
||||
{
|
||||
"Index": 0,
|
||||
@ -902,241 +790,6 @@
|
||||
"Hide": false
|
||||
}
|
||||
],
|
||||
"statistic": [
|
||||
{
|
||||
"Index": 0,
|
||||
"Name": "LogName",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "cyan",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 1,
|
||||
"Name": "LogType",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "magenta",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 2,
|
||||
"Name": "LogTime",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "time",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 3,
|
||||
"Name": "TraceId",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "gray",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": true,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 4,
|
||||
"Name": "Image",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 5,
|
||||
"Name": "Server",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 6,
|
||||
"Name": "Category",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 7,
|
||||
"Name": "Item",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 8,
|
||||
"Name": "Value",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 9,
|
||||
"Name": "Extra",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
}
|
||||
],
|
||||
"task": [
|
||||
{
|
||||
"Index": 0,
|
||||
"Name": "LogName",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "cyan",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 1,
|
||||
"Name": "LogType",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "magenta",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 2,
|
||||
"Name": "LogTime",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "time",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 3,
|
||||
"Name": "TraceId",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "gray",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": true,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 4,
|
||||
"Name": "Image",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 5,
|
||||
"Name": "Server",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": true
|
||||
},
|
||||
{
|
||||
"Index": 6,
|
||||
"Name": "Task",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 7,
|
||||
"Name": "UsedTime",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 8,
|
||||
"Name": "Success",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 9,
|
||||
"Name": "Message",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
},
|
||||
{
|
||||
"Index": 10,
|
||||
"Name": "Extra",
|
||||
"KeyName": "",
|
||||
"AttachBefore": false,
|
||||
"Color": "",
|
||||
"Format": "",
|
||||
"Precision": 0,
|
||||
"WithoutKey": false,
|
||||
"Hide": false
|
||||
}
|
||||
],
|
||||
"warning": [
|
||||
{
|
||||
"Index": 0,
|
||||
|
||||
28
CHANGELOG.md
28
CHANGELOG.md
@ -1,5 +1,33 @@
|
||||
# CHANGELOG - go/service
|
||||
|
||||
## v1.3.1 (2026-05-10)
|
||||
- **Logging Refactor (Callback Pattern)**: 引入 `LogRequest` 闭环式回调封装,自动处理日志级别检查、对象池获取及元数据填充,消除 20+ 参数带来的维护压力。
|
||||
- **Graceful Shutdown**: `ServiceConfig` 新增 `StopTimeout` 字段,支持通过配置灵活管控服务优雅退出的超时时间(默认 5s)。
|
||||
- **Panic Recovery**: 增强 `handler.go` 中的 `recover` 逻辑,在发生 Panic 时自动记录 `requestId` 和 `path`,大幅提升故障定位效率。
|
||||
- **Infrastructure Alignment**: 全量回退并对齐 Go 版本至 `1.25.0`;同步更新 `handler.go` 以适配新的日志调用模式。
|
||||
|
||||
## v1.0.5 (2026-05-10)
|
||||
- **Wait**: This version was a temporary placeholder. See v1.3.1 for the actual release.
|
||||
|
||||
## v1.0.4 (2026-05-10)
|
||||
- **Log Optimization**: Implemented `NoLogGets`, `NoLogHeaders`, `LogInputArrayNum`, `LogInputFieldSize`, and other fine-grained logging filters.
|
||||
- **Static Log**: Added automatic logging for static file access.
|
||||
- **Panic Recovery**: Introduced a global `recover` middleware to capture and log panics with full stack traces.
|
||||
- **Server Hardening**: Applied `ReadTimeout`, `WriteTimeout`, `IdleTimeout`, and `MaxHeaderBytes` configurations to the underlying HTTP server.
|
||||
- **Service Alignment**: Populated `Config.App` and improved `serverId` generation for better traceability.
|
||||
- **Cleanup**: Removed deprecated fields (`CpuMonitor`, `MemoryMonitor`, `Fast`, etc.) and unused logging methods (`logTask`, `logMonitor`).
|
||||
|
||||
## v1.0.3 (2026-05-09)
|
||||
### Added
|
||||
- **Zero-Config Microservices**: 实现智能启动逻辑。当 `Listen` 为空时,自动开启随机端口并使用 `h2c` 协议。
|
||||
- **Auto-Discover**: 自动集成 `go/discover`。当使用随机端口或配置了应用名时,自动完成服务注册。
|
||||
- **Smart Detection**: 内置应用名自动识别 (`debug.ReadBuildInfo`) 与服务 IP 自动探测 (UDP 伪拨号法)。
|
||||
- **H2C Server**: 原生支持 HTTP/2 Cleartext (h2c) 服务端协议,提升微服务间通信性能。
|
||||
|
||||
### Changed
|
||||
- **Infrastructure Alignment**: `go.mod` 引入 `golang.org/x/net` 以支持 H2C,升级 `go/discover` 至 `v1.0.9`。
|
||||
- **IO Security**: 持续优化 `go/file` 在静态文件服务中的应用。
|
||||
|
||||
## v1.0.2 (2026-05-09)
|
||||
### Changed
|
||||
- **Infrastructure Alignment**: `go.mod` 升级 `go/config` 至 `v1.0.7`,`go/http` 至 `v1.0.10`。
|
||||
|
||||
216
DocTpl.html
216
DocTpl.html
@ -1,216 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{{.title}}</title>
|
||||
<style>
|
||||
html {
|
||||
overflow: auto;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 10px;
|
||||
background: #fff;
|
||||
color: #333;
|
||||
font-size: 16px;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.nav {
|
||||
overflow-x: hidden;
|
||||
overflow-y: auto;
|
||||
flex: 1;
|
||||
background: #333;
|
||||
color: #fff;
|
||||
height: 100%;
|
||||
padding: 10px 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.navItem {
|
||||
width: 100%;
|
||||
display: block;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
padding: 4px 10px;
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
color: #fff;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.navItem:hover {
|
||||
background: #99ccff;
|
||||
}
|
||||
|
||||
.navItem .memo {
|
||||
font-weight: normal;
|
||||
font-size: 12px;
|
||||
color: #ccc;
|
||||
}
|
||||
|
||||
.apiBox {
|
||||
overflow: auto;
|
||||
flex: 3;
|
||||
padding: 8px;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
header {
|
||||
border-bottom: #ddd 1px solid;
|
||||
margin-bottom: 5px;
|
||||
background: #333;
|
||||
color: #fff;
|
||||
padding: 12px;
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
}
|
||||
|
||||
header > span {
|
||||
font-weight: bold;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
header > span.memo {
|
||||
font-weight: normal;
|
||||
font-size: 14px;
|
||||
color: #faebd7;
|
||||
}
|
||||
|
||||
label {
|
||||
display: inline-block;
|
||||
margin-right: 10px;
|
||||
padding: 4px 8px;
|
||||
font-size: 12px;
|
||||
background: #ccc;
|
||||
color: #000;
|
||||
font-weight: bold;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
label.authLevel {
|
||||
background: #f90;
|
||||
color: #000;
|
||||
}
|
||||
|
||||
label.authLevel0 {
|
||||
background: #ccc;
|
||||
color: #000;
|
||||
}
|
||||
|
||||
label.type {
|
||||
background: #9cf;
|
||||
color: #000;
|
||||
}
|
||||
|
||||
section {
|
||||
margin-bottom: 40px;
|
||||
white-space: nowrap;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
header.Action, section.Action {
|
||||
margin-left: 20px;
|
||||
}
|
||||
|
||||
section > table {
|
||||
width: 50%;
|
||||
display: inline-table;
|
||||
border-collapse: collapse;
|
||||
vertical-align: top;
|
||||
font-size: 16px;
|
||||
|
||||
}
|
||||
|
||||
section > table:last-child {
|
||||
border-left: 1px solid #ddd;
|
||||
}
|
||||
|
||||
tr:nth-child(even) {
|
||||
background: #f9f9f9;
|
||||
}
|
||||
|
||||
th {
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
td {
|
||||
padding: 6px 12px;
|
||||
white-space: pre-wrap;
|
||||
}
|
||||
|
||||
td:last-child {
|
||||
color: #666;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="nav">
|
||||
{{range .api}}
|
||||
<a href="#{{.Path}}" class="navItem">
|
||||
<span>{{.Path}}</span>
|
||||
<span class="memo">{{.Memo}}</span>
|
||||
</a>
|
||||
{{end}}
|
||||
</div>
|
||||
<div class="apiBox">
|
||||
{{range .api}}
|
||||
<a name="{{.Path}}"></a>
|
||||
<div style="height: 16px"></div>
|
||||
<header class="{{.Type}}">
|
||||
<span>{{.Path}}</span>
|
||||
<span class="memo">{{.Memo}}</span>
|
||||
{{if ne .Method ""}}<label>{{.Method}}</label>{{end}}
|
||||
<label title="Auth Level" class="authLevel authLevel{{.AuthLevel}}">{{.AuthLevel}}</label>
|
||||
{{if ne .Type "Web"}}<label class="type">{{.Type}}</label>{{end}}
|
||||
</header>
|
||||
<section class="{{.Type}}">
|
||||
<table>
|
||||
{{if isMap .In}}
|
||||
<tr>
|
||||
<th colspan="2">Request</th>
|
||||
</tr>
|
||||
{{range $k, $v := .In}}
|
||||
<tr>
|
||||
<td width="30%">{{$k}}</td>
|
||||
<td width="70%">{{toText $v}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
{{else}}
|
||||
<tr>
|
||||
<td colspan="2">{{.In}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
<table>
|
||||
{{if isMap .Out}}
|
||||
<tr>
|
||||
<th colspan="2">Response</th>
|
||||
</tr>
|
||||
{{range $k, $v := .Out}}
|
||||
<tr>
|
||||
<td width="30%">{{$k}}</td>
|
||||
<td width="70%">{{toText $v}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
{{else}}
|
||||
<tr>
|
||||
<td colspan="2">{{.Out}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
</section>
|
||||
{{else}}
|
||||
<div><strong>no document</strong></div>
|
||||
{{end}}
|
||||
<div style="height: 800px"></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
55
README.md
55
README.md
@ -4,6 +4,8 @@
|
||||
|
||||
## 核心特性
|
||||
- **极致精简**: 剥离非核心组件(如 Starter, Task, 业务 Result 定义),保持底座纯净。
|
||||
- **零配置启动**: 默认监听随机端口并启用 `h2c` 协议,自动完成 `discover` 注册,实现真正的 "Zero-Config" 微服务。
|
||||
- **智能探测**: 自动识别应用名称 (`ReadBuildInfo`) 与局域网 IP,消除环境摩擦。
|
||||
- **路由反射**: 自动解析函数参数,支持 `*Request`, `*Response`, `*log.Logger` 及自定义结构体自动注入。
|
||||
- **自动校验**: 集成 `verify` 引擎,通过 Struct Tag 实现入参合法性自动检查。
|
||||
- **功能闭环**: 内置静态文件服务、基础 WebSocket 注册、URL 重写、反向代理(对接 Discover)。
|
||||
@ -57,6 +59,59 @@ func main() {
|
||||
- **文档生成**: `service.MakeDocument()` 返回全量接口描述
|
||||
- **依赖注入**: `service.GetInjectT[T]()` 快速获取已注入的对象或组件
|
||||
|
||||
## 配置指南 (ServiceConfig)
|
||||
|
||||
详细配置项可查阅 `config.go` 中的 `ServiceConfig` 结构。通过 `config.Load` 支持从 `env.yml` 或环境变量加载。
|
||||
|
||||
### 1. 基础服务配置
|
||||
```yaml
|
||||
service:
|
||||
App: "user-service" # 应用名称 (缺省自动探测)
|
||||
Listen: ":8080,http|:443" # 监听端口, 多监听用 | 隔开, 选项用 , 隔开 (http/h2/h2c)
|
||||
Register: "127.0.0.1:6379" # 发现中心地址 (Redis URL 或配置名)
|
||||
Weight: 100 # 服务发现权重
|
||||
ReadTimeout: 30000 # 读取请求超时 (ms)
|
||||
WriteTimeout: 30000 # 写入响应超时 (ms)
|
||||
MaxHeaderBytes: 1048576 # 最大头部字节数 (1MB)
|
||||
StopTimeout: 5000 # 优雅停机等待时间 (ms)
|
||||
```
|
||||
|
||||
### 2. 下游调用配置 (Calls)
|
||||
```yaml
|
||||
service:
|
||||
Calls:
|
||||
order-service: # 目标应用名
|
||||
Timeout: 5000 # 调用超时时间 (ms)
|
||||
Http2: true # 强制启用 H2C
|
||||
Token: "secure-token" # 访问凭证
|
||||
```
|
||||
|
||||
### 3. 声明式路由与代理 (Nginx 模式)
|
||||
除了代码注册,也支持在配置文件中直接声明:
|
||||
```yaml
|
||||
service:
|
||||
# 代理规则 (Proxy)
|
||||
Proxies:
|
||||
"aa.com": # 指定 Host (支持 aa.com, :8080, *, "")
|
||||
- Path: "/api/*" # 必须以 /* 结尾启用前缀匹配
|
||||
ToApp: "backend-app" # 转发至服务发现的应用名
|
||||
ToPath: "/v1/*" # 后缀映射
|
||||
- Path: "/direct" # 精确匹配
|
||||
ToApp: "http://1.1.1.1:80" # 直接转发至 URL
|
||||
ToPath: "/hello"
|
||||
|
||||
# 重写规则 (Rewrite)
|
||||
Rewrites:
|
||||
"*": # 全局规则
|
||||
- Path: "^/old/(.*)$" # 带捕获组的正则匹配
|
||||
ToPath: "/new/$1" # 变量替换
|
||||
|
||||
# 静态目录 (Static)
|
||||
Statics:
|
||||
"www.example.com":
|
||||
"/ui": "./dist" # 将 /ui 路径映射至本地 ./dist 目录
|
||||
```
|
||||
|
||||
## 基础设施对齐
|
||||
- **类型转换**: `apigo.cc/go/cast`
|
||||
- **日志系统**: `apigo.cc/go/log`
|
||||
|
||||
19
TEST.md
19
TEST.md
@ -1,29 +1,36 @@
|
||||
# Service Module Test Report
|
||||
|
||||
## 性能测试 (Benchmark)
|
||||
- 测试日期: 2026-05-09
|
||||
- 版本: v1.0.2
|
||||
- 指标: `BenchmarkRouting`: 2984 ns/op
|
||||
- 测试日期: 2026-05-10
|
||||
- 版本: v1.0.4
|
||||
- 指标: `BenchmarkRouting`: 2791 ns/op
|
||||
- 环境: Darwin / Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
|
||||
|
||||
## 单元测试覆盖 (Unit Test)
|
||||
- [x] `TestServeHTTP`: 基础请求与响应
|
||||
- [x] `TestServeHTTP_404`: 404 处理
|
||||
- [x] `TestServeHTTP_VerifyFailed`: 参数校验失败处理
|
||||
- [x] `TestServeHTTP_Panic`: **(New)** 验证全局 Panic 恢复与日志记录
|
||||
- [x] `TestRewrite`: 路径重写
|
||||
- [x] `TestProxyDirect`: 代理转发 (Mock)
|
||||
- [x] `TestAsyncServer`: 异步启动与生命周期
|
||||
- [x] `TestAsyncServer`: 异步启动与生命周期 (已验证 Server Timeout 配置生效)
|
||||
- [x] `TestServiceRegister`: 基础路由注册
|
||||
- [x] `TestRegexServiceRegister`: 正则路由注册
|
||||
- [x] `TestStaticService`: 静态文件服务 (已支持内存文件)
|
||||
- [x] `TestStaticService`: 静态文件服务 (已支持内存文件与自动日志记录)
|
||||
- [x] `TestVerifyStruct`: 基础结构校验
|
||||
- [x] `TestNestedVerify`: 嵌套结构校验
|
||||
- [x] `TestCustomVerify`: 自定义校验函数
|
||||
- [x] `TestWebSocketService`: WebSocket 注册
|
||||
- [x] `TestGetDefaultName`: 自动应用名识别
|
||||
- [x] `TestGetServerIp`: 自动 IP 探测
|
||||
- [x] `TestSmartStartup`: 零配置智能启动与 Discover 注册
|
||||
- [x] **Logging Filters**: 已手动验证 `NoLogGets`, `NoLogHeaders` 等过滤逻辑。
|
||||
|
||||
## 基础设施对齐验证
|
||||
- [x] 成功集成 `apigo.cc/go/cast` 用于参数解析与类型强转。
|
||||
- [x] 成功集成 `apigo.cc/go/timer` 用于高性能耗时追踪。
|
||||
- [x] 成功集成 `apigo.cc/go/log` 并实现完整的 Request 日志记录。
|
||||
- [x] 成功集成 `apigo.cc/go/log` 并实现完整的 Request 日志记录,支持头过滤与内容截断。
|
||||
- [x] 强制集成 `apigo.cc/go/file` 替代原生 `os`,全面支持内存虚拟文件系统。
|
||||
- [x] 成功集成 `apigo.cc/go/id` 与 `go/redis` 实现分布式有序 ID。
|
||||
- [x] 成功集成 `apigo.cc/go/discover` 并支持 H2C 协议的零配置自动注册。
|
||||
- [x] **Safety**: 已集成 `recover` 机制,保障服务在高并发业务 Panic 时的稳定性。
|
||||
|
||||
111
config.go
111
config.go
@ -1,13 +1,30 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"apigo.cc/go/cast"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// CertSet SSL 证书配置
|
||||
type CertSet struct {
|
||||
CertFile string
|
||||
KeyFile string
|
||||
}
|
||||
|
||||
// CallConfig 下游服务调用配置
|
||||
type CallConfig struct {
|
||||
Timeout int // 超时时间 (ms)
|
||||
Token string // 访问凭证
|
||||
Http2 bool // 是否强制使用 HTTP/2 (H2C)
|
||||
SSL bool // 是否使用 HTTPS/WSS
|
||||
}
|
||||
|
||||
// ServiceConfig 核心服务配置
|
||||
type ServiceConfig struct {
|
||||
App string // 应用名称。优先从环境变量 DISCOVER_APP 获取,若为空则自动通过代码检测。
|
||||
Register string // 发现服务注册中心地址。支持 Redis URL 或 Redis 配置名称。
|
||||
Weight int // 当前节点在发现服务中的权重 (默认 100)
|
||||
Calls map[string]CallConfig // 依赖的下游服务调用配置
|
||||
Listen string // 监听端口(|隔开多个监听)(,隔开多个选项),例如 80,http|443|443:h2|127.0.0.1:8080,h2c
|
||||
SSL map[string]*CertSet // SSL 证书配置,key 为域名
|
||||
NoLogGets bool // 不记录 GET 请求的日志
|
||||
@ -17,7 +34,6 @@ type ServiceConfig struct {
|
||||
NoLogOutputFields string // 不记录响应字段中包含的这些字段
|
||||
LogOutputArrayNum int // 响应字段中容器类型在日志打印个数限制
|
||||
LogOutputFieldSize int // 响应字段中单个字段在日志打印长度限制
|
||||
LogWebsocketAction bool // 记录 Websocket 中每个 Action 的请求日志
|
||||
Compress bool // 是否启用压缩
|
||||
CompressMinSize int // 启用压缩的最小长度
|
||||
CompressMaxSize int // 启用压缩的最大长度
|
||||
@ -27,22 +43,13 @@ type ServiceConfig struct {
|
||||
AcceptXRealIpWithoutRequestId bool // 是否允许头部没有携带请求ID的 X-Real-IP 信息
|
||||
StatisticTime bool // 是否开启请求时间统计
|
||||
StatisticTimeInterval int // 统计时间间隔 (ms)
|
||||
Fast bool // 是否启用快速模式
|
||||
MaxUploadSize int64 // 最大上传文件大小 (Bytes)
|
||||
IpPrefix string // Discover 服务发现时指定使用的 IP 网段
|
||||
Cpu int // CPU 占用的核数限制
|
||||
Memory int // 内存限制 (MB)
|
||||
CpuMonitor bool // 记录 CPU 使用情况
|
||||
MemoryMonitor bool // 记录内存使用情况
|
||||
CpuLimitValue uint // CPU 自动重启阈值 (10-100)
|
||||
MemoryLimitValue uint // 内存自动重启阈值 (10-100)
|
||||
CpuLimitTimes uint // CPU 报警阈值连续次数
|
||||
MemoryLimitTimes uint // 内存报警阈值连续次数
|
||||
CookieScope string // Session Cookie 有效范围: host|domain|topDomain
|
||||
SessionWithoutCookie bool // Session 禁用 Cookie
|
||||
DeviceWithoutCookie bool // 设备ID禁用 Cookie
|
||||
IdServer string // Redis 服务器连接 (用于全局唯一 ID 生成)
|
||||
KeepKeyCase bool // 是否保持 Key 的首字母大小写
|
||||
IndexFiles []string // 静态文件索引文件
|
||||
IndexDir bool // 访问目录时显示文件列表
|
||||
ReadTimeout int // 读取请求的超时时间 (ms)
|
||||
@ -57,6 +64,90 @@ type ServiceConfig struct {
|
||||
MaxReadFrameSize uint32 // 单个帧的最大读取大小
|
||||
MaxUploadBufferPerConnection int32 // 每个连接的最大上传缓冲区大小
|
||||
MaxUploadBufferPerStream int32 // 每个流的最大上传缓冲区大小
|
||||
StopTimeout int // 停止服务的超时时间 (ms)
|
||||
|
||||
// 从配置文件中加载的静态路由策略 (按 Host 分组,全局配置用 "" 或 "*")
|
||||
Proxies map[string]map[string]any
|
||||
Rewrites map[string]map[string]any
|
||||
Statics map[string]map[string]string
|
||||
}
|
||||
|
||||
var Config = ServiceConfig{}
|
||||
|
||||
// ApplyConfig 将 ServiceConfig 中的路由策略应用到内部的文件级策略中
|
||||
func ApplyConfig() {
|
||||
hostPoliciesLock.Lock()
|
||||
defer hostPoliciesLock.Unlock()
|
||||
|
||||
// 1. Proxies KV 解析
|
||||
fileProxies = make(map[string][]*proxyType)
|
||||
for host, kv := range Config.Proxies {
|
||||
if host == "*" {
|
||||
host = ""
|
||||
}
|
||||
rules := make([]*proxyType, 0, len(kv))
|
||||
for path, val := range kv {
|
||||
if to, ok := val.(string); ok {
|
||||
// 极简 KV 模式: "/api/*": "user-svc/v1/*"
|
||||
rules = append(rules, parseProxyRule(0, path, "", "", to))
|
||||
} else {
|
||||
// 对象模式: "/api/*": {"To": "...", "Auth": 1}
|
||||
m, _ := cast.ToMap[string, any](val)
|
||||
rules = append(rules, parseProxyRule(
|
||||
cast.Int(m["Auth"]),
|
||||
path,
|
||||
cast.String(m["ToApp"]),
|
||||
cast.String(m["ToPath"]),
|
||||
cast.String(m["To"]),
|
||||
))
|
||||
}
|
||||
}
|
||||
fileProxies[host] = rules
|
||||
rebuildProxiesUnderLock(host)
|
||||
}
|
||||
|
||||
// 2. Rewrites KV 解析
|
||||
fileRewrites = make(map[string][]*rewriteType)
|
||||
for host, kv := range Config.Rewrites {
|
||||
if host == "*" {
|
||||
host = ""
|
||||
}
|
||||
rules := make([]*rewriteType, 0, len(kv))
|
||||
for path, val := range kv {
|
||||
if to, ok := val.(string); ok {
|
||||
rules = append(rules, parseRewriteRule(path, "", to))
|
||||
} else {
|
||||
m, _ := cast.ToMap[string, any](val)
|
||||
rules = append(rules, parseRewriteRule(
|
||||
path,
|
||||
cast.String(m["ToPath"]),
|
||||
cast.String(m["To"]),
|
||||
))
|
||||
}
|
||||
}
|
||||
fileRewrites[host] = rules
|
||||
rebuildRewritesUnderLock(host)
|
||||
}
|
||||
|
||||
staticsByHostLock.Lock()
|
||||
defer staticsByHostLock.Unlock()
|
||||
fileStatics = make(map[string]map[string]*string)
|
||||
|
||||
for host, config := range Config.Statics {
|
||||
if host == "*" {
|
||||
host = ""
|
||||
}
|
||||
newStatics := make(map[string]*string, len(config))
|
||||
for path, rootPath := range config {
|
||||
rp := rootPath
|
||||
if !filepath.IsAbs(rp) {
|
||||
if absPath, err := filepath.Abs(rp); err == nil {
|
||||
rp = absPath
|
||||
}
|
||||
}
|
||||
newStatics[path] = &rp
|
||||
}
|
||||
fileStatics[host] = newStatics
|
||||
rebuildStaticsUnderLock(host)
|
||||
}
|
||||
}
|
||||
|
||||
41
discover_test.go
Normal file
41
discover_test.go
Normal file
@ -0,0 +1,41 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetDefaultName(t *testing.T) {
|
||||
// Test from build info or args
|
||||
name := GetDefaultName()
|
||||
if name == "" {
|
||||
t.Error("Expected non-empty name")
|
||||
}
|
||||
t.Logf("Detected name: %s", name)
|
||||
}
|
||||
|
||||
func TestGetServerIp(t *testing.T) {
|
||||
ip := GetServerIp()
|
||||
if ip == "" || ip == "0.0.0.0" {
|
||||
t.Errorf("Invalid IP detected: %s", ip)
|
||||
}
|
||||
t.Logf("Detected IP: %s", ip)
|
||||
}
|
||||
|
||||
func TestSmartStartup(t *testing.T) {
|
||||
// Reset config
|
||||
Config.Listen = ""
|
||||
Config.App = "smart-test"
|
||||
|
||||
as := AsyncStart()
|
||||
if as.Addr == "" {
|
||||
t.Fatal("Server address should not be empty")
|
||||
}
|
||||
|
||||
t.Logf("Server started on %s", as.Addr)
|
||||
|
||||
if !as.useDiscover {
|
||||
t.Error("Should have enabled discover")
|
||||
}
|
||||
|
||||
as.Stop()
|
||||
}
|
||||
@ -19,9 +19,6 @@ type Api struct {
|
||||
Host string
|
||||
}
|
||||
|
||||
//go:embed DocTpl.html
|
||||
var defaultDocTpl string
|
||||
|
||||
// MakeDocument 生成文档数据
|
||||
func MakeDocument() []Api {
|
||||
out := make([]Api, 0)
|
||||
|
||||
33
go.mod
33
go.mod
@ -3,14 +3,29 @@ module apigo.cc/go/service
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
apigo.cc/go/cast v1.2.8
|
||||
apigo.cc/go/config v1.0.7
|
||||
apigo.cc/go/discover v1.0.7
|
||||
apigo.cc/go/file v1.0.7
|
||||
apigo.cc/go/http v1.0.10
|
||||
apigo.cc/go/id v1.0.5
|
||||
apigo.cc/go/log v1.1.9
|
||||
apigo.cc/go/redis v1.0.5
|
||||
apigo.cc/go/timer v1.0.6
|
||||
apigo.cc/go/cast v1.3.3
|
||||
apigo.cc/go/config v1.3.1
|
||||
apigo.cc/go/discover v1.3.2
|
||||
apigo.cc/go/file v1.3.2
|
||||
apigo.cc/go/http v1.3.2
|
||||
apigo.cc/go/id v1.3.1
|
||||
apigo.cc/go/log v1.3.4
|
||||
apigo.cc/go/redis v1.3.2
|
||||
apigo.cc/go/safe v1.3.1
|
||||
apigo.cc/go/starter v1.0.5
|
||||
apigo.cc/go/timer v1.3.1
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
golang.org/x/net v0.54.0
|
||||
)
|
||||
|
||||
require (
|
||||
apigo.cc/go/crypto v1.3.1 // indirect
|
||||
apigo.cc/go/encoding v1.3.1 // indirect
|
||||
apigo.cc/go/rand v1.3.1 // indirect
|
||||
apigo.cc/go/shell v1.3.1 // indirect
|
||||
github.com/gomodule/redigo v2.0.0+incompatible // indirect
|
||||
golang.org/x/crypto v0.51.0 // indirect
|
||||
golang.org/x/sys v0.44.0 // indirect
|
||||
golang.org/x/text v0.37.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
51
go.sum
51
go.sum
@ -1,2 +1,53 @@
|
||||
apigo.cc/go/cast v1.3.3 h1:aln5eDR5DZVWVzZ/y5SJh1gQNgWv2sT82I25NaO9g34=
|
||||
apigo.cc/go/cast v1.3.3/go.mod h1:lGlwImiOvHxG7buyMWhFzcdvQzmSaoKbmr7bcDfUpHk=
|
||||
apigo.cc/go/config v1.3.1 h1:wZzUh4oL+fGD6SayVgX6prLPMsniM25etWFcEH8XzIE=
|
||||
apigo.cc/go/config v1.3.1/go.mod h1:7KHz/1WmtBLM762Lln/TaXh2dmlMvJTLhnlk33zbS3U=
|
||||
apigo.cc/go/crypto v1.3.1 h1:ulQ2zX9bUWirk0sEacx1Srsjs2Jow7HlZq7ED7msNcg=
|
||||
apigo.cc/go/crypto v1.3.1/go.mod h1:SwHlBFDPddttWgFFtzsEMla8CM/rcFy9nvdsJjW4CIs=
|
||||
apigo.cc/go/discover v1.3.2 h1:hzo5PQSAbJrF9Qk7yCMoXWatHGe7lR2MNUqLLQ5oUSk=
|
||||
apigo.cc/go/discover v1.3.2/go.mod h1:77kZLGovdCYAMspkmL6iif65Yzhyg143ffQseZx+j40=
|
||||
apigo.cc/go/encoding v1.3.1 h1:y8O58KYAyulkThg1O2ji2BqjnFoSvk42sit9I3z+K7Y=
|
||||
apigo.cc/go/encoding v1.3.1/go.mod h1:xAJk5b83VZ31mXMTnyp0dfMoBKfT/AHDn0u+cQfojgY=
|
||||
apigo.cc/go/file v1.3.2 h1:pu4oiDyiqgj3/eykfnJf+/6+A9v/Z0b3ClP5XK+lwG4=
|
||||
apigo.cc/go/file v1.3.2/go.mod h1:vci4h0Pz94mV6dkniQkuyBYERVYeq7/LX4jJVuCg9hs=
|
||||
apigo.cc/go/http v1.3.2 h1:0Or5KfoIq4+yeWKYusYPV8XLPw8XuzJMeaFv7dZViLI=
|
||||
apigo.cc/go/http v1.3.2/go.mod h1:Q9R7Ors0Fz2A6Mxg0dykO2PjCzdAHRRXreOUMjMOLwA=
|
||||
apigo.cc/go/id v1.3.1 h1:pkqi6VeWyQoHuIu0Zbx/RRxIAdM61Js0j6cY1M9XVCk=
|
||||
apigo.cc/go/id v1.3.1/go.mod h1:P2/vl3tyW3US+ayOFSMoPIOCulNLBngNYPhXJC/Z7J4=
|
||||
apigo.cc/go/log v1.3.4 h1:UT8Neb9r4QjjbCFbTzw+ZeTxd+DmdmR5gNExeR4Cj+g=
|
||||
apigo.cc/go/log v1.3.4/go.mod h1:/Q/2r51xWSsrS4QN5U9jLiTw8n6qNC8kG9nuVHweY20=
|
||||
apigo.cc/go/rand v1.3.1 h1:7FvsI6PtQ5XrWER0dTiLVo0p7GIxRidT/TBKhVy93j8=
|
||||
apigo.cc/go/rand v1.3.1/go.mod h1:mZ/4Soa3bk+XvDaqPWJuUe1bfEi4eThBj1XmEAuYxsk=
|
||||
apigo.cc/go/redis v1.3.2 h1:iUWL/CHHnfonz0dJq6/V4IG3QuXBoHA2L1xnoGEbNEQ=
|
||||
apigo.cc/go/redis v1.3.2/go.mod h1:/k5wcfAzB9jrfd9otabio9CPUxEsLPgEs4oggBG5sbs=
|
||||
apigo.cc/go/safe v1.3.1 h1:irTCqPAC97gGsX/Lw5AzLelDt1xXLEZIAaVhLELWe9Q=
|
||||
apigo.cc/go/safe v1.3.1/go.mod h1:XdOpBhN2vkImalaykYXXmEpczqWa1y3ah6/Q72cdRqE=
|
||||
apigo.cc/go/shell v1.3.1 h1:M8oD0b2HcJuCC6frQFx11b3UTcTx3lATX8XK+YXSVm8=
|
||||
apigo.cc/go/shell v1.3.1/go.mod h1:ZMdJjpCpWdvsHKUXlelh/AxsV/nWdkH/k3lISfzMdUw=
|
||||
apigo.cc/go/starter v1.0.5 h1:pgjBun7zc3J+3hcWnP22bGtgB3+TiNXOrOGVyGM7u1Q=
|
||||
apigo.cc/go/starter v1.0.5/go.mod h1:auAvnBknZuMMps6HRtlwf8Z5gCHRQYsVw7WhhiGwWpg=
|
||||
apigo.cc/go/timer v1.3.1 h1:YMSusF1LfJYOf6tAW94Yipj3pHrX6QhfP7Rk3nGFT8k=
|
||||
apigo.cc/go/timer v1.3.1/go.mod h1:kOnqTTX+zA4AH7SfC+LpUm4ZvS+DVyWWMqul/V5QWJs=
|
||||
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
|
||||
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||
golang.org/x/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI=
|
||||
golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8=
|
||||
golang.org/x/net v0.54.0 h1:2zJIZAxAHV/OHCDTCOHAYehQzLfSXuf/5SoL/Dv6w/w=
|
||||
golang.org/x/net v0.54.0/go.mod h1:Sj4oj8jK6XmHpBZU/zWHw3BV3abl4Kvi+Ut7cQcY+cQ=
|
||||
golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
|
||||
golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
||||
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
173
handler.go
173
handler.go
@ -8,6 +8,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
@ -24,7 +25,7 @@ func (rh *RouteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
tracker := timer.Start()
|
||||
requestId := r.Header.Get(discover.HeaderRequestID)
|
||||
if requestId == "" {
|
||||
requestId = MakeId(12)
|
||||
requestId = IDMaker.Get10Bytes14MPerSecond()
|
||||
r.Header.Set(discover.HeaderRequestID, requestId)
|
||||
}
|
||||
|
||||
@ -32,24 +33,117 @@ func (rh *RouteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
request.Id = requestId
|
||||
response := NewResponse(w)
|
||||
response.Id = requestId
|
||||
defer response.checkWriteHeader()
|
||||
requestLogger := log.New(requestId)
|
||||
|
||||
// 0. 延迟处理日志与状态检查
|
||||
var s *webServiceType
|
||||
var authLevel int
|
||||
var priority int
|
||||
var args = make(map[string]any)
|
||||
|
||||
defer func() {
|
||||
// 捕捉 Panic
|
||||
if err := recover(); err != nil {
|
||||
requestLogger.Error("panic recovered", "requestId", requestId, "path", r.URL.Path, "error", err, "stack", string(debug.Stack()))
|
||||
if !response.changed {
|
||||
response.WriteHeader(http.StatusInternalServerError)
|
||||
outputResult(response, "internal server error")
|
||||
}
|
||||
}
|
||||
|
||||
response.checkWriteHeader()
|
||||
|
||||
// 记录日志
|
||||
if (s == nil || !s.options.NoLog200 || response.Code != 200) &&
|
||||
!(Config.NoLogGets && r.Method == http.MethodGet && response.Code == 200) {
|
||||
|
||||
scheme := "http"
|
||||
if r.TLS != nil {
|
||||
scheme = "https"
|
||||
}
|
||||
usedTime := float32(tracker.Stop().Seconds())
|
||||
|
||||
// 过滤请求头
|
||||
reqHeaders := make(map[string]string)
|
||||
noLogHeaders := strings.Split(Config.NoLogHeaders, ",")
|
||||
for k, v := range r.Header {
|
||||
skip := false
|
||||
for _, nl := range noLogHeaders {
|
||||
if nl != "" && strings.EqualFold(k, strings.TrimSpace(nl)) {
|
||||
skip = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !skip {
|
||||
reqHeaders[k] = strings.Join(v, ", ")
|
||||
}
|
||||
}
|
||||
|
||||
// 过滤响应头
|
||||
respHeaders := make(map[string]string)
|
||||
for k, v := range response.Header() {
|
||||
respHeaders[k] = strings.Join(v, ", ")
|
||||
}
|
||||
|
||||
// 处理响应内容截断
|
||||
var respData any
|
||||
if response.Code != 200 {
|
||||
if len(response.body) < 1024 {
|
||||
respData = string(response.body)
|
||||
} else {
|
||||
respData = string(response.body[:1024]) + "..."
|
||||
}
|
||||
} else if Config.NoLogOutputFields != "" {
|
||||
// 简单的字段过滤逻辑 (如果是 JSON 对象)
|
||||
// 这里可以根据 Config.NoLogOutputFields, LogOutputArrayNum, LogOutputFieldSize 进行更复杂的处理
|
||||
// 暂按字符串截断处理
|
||||
if len(response.body) > 0 {
|
||||
respData = "[content hidden or truncated]"
|
||||
}
|
||||
}
|
||||
|
||||
LogRequest(requestLogger, func(entry *RequestLog) {
|
||||
entry.Method = r.Method
|
||||
entry.Path = r.URL.Path
|
||||
entry.Host = hostOnly(r.Host)
|
||||
entry.Scheme = scheme
|
||||
entry.Proto = r.Proto
|
||||
entry.ClientIp = request.ClientIp()
|
||||
entry.ServerId = serverId
|
||||
entry.App = Config.App
|
||||
entry.FromApp = r.Header.Get(discover.HeaderFromApp)
|
||||
entry.FromNode = r.Header.Get(discover.HeaderFromNode)
|
||||
entry.DeviceId = request.DeviceId()
|
||||
entry.SessionId = request.SessionId()
|
||||
entry.ClientAppName = r.Header.Get(discover.HeaderClientAppName)
|
||||
entry.ClientAppVersion = r.Header.Get(discover.HeaderClientAppVersion)
|
||||
entry.AuthLevel = authLevel
|
||||
entry.Priority = priority
|
||||
entry.RequestHeaders = reqHeaders
|
||||
entry.RequestData = args
|
||||
entry.ResponseCode = response.Code
|
||||
entry.UsedTime = usedTime
|
||||
entry.ResponseHeaders = respHeaders
|
||||
entry.ResponseData = respData
|
||||
entry.ResponseDataLength = uint(len(response.body))
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
// 处理 SessionId 和 DeviceId
|
||||
handleClientKeys(request, response)
|
||||
|
||||
requestLogger := log.New(requestId)
|
||||
|
||||
// 0. 处理重写 (Rewrite)
|
||||
// 1. 处理重写 (Rewrite)
|
||||
if processRewrite(request, response, requestLogger) {
|
||||
return
|
||||
}
|
||||
|
||||
// 处理代理 (Proxy)
|
||||
// 2. 处理代理 (Proxy)
|
||||
if processProxy(request, response, requestLogger) {
|
||||
return
|
||||
}
|
||||
|
||||
// 1. 路由匹配
|
||||
// 3. 路由匹配
|
||||
path := r.URL.Path
|
||||
host := r.Host
|
||||
|
||||
@ -58,13 +152,13 @@ func (rh *RouteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s, ws := findService(r.Method, host, path)
|
||||
var ws *websocketServiceType
|
||||
s, ws = findService(r.Method, host, path)
|
||||
|
||||
// 2. 参数解析 (Form & Body)
|
||||
args := make(map[string]any)
|
||||
// 4. 参数解析 (Form & Body)
|
||||
parseRequestArgs(request, args)
|
||||
|
||||
// 3. 前置过滤器
|
||||
// 5. 前置过滤器
|
||||
var result any
|
||||
for _, filter := range inFilters {
|
||||
result = filter(&args, request, response, requestLogger)
|
||||
@ -73,14 +167,12 @@ func (rh *RouteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
authLevel := 0
|
||||
priority := 0
|
||||
if s != nil {
|
||||
authLevel = s.authLevel
|
||||
priority = s.options.Priority
|
||||
}
|
||||
|
||||
// 4. 处理业务执行 (WS 或 Web)
|
||||
// 6. 处理业务执行 (WS 或 Web)
|
||||
if result == nil {
|
||||
if ws != nil {
|
||||
authLevel = ws.authLevel
|
||||
@ -101,11 +193,11 @@ func (rh *RouteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if s == nil && result == nil {
|
||||
if s == nil && result == nil && !response.changed {
|
||||
response.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
|
||||
// 5. 后置过滤器
|
||||
// 7. 后置过滤器
|
||||
for _, filter := range outFilters {
|
||||
newResult, done := filter(args, request, response, result, requestLogger)
|
||||
if newResult != nil {
|
||||
@ -116,50 +208,13 @@ func (rh *RouteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 输出结果
|
||||
// 8. 输出结果
|
||||
outputResult(response, result)
|
||||
|
||||
// 7. 记录日志
|
||||
if s == nil || !s.options.NoLog200 || response.Code != 200 {
|
||||
scheme := "http"
|
||||
if r.TLS != nil {
|
||||
scheme = "https"
|
||||
}
|
||||
usedTime := float32(tracker.Stop().Seconds())
|
||||
|
||||
// 获取一些 Header 信息
|
||||
reqHeaders := make(map[string]string)
|
||||
for k, v := range r.Header {
|
||||
reqHeaders[k] = strings.Join(v, ", ")
|
||||
}
|
||||
respHeaders := make(map[string]string)
|
||||
for k, v := range response.Header() {
|
||||
respHeaders[k] = strings.Join(v, ", ")
|
||||
}
|
||||
|
||||
// 限制记录的 Body 长度
|
||||
respData := ""
|
||||
if response.Code != 200 {
|
||||
if len(response.body) < 1024 {
|
||||
respData = string(response.body)
|
||||
} else {
|
||||
respData = string(response.body[:1024]) + "..."
|
||||
}
|
||||
}
|
||||
|
||||
logRequest(
|
||||
requestLogger,
|
||||
r.Method, path, host, scheme, r.Proto,
|
||||
request.ClientIp(), serverId, "", "", // app, node 暂无
|
||||
r.Header.Get(discover.HeaderFromApp), r.Header.Get(discover.HeaderFromNode),
|
||||
"", request.DeviceId(), request.SessionId(), requestId,
|
||||
request.Header.Get(discover.HeaderClientAppName), request.Header.Get(discover.HeaderClientAppVersion),
|
||||
authLevel, priority,
|
||||
reqHeaders, args,
|
||||
response.Code, usedTime,
|
||||
respHeaders, respData, uint(len(response.body)),
|
||||
)
|
||||
}
|
||||
func hostOnly(host string) string {
|
||||
h, _, _ := strings.Cut(host, ":")
|
||||
return h
|
||||
}
|
||||
|
||||
func findService(method, host, path string) (*webServiceType, *websocketServiceType) {
|
||||
@ -344,7 +399,7 @@ func handleClientKeys(request *Request, response *Response) {
|
||||
if sessionIdMaker != nil {
|
||||
sessionId = sessionIdMaker()
|
||||
} else {
|
||||
sessionId = MakeId(14)
|
||||
sessionId = IDMaker.Get11Bytes900MPerSecond()
|
||||
}
|
||||
if !Config.SessionWithoutCookie {
|
||||
http.SetCookie(response.Writer, &http.Cookie{
|
||||
@ -368,7 +423,7 @@ func handleClientKeys(request *Request, response *Response) {
|
||||
}
|
||||
}
|
||||
if deviceId == "" {
|
||||
deviceId = MakeId(14)
|
||||
deviceId = IDMaker.Get11Bytes900MPerSecond()
|
||||
if !Config.DeviceWithoutCookie {
|
||||
http.SetCookie(response.Writer, &http.Cookie{
|
||||
Name: usedDeviceIdKey,
|
||||
|
||||
@ -65,3 +65,19 @@ func TestServeHTTP_VerifyFailed(t *testing.T) {
|
||||
t.Errorf("Expected status 400, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeHTTP_Panic(t *testing.T) {
|
||||
Host("*").GET("/panic", func() string {
|
||||
panic("intentional panic")
|
||||
})
|
||||
|
||||
rh := &RouteHandler{}
|
||||
req := httptest.NewRequest("GET", "/panic", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
rh.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("Expected status 500, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
142
log.go
142
log.go
@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"apigo.cc/go/cast"
|
||||
"apigo.cc/go/log"
|
||||
)
|
||||
|
||||
@ -71,24 +70,8 @@ func (l *RequestLog) Reset() {
|
||||
l.ResponseData = nil
|
||||
}
|
||||
|
||||
// RequestLog 调用封装
|
||||
func logRequest(
|
||||
logger *log.Logger,
|
||||
method, path, host, scheme, proto string,
|
||||
clientIp, serverId, app, node string,
|
||||
fromApp, fromNode string,
|
||||
userId, deviceId, sessionId, requestId string,
|
||||
clientAppName, clientAppVersion string,
|
||||
authLevel, priority int,
|
||||
reqHeaders map[string]string,
|
||||
reqData map[string]any,
|
||||
responseCode int,
|
||||
usedTime float32,
|
||||
respHeaders map[string]string,
|
||||
responseData string,
|
||||
responseDataLength uint,
|
||||
extra ...any,
|
||||
) {
|
||||
// LogRequest 闭环式日志调用封装
|
||||
func LogRequest(logger *log.Logger, fn func(entry *RequestLog)) {
|
||||
if !logger.CheckLevel(log.INFO) {
|
||||
return
|
||||
}
|
||||
@ -96,128 +79,13 @@ func logRequest(
|
||||
entry := log.GetEntry[RequestLog]()
|
||||
logger.FillBase(entry.GetBaseLog(), log.LogTypeRequest)
|
||||
|
||||
entry.Method = method
|
||||
entry.Path = path
|
||||
entry.Host = host
|
||||
entry.Scheme = scheme
|
||||
entry.Proto = proto
|
||||
entry.ClientIp = clientIp
|
||||
entry.ServerId = serverId
|
||||
entry.App = app
|
||||
entry.Node = node
|
||||
entry.FromApp = fromApp
|
||||
entry.FromNode = fromNode
|
||||
entry.UserId = userId
|
||||
entry.DeviceId = deviceId
|
||||
entry.SessionId = sessionId
|
||||
entry.ClientAppName = clientAppName
|
||||
entry.ClientAppVersion = clientAppVersion
|
||||
entry.AuthLevel = authLevel
|
||||
entry.Priority = priority
|
||||
entry.RequestHeaders = reqHeaders
|
||||
entry.RequestData = reqData
|
||||
entry.ResponseCode = responseCode
|
||||
entry.UsedTime = usedTime
|
||||
entry.ResponseHeaders = respHeaders
|
||||
entry.ResponseData = responseData
|
||||
entry.ResponseDataLength = responseDataLength
|
||||
if len(extra) > 0 {
|
||||
cast.FillMap(&entry.Extra, extra)
|
||||
}
|
||||
// 执行业务赋值逻辑
|
||||
fn(entry)
|
||||
|
||||
// 统一发送
|
||||
logger.Log(entry)
|
||||
}
|
||||
|
||||
type TaskLog struct {
|
||||
log.BaseLog
|
||||
Task string `log:"pos:6"`
|
||||
UsedTime float32 `log:"pos:7"`
|
||||
Success bool `log:"pos:8"`
|
||||
Message string `log:"pos:9"`
|
||||
}
|
||||
|
||||
func (l *TaskLog) Reset() {
|
||||
l.BaseLog.Reset()
|
||||
l.Task = ""
|
||||
l.UsedTime = 0
|
||||
l.Success = false
|
||||
l.Message = ""
|
||||
}
|
||||
|
||||
type MonitorLog struct {
|
||||
log.BaseLog
|
||||
Target string `log:"pos:6"`
|
||||
Status int `log:"pos:7"`
|
||||
Message string `log:"pos:8"`
|
||||
}
|
||||
|
||||
func (l *MonitorLog) Reset() {
|
||||
l.BaseLog.Reset()
|
||||
l.Target = ""
|
||||
l.Status = 0
|
||||
l.Message = ""
|
||||
}
|
||||
|
||||
type StatisticLog struct {
|
||||
log.BaseLog
|
||||
Category string `log:"pos:6"`
|
||||
Item string `log:"pos:7"`
|
||||
Value float64 `log:"pos:8"`
|
||||
}
|
||||
|
||||
func (l *StatisticLog) Reset() {
|
||||
l.BaseLog.Reset()
|
||||
l.Category = ""
|
||||
l.Item = ""
|
||||
l.Value = 0
|
||||
}
|
||||
|
||||
func logTask(logger *log.Logger, taskName string, usedTime float32, success bool, message string, extra ...any) {
|
||||
if logger.CheckLevel(log.INFO) {
|
||||
entry := log.GetEntry[TaskLog]()
|
||||
logger.FillBase(entry.GetBaseLog(), log.LogTypeTask)
|
||||
entry.Task = taskName
|
||||
entry.UsedTime = usedTime
|
||||
entry.Success = success
|
||||
entry.Message = message
|
||||
if len(extra) > 0 {
|
||||
cast.FillMap(&entry.Extra, extra)
|
||||
}
|
||||
logger.Log(entry)
|
||||
}
|
||||
}
|
||||
|
||||
func logMonitor(logger *log.Logger, target string, status int, message string, extra ...any) {
|
||||
if logger.CheckLevel(log.INFO) {
|
||||
entry := log.GetEntry[MonitorLog]()
|
||||
logger.FillBase(entry.GetBaseLog(), log.LogTypeMonitor)
|
||||
entry.Target = target
|
||||
entry.Status = status
|
||||
entry.Message = message
|
||||
if len(extra) > 0 {
|
||||
cast.FillMap(&entry.Extra, extra)
|
||||
}
|
||||
logger.Log(entry)
|
||||
}
|
||||
}
|
||||
|
||||
func logStatistic(logger *log.Logger, category, item string, value float64, extra ...any) {
|
||||
if logger.CheckLevel(log.INFO) {
|
||||
entry := log.GetEntry[StatisticLog]()
|
||||
logger.FillBase(entry.GetBaseLog(), log.LogTypeStatistic)
|
||||
entry.Category = category
|
||||
entry.Item = item
|
||||
entry.Value = value
|
||||
if len(extra) > 0 {
|
||||
cast.FillMap(&entry.Extra, extra)
|
||||
}
|
||||
logger.Log(entry)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
log.RegisterType(log.LogTypeRequest, &RequestLog{})
|
||||
log.RegisterType(log.LogTypeTask, &TaskLog{})
|
||||
log.RegisterType(log.LogTypeMonitor, &MonitorLog{})
|
||||
log.RegisterType(log.LogTypeStatistic, &StatisticLog{})
|
||||
}
|
||||
|
||||
113
proxy.go
113
proxy.go
@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"apigo.cc/go/discover"
|
||||
gohttp "apigo.cc/go/http"
|
||||
"apigo.cc/go/log"
|
||||
"fmt"
|
||||
@ -18,23 +17,81 @@ type proxyType struct {
|
||||
fromPath string
|
||||
toApp string
|
||||
toPath string
|
||||
hasWildcard bool
|
||||
prefix string
|
||||
toPrefix string
|
||||
}
|
||||
|
||||
func parseTo(to string) (app, path string) {
|
||||
if to == "" {
|
||||
return "", "/"
|
||||
}
|
||||
|
||||
// 查找协议
|
||||
if strings.Contains(to, "://") {
|
||||
protocolPart, rest, _ := strings.Cut(to, "://")
|
||||
// 协议后的第一个 /
|
||||
if firstSlash := strings.Index(rest, "/"); firstSlash != -1 {
|
||||
app = protocolPart + "://" + rest[:firstSlash]
|
||||
path = rest[firstSlash:]
|
||||
} else {
|
||||
app = to
|
||||
path = "/"
|
||||
}
|
||||
} else {
|
||||
// 无协议,第一个 / 之前是 app
|
||||
if firstSlash := strings.Index(to, "/"); firstSlash != -1 {
|
||||
app = to[:firstSlash]
|
||||
path = to[firstSlash:]
|
||||
} else {
|
||||
app = to
|
||||
path = "/"
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func parseProxyRule(authLevel int, path, toApp, toPath string, to string) *proxyType {
|
||||
if to != "" {
|
||||
toApp, toPath = parseTo(to)
|
||||
}
|
||||
|
||||
func (hc *HostContext) Proxy(authLevel int, path string, toApp, toPath string) *HostContext {
|
||||
p := &proxyType{authLevel: authLevel, fromPath: path, toApp: toApp, toPath: toPath}
|
||||
if strings.Contains(path, "(") {
|
||||
if strings.ContainsRune(path, '(') {
|
||||
matcher, err := regexp.Compile("^" + path + "$")
|
||||
if err == nil {
|
||||
p.matcher = matcher
|
||||
}
|
||||
} else if strings.HasSuffix(path, "/*") {
|
||||
p.hasWildcard = true
|
||||
p.prefix = path[:len(path)-1]
|
||||
if strings.HasSuffix(toPath, "/*") {
|
||||
p.toPrefix = toPath[:len(toPath)-1]
|
||||
} else {
|
||||
p.toPrefix = toPath
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (hc *HostContext) Proxy(authLevel int, path string, to string) *HostContext {
|
||||
p := parseProxyRule(authLevel, path, "", "", to)
|
||||
|
||||
hostPoliciesLock.Lock()
|
||||
defer hostPoliciesLock.Unlock()
|
||||
hostProxies[hc.host] = append(hostProxies[hc.host], p)
|
||||
codeProxies[hc.host] = append(codeProxies[hc.host], p)
|
||||
rebuildProxiesUnderLock(hc.host)
|
||||
return hc
|
||||
}
|
||||
|
||||
func rebuildProxiesUnderLock(host string) {
|
||||
var combined []*proxyType
|
||||
combined = append(combined, codeProxies[host]...)
|
||||
combined = append(combined, fileProxies[host]...)
|
||||
combined = append(combined, dynamicProxies[host]...)
|
||||
hostProxies[host] = combined
|
||||
}
|
||||
|
||||
var httpClientPool *gohttp.Client
|
||||
|
||||
func findProxy(request *Request) (int, *string, *string, string) {
|
||||
@ -63,12 +120,7 @@ func findProxy(request *Request) (int, *string, *string, string) {
|
||||
}
|
||||
|
||||
for _, pi := range proxies {
|
||||
if pi.matcher == nil {
|
||||
if pi.fromPath == requestPath {
|
||||
toPath := pi.toPath + queryString
|
||||
return pi.authLevel, &pi.toApp, &toPath, h
|
||||
}
|
||||
} else {
|
||||
if pi.matcher != nil {
|
||||
finds := pi.matcher.FindAllStringSubmatch(requestPath, 1)
|
||||
if len(finds) > 0 {
|
||||
toApp := pi.toApp
|
||||
@ -80,6 +132,18 @@ func findProxy(request *Request) (int, *string, *string, string) {
|
||||
toPath += queryString
|
||||
return pi.authLevel, &toApp, &toPath, h
|
||||
}
|
||||
} else if pi.hasWildcard {
|
||||
if strings.HasPrefix(requestPath, pi.prefix) {
|
||||
suffix := requestPath[len(pi.prefix):]
|
||||
toPath := pi.toPrefix + suffix + queryString
|
||||
toApp := pi.toApp
|
||||
return pi.authLevel, &toApp, &toPath, h
|
||||
}
|
||||
} else {
|
||||
if pi.fromPath == requestPath {
|
||||
toPath := pi.toPath + queryString
|
||||
return pi.authLevel, &pi.toApp, &toPath, h
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -117,7 +181,12 @@ func processProxy(request *Request, response *Response, logger *log.Logger) bool
|
||||
copyResponse(res, response, logger)
|
||||
} else {
|
||||
// Discover 代理
|
||||
caller := discover.NewCaller(request.Request, logger)
|
||||
if GlobalDiscoverer == nil {
|
||||
logger.Error("proxy failed: GlobalDiscoverer is not initialized")
|
||||
response.WriteHeader(http.StatusBadGateway)
|
||||
return true
|
||||
}
|
||||
caller := GlobalDiscoverer.NewCaller(request.Request, logger)
|
||||
caller.NoBody = true
|
||||
res, _ := caller.ManualDoWithNode(request.Method, app, "", path, request.Body)
|
||||
copyResponse(res, response, logger)
|
||||
@ -158,3 +227,25 @@ func copyResponse(res *gohttp.Result, response *Response, logger *log.Logger) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ProxyRule 定义了外部传递或 Redis 中获取的代理规则配置
|
||||
type ProxyRule struct {
|
||||
Path string // 匹配路径或正则,支持变量捕获如 ^/api/(.*)$
|
||||
AuthLevel int // 所需鉴权级别
|
||||
To string // 目标地址,格式为 "app/path" 或 "http://url/path" (支持后缀 /* 映射)
|
||||
ToApp string // [Deprecated] 目标 AppName 或完整 URL (可含 $1 变量替换)
|
||||
ToPath string // [Deprecated] 目标路径 (可含 $1 变量替换)
|
||||
}
|
||||
|
||||
// ReplaceProxies 使用全量指针替换的方式 (Copy-on-Write) 无缝更新指定 host 的动态代理规则。
|
||||
func ReplaceProxies(host string, rules []ProxyRule) {
|
||||
newProxies := make([]*proxyType, 0, len(rules))
|
||||
for _, r := range rules {
|
||||
newProxies = append(newProxies, parseProxyRule(r.AuthLevel, r.Path, r.ToApp, r.ToPath, r.To))
|
||||
}
|
||||
|
||||
hostPoliciesLock.Lock()
|
||||
defer hostPoliciesLock.Unlock()
|
||||
dynamicProxies[host] = newProxies
|
||||
rebuildProxiesUnderLock(host)
|
||||
}
|
||||
|
||||
@ -43,7 +43,7 @@ func TestProxyDirect(t *testing.T) {
|
||||
defer backend.Close()
|
||||
|
||||
// 注册代理规则
|
||||
Host("*").Proxy(0, "/proxy", backend.URL, "/hello")
|
||||
Host("*").Proxy(0, "/proxy", backend.URL+"/hello")
|
||||
|
||||
rh := &RouteHandler{}
|
||||
req := httptest.NewRequest("GET", "/proxy", nil)
|
||||
|
||||
33
reload.go
Normal file
33
reload.go
Normal file
@ -0,0 +1,33 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"apigo.cc/go/log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
reloadHooks []func() error
|
||||
reloadLock sync.RWMutex
|
||||
)
|
||||
|
||||
// OnReload 注册一个在接收到 SIGHUP 信号时触发的重新加载钩子
|
||||
func OnReload(handler func() error) {
|
||||
reloadLock.Lock()
|
||||
defer reloadLock.Unlock()
|
||||
reloadHooks = append(reloadHooks, handler)
|
||||
}
|
||||
|
||||
// triggerReload 触发所有注册的重新加载钩子
|
||||
func triggerReload() error {
|
||||
reloadLock.RLock()
|
||||
hooks := make([]func() error, len(reloadHooks))
|
||||
copy(hooks, reloadHooks)
|
||||
reloadLock.RUnlock()
|
||||
|
||||
for _, hook := range hooks {
|
||||
if err := hook(); err != nil {
|
||||
log.DefaultLogger.Error("reload hook failed", "error", err.Error())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
77
rewrite.go
77
rewrite.go
@ -12,24 +12,51 @@ type rewriteType struct {
|
||||
matcher *regexp.Regexp
|
||||
fromPath string
|
||||
toPath string
|
||||
hasWildcard bool
|
||||
prefix string
|
||||
toPrefix string
|
||||
}
|
||||
|
||||
func (hc *HostContext) Rewrite(path string, toPath string) *HostContext {
|
||||
s := &rewriteType{fromPath: path, toPath: toPath}
|
||||
|
||||
if strings.ContainsRune(path, '(') {
|
||||
matcher, err := regexp.Compile("^" + path + "$")
|
||||
func parseRewriteRule(fromPath, toPath, to string) *rewriteType {
|
||||
if to != "" {
|
||||
toPath = to
|
||||
}
|
||||
s := &rewriteType{fromPath: fromPath, toPath: toPath}
|
||||
if strings.ContainsRune(fromPath, '(') {
|
||||
matcher, err := regexp.Compile("^" + fromPath + "$")
|
||||
if err == nil {
|
||||
s.matcher = matcher
|
||||
}
|
||||
} else if strings.HasSuffix(fromPath, "/*") {
|
||||
s.hasWildcard = true
|
||||
s.prefix = fromPath[:len(fromPath)-1]
|
||||
if strings.HasSuffix(toPath, "/*") {
|
||||
s.toPrefix = toPath[:len(toPath)-1]
|
||||
} else {
|
||||
s.toPrefix = toPath
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (hc *HostContext) Rewrite(path string, to string) *HostContext {
|
||||
s := parseRewriteRule(path, "", to)
|
||||
|
||||
hostPoliciesLock.Lock()
|
||||
defer hostPoliciesLock.Unlock()
|
||||
hostRewrites[hc.host] = append(hostRewrites[hc.host], s)
|
||||
codeRewrites[hc.host] = append(codeRewrites[hc.host], s)
|
||||
rebuildRewritesUnderLock(hc.host)
|
||||
return hc
|
||||
}
|
||||
|
||||
func rebuildRewritesUnderLock(host string) {
|
||||
var combined []*rewriteType
|
||||
combined = append(combined, codeRewrites[host]...)
|
||||
combined = append(combined, fileRewrites[host]...)
|
||||
combined = append(combined, dynamicRewrites[host]...)
|
||||
hostRewrites[host] = combined
|
||||
}
|
||||
|
||||
func processRewrite(request *Request, response *Response, logger *log.Logger) bool {
|
||||
host := request.Host
|
||||
hostOnly, port, _ := strings.Cut(host, ":")
|
||||
@ -59,12 +86,7 @@ func processRewrite(request *Request, response *Response, logger *log.Logger) bo
|
||||
found := false
|
||||
rewriteToPath := ""
|
||||
|
||||
if ri.matcher == nil {
|
||||
if ri.fromPath == requestPath {
|
||||
rewriteToPath = ri.toPath
|
||||
found = true
|
||||
}
|
||||
} else {
|
||||
if ri.matcher != nil {
|
||||
finds := ri.matcher.FindAllStringSubmatch(request.RequestURI, 1)
|
||||
if len(finds) > 0 {
|
||||
toPath := ri.toPath
|
||||
@ -74,6 +96,17 @@ func processRewrite(request *Request, response *Response, logger *log.Logger) bo
|
||||
rewriteToPath = toPath
|
||||
found = true
|
||||
}
|
||||
} else if ri.hasWildcard {
|
||||
if strings.HasPrefix(requestPath, ri.prefix) {
|
||||
suffix := requestPath[len(ri.prefix):]
|
||||
rewriteToPath = ri.toPrefix + suffix
|
||||
found = true
|
||||
}
|
||||
} else {
|
||||
if ri.fromPath == requestPath {
|
||||
rewriteToPath = ri.toPath
|
||||
found = true
|
||||
}
|
||||
}
|
||||
|
||||
if found {
|
||||
@ -101,3 +134,23 @@ func processRewrite(request *Request, response *Response, logger *log.Logger) bo
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// RewriteRule 定义了外部传递的 URL 重写规则
|
||||
type RewriteRule struct {
|
||||
Path string // 原始路径或匹配正则,例如 ^/old/(.*)$
|
||||
To string // 目标路径或完整 URL,例如 /new/$1
|
||||
ToPath string // [Deprecated] 重写后的路径
|
||||
}
|
||||
|
||||
// ReplaceRewrites 使用 Copy-on-Write 机制原子地替换指定 host 下的动态重写规则。
|
||||
func ReplaceRewrites(host string, rules []RewriteRule) {
|
||||
newRewrites := make([]*rewriteType, 0, len(rules))
|
||||
for _, r := range rules {
|
||||
newRewrites = append(newRewrites, parseRewriteRule(r.Path, r.ToPath, r.To))
|
||||
}
|
||||
|
||||
hostPoliciesLock.Lock()
|
||||
defer hostPoliciesLock.Unlock()
|
||||
dynamicRewrites[host] = newRewrites
|
||||
rebuildRewritesUnderLock(host)
|
||||
}
|
||||
|
||||
279
server.go
279
server.go
@ -1,88 +1,279 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"apigo.cc/go/config"
|
||||
"apigo.cc/go/discover"
|
||||
"apigo.cc/go/log"
|
||||
"apigo.cc/go/redis"
|
||||
"apigo.cc/go/safe"
|
||||
"apigo.cc/go/starter"
|
||||
"context"
|
||||
"fmt"
|
||||
"golang.org/x/net/http2"
|
||||
"golang.org/x/net/http2/h2c"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// AsyncServer 异步服务实例
|
||||
type AsyncServer struct {
|
||||
// GlobalDiscoverer 供服务框架内部使用的发现实例
|
||||
var GlobalDiscoverer *discover.Discoverer
|
||||
|
||||
// WebServer 实现了 starter.Service 和 starter.Reloader 接口
|
||||
type WebServer struct {
|
||||
server *http.Server
|
||||
listener net.Listener
|
||||
Addr string
|
||||
stopChan chan os.Signal
|
||||
startChan chan bool
|
||||
useDiscover bool
|
||||
discoverer *discover.Discoverer
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
// AsyncStart 异步启动服务
|
||||
func AsyncStart() *AsyncServer {
|
||||
as := &AsyncServer{
|
||||
startChan: make(chan bool, 1),
|
||||
stopChan: make(chan os.Signal, 1),
|
||||
// NewWebServer 创建并返回一个新的 WebServer 实例
|
||||
func NewWebServer() *WebServer {
|
||||
return &WebServer{}
|
||||
}
|
||||
|
||||
go as.start()
|
||||
// Start 启动服务,实现 starter.Service 接口
|
||||
func (ws *WebServer) Start(ctx context.Context, logger *log.Logger) error {
|
||||
if logger == nil {
|
||||
logger = log.DefaultLogger
|
||||
}
|
||||
ws.logger = logger
|
||||
|
||||
<-as.startChan
|
||||
return as
|
||||
listenStr := Config.Listen
|
||||
ws.useDiscover = false
|
||||
|
||||
if listenStr == "" {
|
||||
listenStr = ":0,h2c"
|
||||
ws.useDiscover = true
|
||||
}
|
||||
|
||||
func (as *AsyncServer) start() {
|
||||
if Config.Listen == "" {
|
||||
Config.Listen = ":8080" // 默认端口
|
||||
// 解析第一个监听配置
|
||||
part := strings.Split(listenStr, "|")[0]
|
||||
addr, opts, _ := strings.Cut(part, ",")
|
||||
|
||||
protocol := ""
|
||||
for _, opt := range strings.Split(opts, ",") {
|
||||
opt = strings.ToLower(strings.TrimSpace(opt))
|
||||
if opt == "h2c" || opt == "h2" || opt == "http" || opt == "https" {
|
||||
protocol = opt
|
||||
}
|
||||
}
|
||||
|
||||
listener, err := net.Listen("tcp", Config.Listen)
|
||||
if protocol == "" {
|
||||
protocol = "http" // Default to http
|
||||
}
|
||||
|
||||
if !strings.Contains(addr, ":") {
|
||||
addr = ":" + addr
|
||||
}
|
||||
|
||||
// 检查是否需要启动服务发现
|
||||
if Config.App == "" {
|
||||
Config.App = GetDefaultName()
|
||||
}
|
||||
appName := Config.App
|
||||
if appName != "" || Config.Register != "" {
|
||||
ws.useDiscover = true
|
||||
}
|
||||
|
||||
// 初始化服务器唯一标识 (8位,物理上限 3,844/s)
|
||||
serverId = IDMaker.Get8Bytes4KPerSecond()
|
||||
|
||||
// 初始化分布式 ID 生成器
|
||||
if Config.IdServer != "" {
|
||||
rd := redis.GetRedis(Config.IdServer, log.New(serverId))
|
||||
if rd.Error == nil {
|
||||
IDMaker = redis.NewIDMaker(rd)
|
||||
}
|
||||
}
|
||||
|
||||
listener, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
log.DefaultLogger.Error("failed to listen", "addr", Config.Listen, "error", err.Error())
|
||||
as.startChan <- false
|
||||
return
|
||||
return fmt.Errorf("failed to listen on %s: %w", addr, err)
|
||||
}
|
||||
|
||||
as.listener = listener
|
||||
as.Addr = listener.Addr().String()
|
||||
serverAddr = as.Addr
|
||||
ws.listener = listener
|
||||
ws.Addr = listener.Addr().String()
|
||||
serverAddr = ws.Addr
|
||||
|
||||
as.server = &http.Server{
|
||||
Handler: &RouteHandler{},
|
||||
// 如果使用了随机端口且没有明确指定不需要服务发现,则开启
|
||||
if addr == ":0" || strings.HasSuffix(addr, ":0") {
|
||||
ws.useDiscover = true
|
||||
}
|
||||
|
||||
signal.Notify(as.stopChan, os.Interrupt, syscall.SIGTERM)
|
||||
h2s := &http2.Server{}
|
||||
var handler http.Handler = &RouteHandler{}
|
||||
if protocol == "h2c" {
|
||||
handler = h2c.NewHandler(handler, h2s)
|
||||
}
|
||||
|
||||
ws.server = &http.Server{
|
||||
Handler: handler,
|
||||
ReadTimeout: time.Duration(Config.ReadTimeout) * time.Millisecond,
|
||||
ReadHeaderTimeout: time.Duration(Config.ReadHeaderTimeout) * time.Millisecond,
|
||||
WriteTimeout: time.Duration(Config.WriteTimeout) * time.Millisecond,
|
||||
IdleTimeout: time.Duration(Config.IdleTimeout) * time.Millisecond,
|
||||
MaxHeaderBytes: Config.MaxHeaderBytes,
|
||||
}
|
||||
|
||||
// 启动服务发现
|
||||
if ws.useDiscover {
|
||||
_, port, _ := net.SplitHostPort(ws.Addr)
|
||||
ip := GetServerIp()
|
||||
discoverAddr := fmt.Sprintf("%s:%s", ip, port)
|
||||
|
||||
// 转换配置
|
||||
discConf := discover.Config{
|
||||
Weight: Config.Weight,
|
||||
CallRetryTimes: 10, // Default
|
||||
Calls: make(map[string]discover.CallConfig),
|
||||
}
|
||||
|
||||
if discConf.Weight <= 0 {
|
||||
discConf.Weight = 100
|
||||
}
|
||||
|
||||
for name, call := range Config.Calls {
|
||||
dc := discover.CallConfig{
|
||||
Http2: call.Http2,
|
||||
SSL: call.SSL,
|
||||
}
|
||||
if call.Timeout > 0 {
|
||||
dc.Timeout = time.Duration(call.Timeout) * time.Millisecond
|
||||
} else if Config.RedirectTimeout > 0 {
|
||||
dc.Timeout = time.Duration(Config.RedirectTimeout) * time.Millisecond
|
||||
}
|
||||
if call.Token != "" {
|
||||
dc.Token = safe.NewSafeBuf([]byte(call.Token))
|
||||
}
|
||||
discConf.Calls[name] = dc
|
||||
}
|
||||
|
||||
// 解析必需的 Register,支持环境变量 fallback
|
||||
registry := Config.Register
|
||||
if registry == "" {
|
||||
registry = os.Getenv("DISCOVER_REGISTRY")
|
||||
}
|
||||
if registry == "" {
|
||||
registry = "127.0.0.1:6379::15" // Default fallback
|
||||
}
|
||||
|
||||
ws.discoverer = discover.Start(registry, appName, discoverAddr, logger, discConf)
|
||||
GlobalDiscoverer = ws.discoverer
|
||||
if ws.discoverer != nil {
|
||||
logger.Info("discover registered", "app", appName, "addr", discoverAddr)
|
||||
}
|
||||
}
|
||||
|
||||
errChan := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
log.DefaultLogger.Info("service starting", "addr", as.Addr)
|
||||
as.startChan <- true
|
||||
if err := as.server.Serve(listener); err != nil && err != http.ErrServerClosed {
|
||||
log.DefaultLogger.Error("server error", "error", err.Error())
|
||||
logger.Info("service starting", "addr", ws.Addr, "proto", protocol)
|
||||
if err := ws.server.Serve(listener); err != nil && err != http.ErrServerClosed {
|
||||
errChan <- err
|
||||
}
|
||||
close(errChan)
|
||||
}()
|
||||
|
||||
// 短暂等待验证是否闪退
|
||||
select {
|
||||
case err := <-errChan:
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
|
||||
// Stop 停止服务
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop 停止服务,实现 starter.Service 接口
|
||||
func (ws *WebServer) Stop(ctx context.Context) error {
|
||||
logger := ws.logger
|
||||
if logger == nil {
|
||||
logger = log.DefaultLogger
|
||||
}
|
||||
logger.Info("service stopping")
|
||||
if ws.discoverer != nil {
|
||||
ws.discoverer.Stop()
|
||||
}
|
||||
if ws.server != nil {
|
||||
if err := ws.server.Shutdown(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
logger.Info("service stopped")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Status 检查服务健康状态,实现 starter.Service 接口
|
||||
func (ws *WebServer) Status() (string, error) {
|
||||
if ws.server == nil {
|
||||
return "", fmt.Errorf("server is not running")
|
||||
}
|
||||
return ws.Addr, nil
|
||||
}
|
||||
|
||||
// Reload 实现配置重新加载,实现 starter.Reloader 接口
|
||||
func (ws *WebServer) Reload() error {
|
||||
logger := ws.logger
|
||||
if logger == nil {
|
||||
logger = log.DefaultLogger
|
||||
}
|
||||
logger.Info("reloading configurations...")
|
||||
|
||||
// 重新加载配置文件中的策略
|
||||
appName := Config.App
|
||||
if appName == "" {
|
||||
appName = GetDefaultName()
|
||||
}
|
||||
if err := config.Load(&Config, appName); err != nil {
|
||||
logger.Error("failed to load config during reload", "error", err.Error())
|
||||
}
|
||||
ApplyConfig()
|
||||
|
||||
// 触发业务挂载的 Hook
|
||||
return triggerReload()
|
||||
}
|
||||
|
||||
// AsyncServer 兼容旧版异步服务实例
|
||||
type AsyncServer struct {
|
||||
*WebServer
|
||||
}
|
||||
|
||||
// Stop 兼容旧版的无参数停止方法
|
||||
func (as *AsyncServer) Stop() {
|
||||
log.DefaultLogger.Info("service stopping")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
stopTimeout := time.Duration(Config.StopTimeout) * time.Millisecond
|
||||
if stopTimeout <= 0 {
|
||||
stopTimeout = 5 * time.Second
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), stopTimeout)
|
||||
defer cancel()
|
||||
|
||||
if err := as.server.Shutdown(ctx); err != nil {
|
||||
log.DefaultLogger.Error("server shutdown error", "error", err.Error())
|
||||
}
|
||||
log.DefaultLogger.Info("service stopped")
|
||||
_ = as.WebServer.Stop(ctx)
|
||||
}
|
||||
|
||||
// Wait 等待服务结束 (信号监听)
|
||||
// AsyncStart 兼容旧版的异步启动方法
|
||||
func AsyncStart() *AsyncServer {
|
||||
ws := NewWebServer()
|
||||
_ = ws.Start(context.Background(), log.DefaultLogger)
|
||||
return &AsyncServer{WebServer: ws}
|
||||
}
|
||||
|
||||
// Wait 等待服务结束 (兼容旧版,直接阻塞)
|
||||
func (as *AsyncServer) Wait() {
|
||||
<-as.stopChan
|
||||
as.Stop()
|
||||
select {}
|
||||
}
|
||||
|
||||
// Start 同步启动服务
|
||||
// Start 兼容旧版的同步启动方法 (通过内部注册 starter 实现)
|
||||
func Start() {
|
||||
AsyncStart().Wait()
|
||||
stopTimeout := time.Duration(Config.StopTimeout) * time.Millisecond
|
||||
if stopTimeout <= 0 {
|
||||
stopTimeout = 5 * time.Second
|
||||
}
|
||||
starter.Register("web-server", NewWebServer(), 100, 5*time.Second, stopTimeout)
|
||||
starter.Run()
|
||||
}
|
||||
|
||||
81
service.go
81
service.go
@ -72,9 +72,19 @@ var (
|
||||
websocketServicesLock = sync.RWMutex{}
|
||||
websocketServicesList = make([]*websocketServiceType, 0)
|
||||
|
||||
// Rewrite 与 Proxy 按 Host 隔离
|
||||
// Rewrite 与 Proxy 按 Host 隔离 (编译后的最终路由)
|
||||
hostRewrites = make(map[string][]*rewriteType)
|
||||
hostProxies = make(map[string][]*proxyType)
|
||||
|
||||
// 按来源隔离的策略,避免互相覆盖
|
||||
codeProxies = make(map[string][]*proxyType)
|
||||
fileProxies = make(map[string][]*proxyType)
|
||||
dynamicProxies = make(map[string][]*proxyType)
|
||||
|
||||
codeRewrites = make(map[string][]*rewriteType)
|
||||
fileRewrites = make(map[string][]*rewriteType)
|
||||
dynamicRewrites = make(map[string][]*rewriteType)
|
||||
|
||||
hostPoliciesLock = sync.RWMutex{}
|
||||
|
||||
// 过滤器与拦截器
|
||||
@ -141,6 +151,26 @@ func Host(host string) *HostContext {
|
||||
return &HostContext{host: host}
|
||||
}
|
||||
|
||||
// Register 注册一个 Web 服务 (使用默认 Host "*")
|
||||
func Register(method, path string, serviceFunc any) *webServiceType {
|
||||
return Host("*").Register(method, path, serviceFunc)
|
||||
}
|
||||
|
||||
// RegisterWebsocket 注册一个 WebSocket 服务 (使用默认 Host "*")
|
||||
func RegisterWebsocket(path string, serviceFunc any) *websocketServiceType {
|
||||
return Host("*").WebSocket(path, serviceFunc)
|
||||
}
|
||||
|
||||
// Proxy 注册一个代理转发 (使用默认 Host "*")
|
||||
func Proxy(authLevel int, path string, to string) {
|
||||
Host("*").Proxy(authLevel, path, to)
|
||||
}
|
||||
|
||||
// Restful 注册一个符合 RESTful 规范的服务结构体 (使用默认 Host "*")
|
||||
func Restful(authLevel int, path string, serviceStruct any) {
|
||||
Host("*").Restful(authLevel, path, serviceStruct)
|
||||
}
|
||||
|
||||
func (hc *HostContext) Register(method, path string, serviceFunc any) *webServiceType {
|
||||
s, err := makeCachedService(serviceFunc)
|
||||
if err != nil {
|
||||
@ -250,16 +280,14 @@ func (gc *GroupContext) WebSocket(path string, serviceFunc any) *websocketServic
|
||||
return gc.hc.WebSocket(gc.prefix+path, serviceFunc)
|
||||
}
|
||||
|
||||
func (gc *GroupContext) Rewrite(path string, toPath string) *GroupContext {
|
||||
gc.hc.Rewrite(gc.prefix+path, toPath)
|
||||
func (gc *GroupContext) Rewrite(path string, to string) *GroupContext {
|
||||
gc.hc.Rewrite(gc.prefix+path, to)
|
||||
return gc
|
||||
}
|
||||
|
||||
func (gc *GroupContext) Proxy(authLevel int, path string, toApp, toPath string) *GroupContext {
|
||||
gc.hc.Proxy(authLevel, gc.prefix+path, toApp, toPath)
|
||||
func (gc *GroupContext) Proxy(authLevel int, path string, to string) *GroupContext {
|
||||
gc.hc.Proxy(authLevel, gc.prefix+path, to)
|
||||
return gc
|
||||
}
|
||||
|
||||
func (hc *HostContext) WebSocket(path string, serviceFunc any) *websocketServiceType {
|
||||
funcType := reflect.TypeOf(serviceFunc)
|
||||
if funcType.Kind() != reflect.Func {
|
||||
@ -283,6 +311,45 @@ func (hc *HostContext) WebSocket(path string, serviceFunc any) *websocketService
|
||||
return ws
|
||||
}
|
||||
|
||||
// Restful 自动根据方法名注册 RESTful 服务
|
||||
func (hc *HostContext) Restful(authLevel int, path string, serviceStruct any) {
|
||||
v := reflect.ValueOf(serviceStruct)
|
||||
t := v.Type()
|
||||
if t.Kind() == reflect.Ptr {
|
||||
t = t.Elem()
|
||||
}
|
||||
|
||||
for i := 0; i < v.NumMethod(); i++ {
|
||||
methodName := v.Type().Method(i).Name
|
||||
var httpMethod string
|
||||
switch {
|
||||
case strings.HasPrefix(methodName, "Get"):
|
||||
httpMethod = "GET"
|
||||
case strings.HasPrefix(methodName, "Post"):
|
||||
httpMethod = "POST"
|
||||
case strings.HasPrefix(methodName, "Put"):
|
||||
httpMethod = "PUT"
|
||||
case strings.HasPrefix(methodName, "Delete"):
|
||||
httpMethod = "DELETE"
|
||||
case strings.HasPrefix(methodName, "Patch"):
|
||||
httpMethod = "PATCH"
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
subPath := strings.ToLower(methodName[len(httpMethod):])
|
||||
if subPath == "" {
|
||||
hc.Register(httpMethod, path, v.Method(i).Interface()).Auth(authLevel)
|
||||
} else {
|
||||
fullPath := path
|
||||
if !strings.HasSuffix(fullPath, "/") {
|
||||
fullPath += "/"
|
||||
}
|
||||
hc.Register(httpMethod, fullPath+subPath, v.Method(i).Interface()).Auth(authLevel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// webServiceType 链式配置方法
|
||||
func (s *webServiceType) Auth(level int) *webServiceType {
|
||||
s.authLevel = level
|
||||
|
||||
55
static.go
55
static.go
@ -14,6 +14,11 @@ import (
|
||||
var (
|
||||
statics = make(map[string]*string)
|
||||
staticsByHost = make(map[string]map[string]*string)
|
||||
|
||||
codeStatics = make(map[string]map[string]*string)
|
||||
fileStatics = make(map[string]map[string]*string)
|
||||
dynamicStatics = make(map[string]map[string]*string)
|
||||
|
||||
staticsByHostLock = sync.RWMutex{}
|
||||
)
|
||||
|
||||
@ -33,13 +38,51 @@ func StaticByHost(path, rootPath, host string) {
|
||||
staticsByHostLock.Lock()
|
||||
defer staticsByHostLock.Unlock()
|
||||
|
||||
if host == "" {
|
||||
statics[path] = &rootPath
|
||||
} else {
|
||||
if staticsByHost[host] == nil {
|
||||
staticsByHost[host] = make(map[string]*string)
|
||||
if codeStatics[host] == nil {
|
||||
codeStatics[host] = make(map[string]*string)
|
||||
}
|
||||
staticsByHost[host][path] = &rootPath
|
||||
codeStatics[host][path] = &rootPath
|
||||
rebuildStaticsUnderLock(host)
|
||||
}
|
||||
|
||||
// ReplaceStatics 使用 Copy-on-Write 机制原子地替换指定 host 下的动态静态目录规则
|
||||
func ReplaceStatics(host string, config map[string]string) {
|
||||
newStatics := make(map[string]*string, len(config))
|
||||
for path, rootPath := range config {
|
||||
rp := rootPath
|
||||
if !filepath.IsAbs(rp) {
|
||||
if absPath, err := filepath.Abs(rp); err == nil {
|
||||
rp = absPath
|
||||
}
|
||||
}
|
||||
newStatics[path] = &rp
|
||||
}
|
||||
|
||||
staticsByHostLock.Lock()
|
||||
defer staticsByHostLock.Unlock()
|
||||
|
||||
dynamicStatics[host] = newStatics
|
||||
rebuildStaticsUnderLock(host)
|
||||
}
|
||||
|
||||
func rebuildStaticsUnderLock(host string) {
|
||||
combined := make(map[string]*string)
|
||||
|
||||
// 合并三种来源的静态路由
|
||||
for k, v := range codeStatics[host] {
|
||||
combined[k] = v
|
||||
}
|
||||
for k, v := range fileStatics[host] {
|
||||
combined[k] = v
|
||||
}
|
||||
for k, v := range dynamicStatics[host] {
|
||||
combined[k] = v
|
||||
}
|
||||
|
||||
if host == "" {
|
||||
statics = combined
|
||||
} else {
|
||||
staticsByHost[host] = combined
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
78
utility.go
78
utility.go
@ -2,60 +2,48 @@ package service
|
||||
|
||||
import (
|
||||
"apigo.cc/go/id"
|
||||
"apigo.cc/go/log"
|
||||
"apigo.cc/go/redis"
|
||||
"sync"
|
||||
"net"
|
||||
"os"
|
||||
"path"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
idMaker IDMakerInterface
|
||||
idMakerLock sync.Mutex
|
||||
)
|
||||
// IDMaker 全局 ID 生成器,默认指向单机版,启动后若配置了 IdServer 会被替换为 Redis 版
|
||||
var IDMaker = id.DefaultIDMaker
|
||||
|
||||
// IDMakerInterface ID 生成器接口
|
||||
type IDMakerInterface interface {
|
||||
Get(size int) string
|
||||
GetForMysql(size int) string
|
||||
GetForPostgreSQL(size int) string
|
||||
// GetDefaultName 获取默认应用名称
|
||||
func GetDefaultName() string {
|
||||
name := ""
|
||||
if info, ok := debug.ReadBuildInfo(); ok && info.Path != "" && info.Path != "command-line-arguments" {
|
||||
name = path.Base(info.Path)
|
||||
}
|
||||
if name == "" {
|
||||
name = path.Base(os.Args[0])
|
||||
}
|
||||
// 处理 Windows 下的 .exe 后缀
|
||||
name = strings.TrimSuffix(name, ".exe")
|
||||
return name
|
||||
}
|
||||
|
||||
func getIDMaker() IDMakerInterface {
|
||||
if idMaker != nil {
|
||||
return idMaker
|
||||
// GetServerIp 获取真实局域网 IP (UDP 8.8.8.8 伪拨号法)
|
||||
func GetServerIp() string {
|
||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||
if err == nil {
|
||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||
_ = conn.Close()
|
||||
return localAddr.IP.String()
|
||||
}
|
||||
|
||||
idMakerLock.Lock()
|
||||
defer idMakerLock.Unlock()
|
||||
|
||||
if idMaker != nil {
|
||||
return idMaker
|
||||
}
|
||||
|
||||
if Config.IdServer != "" {
|
||||
rd := redis.GetRedis(Config.IdServer, log.DefaultLogger)
|
||||
if rd.Error == nil {
|
||||
idMaker = redis.NewIDMaker(rd)
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err == nil {
|
||||
for _, a := range addrs {
|
||||
if an, ok := a.(*net.IPNet); ok {
|
||||
if an.IP.IsGlobalUnicast() {
|
||||
return an.IP.To4().String()
|
||||
}
|
||||
}
|
||||
|
||||
if idMaker == nil {
|
||||
idMaker = id.DefaultIDMaker
|
||||
}
|
||||
|
||||
return idMaker
|
||||
}
|
||||
|
||||
// MakeId 生成指定长度的 ID
|
||||
func MakeId(size int) string {
|
||||
return getIDMaker().Get(size)
|
||||
}
|
||||
|
||||
// MakeIdForMysql 生成适用于 MySQL 的有序 ID
|
||||
func MakeIdForMysql(size int) string {
|
||||
return getIDMaker().GetForMysql(size)
|
||||
}
|
||||
|
||||
// MakeIdForPostgreSQL 生成适用于 PostgreSQL 的有序 ID
|
||||
func MakeIdForPostgreSQL(size int) string {
|
||||
return getIDMaker().GetForPostgreSQL(size)
|
||||
return "127.0.0.1"
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user