mirror of
https://github.com/tnb-labs/panel.git
synced 2026-09-01 14:55:12 +08:00
443516a2cf
依赖注入:
- 移除 google/wire(含 wire.go/wire_gen.go 与全部 ProviderSet),
改用 samber/do v2 单注入器 + 双入口惰性构建
- 贡献模型替代命令式注册:路由 routes:、命令 commands:、
任务 jobs: 前缀经 internal/registry 收集与校验
- 构造函数统一 func NewXxx(i do.Injector) (T, error)
三层架构:
- 补全用例层:每个 biz.XxxRepo 配 XxxUsecase,
service/command/job 表现层只依赖用例,不再直接引用仓储
路由与文档:
- route/http.go 按域拆为声明式 Endpoint 贡献,
Endpoint 承载登录白名单与端点限流语义
- 调试模式下提供 OpenAPI 3.1 文档:/openapi.json 与 /docs(Scalar),
从 validate 标签生成
目录与依赖:
- internal/http/{middleware,request,rule} 拍平至 internal/*
- CLI 命令拆至 internal/command
- 日志轮转 timberjack 换为 libtnb/logrotate
- 升级 validator/cron/sessions/gormstore/sqlite/securecookie
- 数据库迁移保持 gormigrate 不变
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
164 lines
4.4 KiB
Go
164 lines
4.4 KiB
Go
package apache
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"regexp"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/leonelquinteros/gotext"
|
|
"github.com/samber/do/v2"
|
|
"github.com/spf13/cast"
|
|
|
|
"github.com/acepanel/panel/v3/internal/app"
|
|
"github.com/acepanel/panel/v3/internal/service"
|
|
"github.com/acepanel/panel/v3/pkg/io"
|
|
"github.com/acepanel/panel/v3/pkg/shell"
|
|
"github.com/acepanel/panel/v3/pkg/systemctl"
|
|
"github.com/acepanel/panel/v3/pkg/tools"
|
|
"github.com/acepanel/panel/v3/pkg/types"
|
|
)
|
|
|
|
type App struct {
|
|
t *gotext.Locale
|
|
}
|
|
|
|
func NewApp(i do.Injector) (*App, error) {
|
|
return &App{
|
|
t: do.MustInvoke[*gotext.Locale](i),
|
|
}, nil
|
|
}
|
|
|
|
func (s *App) Route(r chi.Router) {
|
|
r.Get("/load", s.Load)
|
|
r.Get("/config", s.GetConfig)
|
|
r.Post("/config", s.SaveConfig)
|
|
r.Get("/error_log", s.ErrorLog)
|
|
r.Post("/clear_error_log", s.ClearErrorLog)
|
|
}
|
|
|
|
func (s *App) Status() string {
|
|
ok, _ := systemctl.Status("apache")
|
|
return types.AggregateAppStatus(ok)
|
|
}
|
|
|
|
func (s *App) GetConfig(w http.ResponseWriter, r *http.Request) {
|
|
config, err := io.Read(fmt.Sprintf("%s/server/apache/conf/httpd.conf", app.Root))
|
|
if err != nil {
|
|
service.Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
service.Success(w, config)
|
|
}
|
|
|
|
func (s *App) SaveConfig(w http.ResponseWriter, r *http.Request) {
|
|
req, err := service.Bind[UpdateConfig](r)
|
|
if err != nil {
|
|
service.Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
if err = io.Write(fmt.Sprintf("%s/server/apache/conf/httpd.conf", app.Root), req.Config, 0600); err != nil {
|
|
service.Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
if err = systemctl.Reload("apache"); err != nil {
|
|
_, err = shell.Execf("%s/server/apache/bin/apachectl configtest", app.Root)
|
|
service.Error(w, http.StatusInternalServerError, s.t.Get("failed to reload apache: %v", err))
|
|
return
|
|
}
|
|
|
|
service.Success(w, nil)
|
|
}
|
|
|
|
func (s *App) ErrorLog(w http.ResponseWriter, r *http.Request) {
|
|
service.Success(w, fmt.Sprintf("%s/%s", app.Root, "server/apache/logs/error_log"))
|
|
}
|
|
|
|
func (s *App) ClearErrorLog(w http.ResponseWriter, r *http.Request) {
|
|
if _, err := shell.Execf("cat /dev/null > %s/%s", app.Root, "server/apache/logs/error_log"); err != nil {
|
|
service.Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
service.Success(w, nil)
|
|
}
|
|
|
|
func (s *App) Load(w http.ResponseWriter, r *http.Request) {
|
|
status, err := shell.Execf("curl -s http://127.0.0.1/server_status?auto 2>/dev/null || true")
|
|
if err != nil {
|
|
service.Success(w, []types.NV{})
|
|
return
|
|
}
|
|
|
|
var data []types.NV
|
|
|
|
workers, err := shell.Execf("ps aux | grep httpd | grep -v grep | wc -l")
|
|
if err != nil {
|
|
service.Error(w, http.StatusInternalServerError, s.t.Get("failed to get apache workers: %v", err))
|
|
return
|
|
}
|
|
data = append(data, types.NV{
|
|
Name: s.t.Get("Workers"),
|
|
Value: workers,
|
|
})
|
|
|
|
out, err := shell.Execf("ps aux | grep httpd | grep -v grep | awk '{memsum+=$6};END {print memsum}'")
|
|
if err != nil {
|
|
service.Error(w, http.StatusInternalServerError, s.t.Get("failed to get apache workers: %v", err))
|
|
return
|
|
}
|
|
mem := tools.FormatBytes(cast.ToFloat64(out))
|
|
data = append(data, types.NV{
|
|
Name: s.t.Get("Memory"),
|
|
Value: mem,
|
|
})
|
|
|
|
// Parse server-status output
|
|
if match := regexp.MustCompile(`Total Accesses:\s*(\d+)`).FindStringSubmatch(status); len(match) == 2 {
|
|
data = append(data, types.NV{
|
|
Name: s.t.Get("Total Accesses"),
|
|
Value: match[1],
|
|
})
|
|
}
|
|
|
|
if match := regexp.MustCompile(`Total kBytes:\s*(\d+)`).FindStringSubmatch(status); len(match) == 2 {
|
|
data = append(data, types.NV{
|
|
Name: s.t.Get("Total Traffic"),
|
|
Value: tools.FormatBytes(cast.ToFloat64(match[1]) * 1024),
|
|
})
|
|
}
|
|
|
|
if match := regexp.MustCompile(`BusyWorkers:\s*(\d+)`).FindStringSubmatch(status); len(match) == 2 {
|
|
data = append(data, types.NV{
|
|
Name: s.t.Get("Busy Workers"),
|
|
Value: match[1],
|
|
})
|
|
}
|
|
|
|
if match := regexp.MustCompile(`IdleWorkers:\s*(\d+)`).FindStringSubmatch(status); len(match) == 2 {
|
|
data = append(data, types.NV{
|
|
Name: s.t.Get("Idle Workers"),
|
|
Value: match[1],
|
|
})
|
|
}
|
|
|
|
if match := regexp.MustCompile(`ReqPerSec:\s*([\d.]+)`).FindStringSubmatch(status); len(match) == 2 {
|
|
data = append(data, types.NV{
|
|
Name: s.t.Get("Requests/sec"),
|
|
Value: match[1],
|
|
})
|
|
}
|
|
|
|
if match := regexp.MustCompile(`BytesPerSec:\s*([\d.]+)`).FindStringSubmatch(status); len(match) == 2 {
|
|
data = append(data, types.NV{
|
|
Name: s.t.Get("Bytes/sec"),
|
|
Value: tools.FormatBytes(cast.ToFloat64(match[1])),
|
|
})
|
|
}
|
|
|
|
service.Success(w, data)
|
|
}
|