mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-06-13 15:47:37 +08:00
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:
parent
3d01d4cd15
commit
8af0971f9e
2 changed files with 119 additions and 39 deletions
74
api/api.go
74
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) {
|
func SizeLimitHandler(cfg *config.Config, c *touka.Context) {
|
||||||
sizeLimit := cfg.Server.SizeLimit
|
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
c.JSON(200, (map[string]any{
|
c.JSON(200, SizeLimitResponse{
|
||||||
"MaxResponseBodySize": sizeLimit,
|
MaxResponseBodySize: cfg.Server.SizeLimit,
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func WhiteListStatusHandler(cfg *config.Config, c *touka.Context) {
|
func WhiteListStatusHandler(cfg *config.Config, c *touka.Context) {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
c.JSON(200, (map[string]any{
|
c.JSON(200, WhitelistStatusResponse{
|
||||||
"Whitelist": cfg.Whitelist.Enabled,
|
Whitelist: cfg.Whitelist.Enabled,
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func BlackListStatusHandler(cfg *config.Config, c *touka.Context) {
|
func BlackListStatusHandler(cfg *config.Config, c *touka.Context) {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
c.JSON(200, (map[string]any{
|
c.JSON(200, BlacklistStatusResponse{
|
||||||
"Blacklist": cfg.Blacklist.Enabled,
|
Blacklist: cfg.Blacklist.Enabled,
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func CorsStatusHandler(cfg *config.Config, c *touka.Context) {
|
func CorsStatusHandler(cfg *config.Config, c *touka.Context) {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
c.JSON(200, (map[string]any{
|
c.JSON(200, CorsStatusResponse{
|
||||||
"Cors": cfg.Server.Cors,
|
Cors: cfg.Server.Cors,
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func HealthcheckHandler(c *touka.Context) {
|
func HealthcheckHandler(c *touka.Context) {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
c.JSON(200, (map[string]any{
|
// 复制预定义的固定响应,避免重复分配
|
||||||
"Status": "OK",
|
resp := baseHealthcheckResponse
|
||||||
"Repo": "WJQSERVER-STUDIO/GHProxy",
|
c.JSON(200, resp)
|
||||||
"Author": "WJQSERVER-STUDIO",
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func VersionHandler(c *touka.Context, version string) {
|
func VersionHandler(c *touka.Context, version string) {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
c.JSON(200, (map[string]any{
|
// 复制预定义的固定响应并填充动态字段
|
||||||
"Version": version,
|
resp := baseVersionResponse
|
||||||
"Repo": "WJQSERVER-STUDIO/GHProxy",
|
resp.Version = version
|
||||||
"Author": "WJQSERVER-STUDIO",
|
c.JSON(200, resp)
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func RateLimitStatusHandler(cfg *config.Config, c *touka.Context) {
|
func RateLimitStatusHandler(cfg *config.Config, c *touka.Context) {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
c.JSON(200, (map[string]any{
|
c.JSON(200, RateLimitStatusResponse{
|
||||||
"RateLimit": cfg.RateLimit.Enabled,
|
RateLimit: cfg.RateLimit.Enabled,
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func RateLimitLimitHandler(cfg *config.Config, c *touka.Context) {
|
func RateLimitLimitHandler(cfg *config.Config, c *touka.Context) {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
c.JSON(200, (map[string]any{
|
c.JSON(200, RateLimitLimitResponse{
|
||||||
"RatePerMinute": cfg.RateLimit.RatePerMinute,
|
RatePerMinute: cfg.RateLimit.RatePerMinute,
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func SmartGitStatusHandler(cfg *config.Config, c *touka.Context) {
|
func SmartGitStatusHandler(cfg *config.Config, c *touka.Context) {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
c.JSON(200, (map[string]any{
|
c.JSON(200, SmartGitStatusResponse{
|
||||||
"enabled": cfg.GitClone.Mode == "cache",
|
Enabled: cfg.GitClone.Mode == "cache",
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func shellNestStatusHandler(cfg *config.Config, c *touka.Context) {
|
func shellNestStatusHandler(cfg *config.Config, c *touka.Context) {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
c.JSON(200, (map[string]any{
|
c.JSON(200, ShellNestStatusResponse{
|
||||||
"enabled": cfg.Shell.Editor,
|
Enabled: cfg.Shell.Editor,
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func ociProxyStatusHandler(cfg *config.Config, c *touka.Context) {
|
func ociProxyStatusHandler(cfg *config.Config, c *touka.Context) {
|
||||||
c.SetHeader("Content-Type", "application/json")
|
c.SetHeader("Content-Type", "application/json")
|
||||||
c.JSON(200, (map[string]any{
|
c.JSON(200, OCIDockerResponse{
|
||||||
"enabled": cfg.Docker.Enabled,
|
Enabled: cfg.Docker.Enabled,
|
||||||
"target": cfg.Docker.Target,
|
Target: cfg.Docker.Target,
|
||||||
}))
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
84
api/types.go
Normal file
84
api/types.go
Normal 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",
|
||||||
|
}
|
||||||
|
)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue