From 8af0971f9eb7a5a3cfb28365511936e52520f6e1 Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Sat, 11 Apr 2026 22:39:40 +0800 Subject: [PATCH] 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 --- api/api.go | 74 ++++++++++++++++++++++----------------------- api/types.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 39 deletions(-) create mode 100644 api/types.go diff --git a/api/api.go b/api/api.go index ce5e49f..c2f455c 100644 --- a/api/api.go +++ b/api/api.go @@ -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, + }) } diff --git a/api/types.go b/api/types.go new file mode 100644 index 0000000..fe052f7 --- /dev/null +++ b/api/types.go @@ -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", + } +)