refactor(api): use predefined structs for fixed API responses

- Add api/types.go with predefined response struct types

- Replace map[string]any with typed structs for all fixed-content API endpoints

- Use predefined instances for Healthcheck and Version handlers to reduce allocations

- Improve performance by avoiding per-request map allocations
This commit is contained in:
wjqserver 2026-04-11 22:39:40 +08:00
parent 3d01d4cd15
commit 8af0971f9e
2 changed files with 119 additions and 39 deletions

View file

@ -50,84 +50,80 @@ func InitHandleRouter(cfg *config.Config, r *touka.Engine, version string) {
}
func SizeLimitHandler(cfg *config.Config, c *touka.Context) {
sizeLimit := cfg.Server.SizeLimit
c.SetHeader("Content-Type", "application/json")
c.JSON(200, (map[string]any{
"MaxResponseBodySize": sizeLimit,
}))
c.JSON(200, SizeLimitResponse{
MaxResponseBodySize: cfg.Server.SizeLimit,
})
}
func WhiteListStatusHandler(cfg *config.Config, c *touka.Context) {
c.SetHeader("Content-Type", "application/json")
c.JSON(200, (map[string]any{
"Whitelist": cfg.Whitelist.Enabled,
}))
c.JSON(200, WhitelistStatusResponse{
Whitelist: cfg.Whitelist.Enabled,
})
}
func BlackListStatusHandler(cfg *config.Config, c *touka.Context) {
c.SetHeader("Content-Type", "application/json")
c.JSON(200, (map[string]any{
"Blacklist": cfg.Blacklist.Enabled,
}))
c.JSON(200, BlacklistStatusResponse{
Blacklist: cfg.Blacklist.Enabled,
})
}
func CorsStatusHandler(cfg *config.Config, c *touka.Context) {
c.SetHeader("Content-Type", "application/json")
c.JSON(200, (map[string]any{
"Cors": cfg.Server.Cors,
}))
c.JSON(200, CorsStatusResponse{
Cors: cfg.Server.Cors,
})
}
func HealthcheckHandler(c *touka.Context) {
c.SetHeader("Content-Type", "application/json")
c.JSON(200, (map[string]any{
"Status": "OK",
"Repo": "WJQSERVER-STUDIO/GHProxy",
"Author": "WJQSERVER-STUDIO",
}))
// 复制预定义的固定响应,避免重复分配
resp := baseHealthcheckResponse
c.JSON(200, resp)
}
func VersionHandler(c *touka.Context, version string) {
c.SetHeader("Content-Type", "application/json")
c.JSON(200, (map[string]any{
"Version": version,
"Repo": "WJQSERVER-STUDIO/GHProxy",
"Author": "WJQSERVER-STUDIO",
}))
// 复制预定义的固定响应并填充动态字段
resp := baseVersionResponse
resp.Version = version
c.JSON(200, resp)
}
func RateLimitStatusHandler(cfg *config.Config, c *touka.Context) {
c.SetHeader("Content-Type", "application/json")
c.JSON(200, (map[string]any{
"RateLimit": cfg.RateLimit.Enabled,
}))
c.JSON(200, RateLimitStatusResponse{
RateLimit: cfg.RateLimit.Enabled,
})
}
func RateLimitLimitHandler(cfg *config.Config, c *touka.Context) {
c.SetHeader("Content-Type", "application/json")
c.JSON(200, (map[string]any{
"RatePerMinute": cfg.RateLimit.RatePerMinute,
}))
c.JSON(200, RateLimitLimitResponse{
RatePerMinute: cfg.RateLimit.RatePerMinute,
})
}
func SmartGitStatusHandler(cfg *config.Config, c *touka.Context) {
c.SetHeader("Content-Type", "application/json")
c.JSON(200, (map[string]any{
"enabled": cfg.GitClone.Mode == "cache",
}))
c.JSON(200, SmartGitStatusResponse{
Enabled: cfg.GitClone.Mode == "cache",
})
}
func shellNestStatusHandler(cfg *config.Config, c *touka.Context) {
c.SetHeader("Content-Type", "application/json")
c.JSON(200, (map[string]any{
"enabled": cfg.Shell.Editor,
}))
c.JSON(200, ShellNestStatusResponse{
Enabled: cfg.Shell.Editor,
})
}
func ociProxyStatusHandler(cfg *config.Config, c *touka.Context) {
c.SetHeader("Content-Type", "application/json")
c.JSON(200, (map[string]any{
"enabled": cfg.Docker.Enabled,
"target": cfg.Docker.Target,
}))
c.JSON(200, OCIDockerResponse{
Enabled: cfg.Docker.Enabled,
Target: cfg.Docker.Target,
})
}

84
api/types.go Normal file
View file

@ -0,0 +1,84 @@
package api
// 预定义的固定响应内容,避免每次请求时创建 map
// HealthcheckResponse 健康检查响应
type HealthcheckResponse struct {
Status string `json:"Status"`
Repo string `json:"Repo"`
Author string `json:"Author"`
}
// VersionResponse 版本信息响应
type VersionResponse struct {
Version string `json:"Version"`
Repo string `json:"Repo"`
Author string `json:"Author"`
}
// BoolStatusResponse 布尔状态响应
type BoolStatusResponse struct {
Enabled bool `json:"enabled"`
}
// SizeLimitResponse 大小限制响应
type SizeLimitResponse struct {
MaxResponseBodySize int `json:"MaxResponseBodySize"`
}
// WhitelistStatusResponse 白名单状态响应
type WhitelistStatusResponse struct {
Whitelist bool `json:"Whitelist"`
}
// BlacklistStatusResponse 黑名单状态响应
type BlacklistStatusResponse struct {
Blacklist bool `json:"Blacklist"`
}
// CorsStatusResponse CORS 状态响应
type CorsStatusResponse struct {
Cors string `json:"Cors"`
}
// RateLimitStatusResponse 速率限制状态响应
type RateLimitStatusResponse struct {
RateLimit bool `json:"RateLimit"`
}
// RateLimitLimitResponse 速率限制值响应
type RateLimitLimitResponse struct {
RatePerMinute int `json:"RatePerMinute"`
}
// SmartGitStatusResponse SmartGit 状态响应
type SmartGitStatusResponse struct {
Enabled bool `json:"enabled"`
}
// ShellNestStatusResponse Shell Nest 状态响应
type ShellNestStatusResponse struct {
Enabled bool `json:"enabled"`
}
// OCIDockerResponse OCI/Docker 代理状态响应
type OCIDockerResponse struct {
Enabled bool `json:"enabled"`
Target string `json:"target,omitempty"`
}
// 预定义的固定响应实例,避免重复分配
var (
// baseHealthcheckResponse 基础健康检查响应(固定部分)
baseHealthcheckResponse = HealthcheckResponse{
Status: "OK",
Repo: "WJQSERVER-STUDIO/GHProxy",
Author: "WJQSERVER-STUDIO",
}
// baseVersionResponse 基础版本响应(固定部分)
baseVersionResponse = VersionResponse{
Repo: "WJQSERVER-STUDIO/GHProxy",
Author: "WJQSERVER-STUDIO",
}
)