添加在线调试 api

This commit is contained in:
2025-03-08 10:59:31 +08:00
parent 053041ae34
commit 5786ac9d99
28 changed files with 236 additions and 539 deletions

View File

@@ -7,10 +7,12 @@ import (
"os"
"os/signal"
"proxy-server/pkg/utils"
"proxy-server/server/debug"
"proxy-server/server/fwd"
"proxy-server/server/pkg/env"
"proxy-server/server/pkg/log"
"proxy-server/server/pkg/orm"
"proxy-server/server/web"
"runtime"
"sync"
"syscall"
@@ -38,6 +40,45 @@ func Start() {
env.Init()
orm.Init()
// 退出信号
osQuit := make(chan os.Signal)
signal.Notify(osQuit, os.Interrupt, syscall.SIGTERM)
// 启动服务
slog.Info("启动服务")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg := sync.WaitGroup{}
wg.Add(1)
fwdQuit := make(chan struct{}, 1)
go func() {
defer wg.Done()
defer close(fwdQuit)
err := startFwdServer(ctx)
if err != nil {
slog.Error("代理服务发生错误", "err", err)
}
fwdQuit <- struct{}{}
}()
// 启动 web 服务
wg.Add(1)
apiQuit := make(chan struct{}, 1)
go func() {
defer wg.Done()
err := startWebServer(ctx)
if err != nil {
slog.Error("web 服务发生错误", "err", err)
}
apiQuit <- struct{}{}
}()
// debug
go func() {
debug.Start(ctx)
}()
// 性能监控
go func() {
runtime.SetBlockProfileRate(1)
@@ -47,44 +88,21 @@ func Start() {
}
}()
// 退出信号
osQuit := make(chan os.Signal)
signal.Notify(osQuit, os.Interrupt, syscall.SIGTERM)
// 启动服务
slog.Info("启动服务")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg := sync.WaitGroup{}
wg.Add(1)
errQuit := make(chan struct{}, 1)
defer close(errQuit)
go func() {
defer wg.Done()
err := startFwdServer(ctx)
if err != nil {
slog.Error("代理服务发生错误", "err", err)
}
errQuit <- struct{}{}
}()
// 等待退出信号
select {
case <-osQuit:
slog.Info("服务主动退出")
case <-errQuit:
slog.Warn("服务异常退出")
case <-fwdQuit:
slog.Warn("fwd 服务异常退出")
case <-apiQuit:
slog.Warn("web 服务异常退出")
}
// 退出服务
// 退出其他服务
cancel()
timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
wg.Wait()
select {
case <-utils.ChanWgWait(timeout, &wg):
slog.Info("服务已退出")
@@ -103,6 +121,6 @@ func startFwdServer(ctx context.Context) error {
return nil
}
func startWebServer(ctx context.Context) {
func startWebServer(ctx context.Context) error {
return web.Start(ctx)
}