Compare commits
No commits in common. "v1.5.2" and "main" have entirely different histories.
15
app.go
15
app.go
@ -22,6 +22,7 @@ var GatewayConf = Config{
|
||||
|
||||
// GatewayApp 定义网关应用,融合了 WebServer 的原生能力
|
||||
type GatewayApp struct {
|
||||
*service.WebServer
|
||||
rd *redis.Redis
|
||||
pubsubChannel string
|
||||
cancelPubSub context.CancelFunc
|
||||
@ -29,7 +30,9 @@ type GatewayApp struct {
|
||||
|
||||
// NewGatewayApp 创建 Gateway
|
||||
func NewGatewayApp() *GatewayApp {
|
||||
return &GatewayApp{}
|
||||
return &GatewayApp{
|
||||
WebServer: service.NewWebServer(),
|
||||
}
|
||||
}
|
||||
|
||||
// Start 启动网关服务 (实现 starter.Service)
|
||||
@ -59,8 +62,7 @@ func (g *GatewayApp) Start(ctx context.Context, logger *log.Logger) error {
|
||||
}
|
||||
|
||||
// 启动底层的 WebServer,处理所有实际的 HTTP 连接和发现注册
|
||||
service.Start()
|
||||
return nil
|
||||
return g.WebServer.Start(ctx, logger)
|
||||
}
|
||||
|
||||
// Stop 停止网关服务 (实现 starter.Service)
|
||||
@ -72,14 +74,13 @@ func (g *GatewayApp) Stop(ctx context.Context) error {
|
||||
g.rd.Unsubscribe(g.pubsubChannel)
|
||||
}
|
||||
// 停止底层的 WebServer
|
||||
// 注意:全局 service.Stop 目前在框架中可能由 starter 统一管理,或者手动调用 service.DefaultServer.Stop(ctx)
|
||||
return service.DefaultServer.Stop(ctx)
|
||||
return g.WebServer.Stop(ctx)
|
||||
}
|
||||
|
||||
// Reload 重载网关配置 (实现 starter.Reloader)
|
||||
func (g *GatewayApp) Reload() error {
|
||||
// 1. 触发底层 WebServer 的重载 (会重新加载本地 yaml 配置文件中的固定路由策略,并触发 OnReload 钩子)
|
||||
err := service.DefaultServer.Reload()
|
||||
err := g.WebServer.Reload()
|
||||
|
||||
// 2. 重新全量拉取 Redis 中的动态配置
|
||||
logger := log.DefaultLogger
|
||||
@ -91,7 +92,7 @@ func (g *GatewayApp) Reload() error {
|
||||
|
||||
// Status 返回网关运行状态
|
||||
func (g *GatewayApp) Status() (string, error) {
|
||||
addr, err := service.DefaultServer.Status()
|
||||
addr, err := g.WebServer.Status()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
66
app_test.go
66
app_test.go
@ -1,15 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"apigo.cc/go/log"
|
||||
"apigo.cc/go/redis"
|
||||
"apigo.cc/go/service"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
gohttp "net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@ -26,27 +27,23 @@ func TestGateway(t *testing.T) {
|
||||
// 清理测试数据
|
||||
rd.Do("DEL", "gateway:host:gw.test", "gateway:hosts")
|
||||
|
||||
// 2. 启动一个独立的后端测试服务 test-backend (不使用全局 DefaultServer)
|
||||
backend := service.NewWebServer()
|
||||
backend.Config.App = "test-backend"
|
||||
backend.Config.Listen = ":0"
|
||||
backend.Config.Register = registry
|
||||
// 2. 启动一个后端测试服务 test-backend
|
||||
service.Config.App = "test-backend"
|
||||
service.Config.Listen = ":0"
|
||||
service.Config.Register = registry
|
||||
|
||||
backend.Host("*").GET("/hello", func(req *service.Request) string {
|
||||
service.Host("*").GET("/hello", func(req *service.Request) string {
|
||||
return "hello from backend, path: " + req.RequestURI
|
||||
})
|
||||
backend.Host("*").GET("/hello/world", func(req *service.Request) string {
|
||||
service.Host("*").GET("/hello/world", func(req *service.Request) string {
|
||||
return "hello from backend, path: " + req.RequestURI
|
||||
})
|
||||
|
||||
backendCtx, backendCancel := context.WithCancel(context.Background())
|
||||
defer backendCancel()
|
||||
go backend.Start(backendCtx, log.DefaultLogger)
|
||||
defer backend.Stop(backendCtx)
|
||||
asBackend := service.AsyncStart()
|
||||
defer asBackend.Stop()
|
||||
time.Sleep(200 * time.Millisecond) // 等待后端启动和注册
|
||||
|
||||
time.Sleep(500 * time.Millisecond) // 等待后端启动和注册
|
||||
|
||||
// 3. 配置 Gateway (使用全局 service)
|
||||
// 3. 配置 Gateway
|
||||
GatewayConf.Redis = registry
|
||||
GatewayConf.Prefix = "gateway"
|
||||
|
||||
@ -80,31 +77,29 @@ func TestGateway(t *testing.T) {
|
||||
// 4. 启动 Gateway 应用
|
||||
app := NewGatewayApp()
|
||||
|
||||
// 设置独立的端口给 Gateway 的 WebServer
|
||||
// 设置独立的端口给 Gateway 的 WebServer 避免与 backend 冲突
|
||||
service.Config.App = "gateway"
|
||||
service.Config.Listen = ":0"
|
||||
// 重置发现,确保网关独立
|
||||
service.SetDiscovererForTest(nil)
|
||||
// 配置网关可以通过 discover 找到 test-backend (网关也需要开启 discover)
|
||||
service.Config.Register = registry
|
||||
service.Config.Calls = map[string]service.CallConfig{
|
||||
"test-backend": {Timeout: 1000},
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// 这里假设 GatewayApp.Start 内部会调用 service.Start(),
|
||||
// 而 service.Start() 在新版本中如果是阻塞的,我们需要在 goroutine 中运行
|
||||
go app.Start(ctx, log.DefaultLogger)
|
||||
ctx := context.Background()
|
||||
err := app.Start(ctx, log.DefaultLogger)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to start gateway: %v", err)
|
||||
}
|
||||
defer app.Stop(ctx)
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
gwAddr := service.DefaultServer.Addr
|
||||
if gwAddr == "" {
|
||||
t.Fatalf("Gateway address is empty")
|
||||
}
|
||||
_, gwPort, _ := net.SplitHostPort(gwAddr)
|
||||
_, gwPort, _ := net.SplitHostPort(app.Addr)
|
||||
|
||||
client := &gohttp.Client{Timeout: 5 * time.Second}
|
||||
client := &gohttp.Client{Timeout: 2 * time.Second}
|
||||
|
||||
// ============================================
|
||||
// 测试 1: 直接代理匹配 (Discover)
|
||||
@ -134,8 +129,8 @@ func TestGateway(t *testing.T) {
|
||||
res.Body.Close()
|
||||
body = string(b)
|
||||
}
|
||||
if err != nil || body != "hello from backend, path: /hello" {
|
||||
t.Fatalf("Proxy /api/ failed, got: %s", body)
|
||||
if err != nil || !strings.Contains(body, "hello from backend, path: /hello?a=1") {
|
||||
t.Fatalf("Proxy regexp failed, got: %s", body)
|
||||
}
|
||||
|
||||
// ============================================
|
||||
@ -150,8 +145,8 @@ func TestGateway(t *testing.T) {
|
||||
res.Body.Close()
|
||||
body = string(b)
|
||||
}
|
||||
if err != nil || body != "hello from backend, path: /hello/world" {
|
||||
t.Fatalf("Proxy /v2/* failed, got: %s", body)
|
||||
if err != nil || !strings.Contains(body, "hello from backend, path: /hello/world?x=2") {
|
||||
t.Fatalf("Proxy wildcard failed, got: %s", body)
|
||||
}
|
||||
|
||||
// ============================================
|
||||
@ -224,6 +219,7 @@ func TestGateway(t *testing.T) {
|
||||
t.Fatalf("Request failed: %v", err)
|
||||
}
|
||||
if res.StatusCode != 404 {
|
||||
t.Fatalf("Atomic update failed, old route still exists")
|
||||
t.Fatalf("Old proxy rule should be deleted (404), got status: %v", res.StatusCode)
|
||||
}
|
||||
res.Body.Close()
|
||||
}
|
||||
|
||||
34
go.mod
34
go.mod
@ -3,28 +3,28 @@ module apigo.cc/go/gateway
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
apigo.cc/go/cast v1.5.2
|
||||
apigo.cc/go/config v1.5.2
|
||||
apigo.cc/go/log v1.5.6
|
||||
apigo.cc/go/redis v1.5.4
|
||||
apigo.cc/go/service v1.5.14
|
||||
apigo.cc/go/starter v1.5.4
|
||||
apigo.cc/go/cast v1.5.0
|
||||
apigo.cc/go/config v1.5.1
|
||||
apigo.cc/go/log v1.5.5
|
||||
apigo.cc/go/redis v1.5.0
|
||||
apigo.cc/go/service v1.5.12
|
||||
apigo.cc/go/starter v1.5.3
|
||||
)
|
||||
|
||||
require apigo.cc/go/jsmod v1.5.2 // indirect
|
||||
require apigo.cc/go/jsmod v1.5.0 // indirect
|
||||
|
||||
require (
|
||||
apigo.cc/go/crypto v1.5.2 // indirect
|
||||
apigo.cc/go/discover v1.5.2 // indirect
|
||||
apigo.cc/go/encoding v1.5.3 // indirect
|
||||
apigo.cc/go/file v1.5.4 // indirect
|
||||
apigo.cc/go/http v1.5.2 // indirect
|
||||
apigo.cc/go/id v1.5.3 // indirect
|
||||
apigo.cc/go/rand v1.5.2 // indirect
|
||||
apigo.cc/go/safe v1.5.1 // indirect
|
||||
apigo.cc/go/shell v1.5.2 // indirect
|
||||
apigo.cc/go/crypto v1.5.0 // indirect
|
||||
apigo.cc/go/discover v1.5.0 // indirect
|
||||
apigo.cc/go/encoding v1.5.0 // indirect
|
||||
apigo.cc/go/file v1.5.0 // indirect
|
||||
apigo.cc/go/http v1.5.0 // indirect
|
||||
apigo.cc/go/id v1.5.0 // indirect
|
||||
apigo.cc/go/rand v1.5.0 // indirect
|
||||
apigo.cc/go/safe v1.5.0 // indirect
|
||||
apigo.cc/go/shell v1.5.0 // indirect
|
||||
apigo.cc/go/timer v1.5.0 // indirect
|
||||
apigo.cc/go/watch v1.5.1 // indirect
|
||||
apigo.cc/go/watch v1.5.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.10.1 // indirect
|
||||
github.com/gobwas/glob v0.2.3 // indirect
|
||||
github.com/gomodule/redigo v2.0.0+incompatible // indirect
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user