Skip to content

Commit 4c555ed

Browse files
authored
Merge pull request #187 from WJQSERVER-STUDIO/feat/update-deps
chore(deps): update Go dependencies and refactor API responses
2 parents 0ece317 + e7f56da commit 4c555ed

File tree

6 files changed

+138
-53
lines changed

6 files changed

+138
-53
lines changed

api/api.go

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,84 +50,80 @@ func InitHandleRouter(cfg *config.Config, r *touka.Engine, version string) {
5050
}
5151

5252
func SizeLimitHandler(cfg *config.Config, c *touka.Context) {
53-
sizeLimit := cfg.Server.SizeLimit
5453
c.SetHeader("Content-Type", "application/json")
55-
c.JSON(200, (map[string]any{
56-
"MaxResponseBodySize": sizeLimit,
57-
}))
54+
c.JSON(200, SizeLimitResponse{
55+
MaxResponseBodySize: cfg.Server.SizeLimit,
56+
})
5857
}
5958

6059
func WhiteListStatusHandler(cfg *config.Config, c *touka.Context) {
6160
c.SetHeader("Content-Type", "application/json")
62-
c.JSON(200, (map[string]any{
63-
"Whitelist": cfg.Whitelist.Enabled,
64-
}))
61+
c.JSON(200, WhitelistStatusResponse{
62+
Whitelist: cfg.Whitelist.Enabled,
63+
})
6564
}
6665

6766
func BlackListStatusHandler(cfg *config.Config, c *touka.Context) {
6867
c.SetHeader("Content-Type", "application/json")
69-
c.JSON(200, (map[string]any{
70-
"Blacklist": cfg.Blacklist.Enabled,
71-
}))
68+
c.JSON(200, BlacklistStatusResponse{
69+
Blacklist: cfg.Blacklist.Enabled,
70+
})
7271
}
7372

7473
func CorsStatusHandler(cfg *config.Config, c *touka.Context) {
7574
c.SetHeader("Content-Type", "application/json")
76-
c.JSON(200, (map[string]any{
77-
"Cors": cfg.Server.Cors,
78-
}))
75+
c.JSON(200, CorsStatusResponse{
76+
Cors: cfg.Server.Cors,
77+
})
7978
}
8079

8180
func HealthcheckHandler(c *touka.Context) {
8281
c.SetHeader("Content-Type", "application/json")
83-
c.JSON(200, (map[string]any{
84-
"Status": "OK",
85-
"Repo": "WJQSERVER-STUDIO/GHProxy",
86-
"Author": "WJQSERVER-STUDIO",
87-
}))
82+
// 复制预定义的固定响应,避免重复分配
83+
resp := baseHealthcheckResponse
84+
c.JSON(200, resp)
8885
}
8986

9087
func VersionHandler(c *touka.Context, version string) {
9188
c.SetHeader("Content-Type", "application/json")
92-
c.JSON(200, (map[string]any{
93-
"Version": version,
94-
"Repo": "WJQSERVER-STUDIO/GHProxy",
95-
"Author": "WJQSERVER-STUDIO",
96-
}))
89+
// 复制预定义的固定响应并填充动态字段
90+
resp := baseVersionResponse
91+
resp.Version = version
92+
c.JSON(200, resp)
9793
}
9894

9995
func RateLimitStatusHandler(cfg *config.Config, c *touka.Context) {
10096
c.SetHeader("Content-Type", "application/json")
101-
c.JSON(200, (map[string]any{
102-
"RateLimit": cfg.RateLimit.Enabled,
103-
}))
97+
c.JSON(200, RateLimitStatusResponse{
98+
RateLimit: cfg.RateLimit.Enabled,
99+
})
104100
}
105101

106102
func RateLimitLimitHandler(cfg *config.Config, c *touka.Context) {
107103
c.SetHeader("Content-Type", "application/json")
108-
c.JSON(200, (map[string]any{
109-
"RatePerMinute": cfg.RateLimit.RatePerMinute,
110-
}))
104+
c.JSON(200, RateLimitLimitResponse{
105+
RatePerMinute: cfg.RateLimit.RatePerMinute,
106+
})
111107
}
112108

113109
func SmartGitStatusHandler(cfg *config.Config, c *touka.Context) {
114110
c.SetHeader("Content-Type", "application/json")
115-
c.JSON(200, (map[string]any{
116-
"enabled": cfg.GitClone.Mode == "cache",
117-
}))
111+
c.JSON(200, SmartGitStatusResponse{
112+
Enabled: cfg.GitClone.Mode == "cache",
113+
})
118114
}
119115

120116
func shellNestStatusHandler(cfg *config.Config, c *touka.Context) {
121117
c.SetHeader("Content-Type", "application/json")
122-
c.JSON(200, (map[string]any{
123-
"enabled": cfg.Shell.Editor,
124-
}))
118+
c.JSON(200, ShellNestStatusResponse{
119+
Enabled: cfg.Shell.Editor,
120+
})
125121
}
126122

127123
func ociProxyStatusHandler(cfg *config.Config, c *touka.Context) {
128124
c.SetHeader("Content-Type", "application/json")
129-
c.JSON(200, (map[string]any{
130-
"enabled": cfg.Docker.Enabled,
131-
"target": cfg.Docker.Target,
132-
}))
125+
c.JSON(200, OCIDockerResponse{
126+
Enabled: cfg.Docker.Enabled,
127+
Target: cfg.Docker.Target,
128+
})
133129
}

api/types.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package api
2+
3+
// 预定义的固定响应内容,避免每次请求时创建 map
4+
5+
// HealthcheckResponse 健康检查响应
6+
type HealthcheckResponse struct {
7+
Status string `json:"Status"`
8+
Repo string `json:"Repo"`
9+
Author string `json:"Author"`
10+
}
11+
12+
// VersionResponse 版本信息响应
13+
type VersionResponse struct {
14+
Version string `json:"Version"`
15+
Repo string `json:"Repo"`
16+
Author string `json:"Author"`
17+
}
18+
19+
// BoolStatusResponse 布尔状态响应
20+
type BoolStatusResponse struct {
21+
Enabled bool `json:"enabled"`
22+
}
23+
24+
// SizeLimitResponse 大小限制响应
25+
type SizeLimitResponse struct {
26+
MaxResponseBodySize int `json:"MaxResponseBodySize"`
27+
}
28+
29+
// WhitelistStatusResponse 白名单状态响应
30+
type WhitelistStatusResponse struct {
31+
Whitelist bool `json:"Whitelist"`
32+
}
33+
34+
// BlacklistStatusResponse 黑名单状态响应
35+
type BlacklistStatusResponse struct {
36+
Blacklist bool `json:"Blacklist"`
37+
}
38+
39+
// CorsStatusResponse CORS 状态响应
40+
type CorsStatusResponse struct {
41+
Cors string `json:"Cors"`
42+
}
43+
44+
// RateLimitStatusResponse 速率限制状态响应
45+
type RateLimitStatusResponse struct {
46+
RateLimit bool `json:"RateLimit"`
47+
}
48+
49+
// RateLimitLimitResponse 速率限制值响应
50+
type RateLimitLimitResponse struct {
51+
RatePerMinute int `json:"RatePerMinute"`
52+
}
53+
54+
// SmartGitStatusResponse SmartGit 状态响应
55+
type SmartGitStatusResponse struct {
56+
Enabled bool `json:"enabled"`
57+
}
58+
59+
// ShellNestStatusResponse Shell Nest 状态响应
60+
type ShellNestStatusResponse struct {
61+
Enabled bool `json:"enabled"`
62+
}
63+
64+
// OCIDockerResponse OCI/Docker 代理状态响应
65+
type OCIDockerResponse struct {
66+
Enabled bool `json:"enabled"`
67+
Target string `json:"target,omitempty"`
68+
}
69+
70+
// 预定义的固定响应实例,避免重复分配
71+
var (
72+
// baseHealthcheckResponse 基础健康检查响应(固定部分)
73+
baseHealthcheckResponse = HealthcheckResponse{
74+
Status: "OK",
75+
Repo: "WJQSERVER-STUDIO/GHProxy",
76+
Author: "WJQSERVER-STUDIO",
77+
}
78+
79+
// baseVersionResponse 基础版本响应(固定部分)
80+
baseVersionResponse = VersionResponse{
81+
Repo: "WJQSERVER-STUDIO/GHProxy",
82+
Author: "WJQSERVER-STUDIO",
83+
}
84+
)

go.mod

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ go 1.26
55
require (
66
github.com/BurntSushi/toml v1.6.0
77
github.com/WJQSERVER-STUDIO/httpc v0.9.0
8-
golang.org/x/net v0.52.0
8+
golang.org/x/net v0.53.0
99
golang.org/x/time v0.15.0
1010
)
1111

1212
require (
13-
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2
1413
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2
1514
github.com/WJQSERVER/wanf v0.0.8
1615
github.com/fenthope/bauth v0.0.1
@@ -20,8 +19,12 @@ require (
2019
github.com/fenthope/record v0.0.4
2120
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433
2221
github.com/hashicorp/golang-lru/v2 v2.0.7
23-
github.com/infinite-iroha/touka v0.5.1
22+
github.com/infinite-iroha/touka v0.5.1-0.20260409232140-271e54eb4d44
2423
github.com/wjqserver/modembed v0.0.1
2524
)
2625

27-
require github.com/valyala/bytebufferpool v1.0.0 // indirect
26+
require (
27+
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.3 // indirect
28+
github.com/valyala/bytebufferpool v1.0.0 // indirect
29+
golang.org/x/text v0.36.0 // indirect
30+
)

go.sum

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
22
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
3-
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2 h1:AiIHXP21LpK7pFfqUlUstgQEWzjbekZgxOuvVwiMfyM=
4-
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2/go.mod h1:mCLqYU32bTmEE6dpj37MKKiZgz70Jh/xyK9vVbq6pok=
3+
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.3 h1:Hc1O6D50U3URkdSzfQ/SgeUU750wUBCYhefdvAbE2Ck=
4+
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.3/go.mod h1:nFQzepAwwdj5Hp5U+X19l4FVvsaOSBTW41BzfI/CkMA=
55
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2 h1:8bBkKk6E2Zr+I5szL7gyc5f0DK8N9agIJCpM1Cqw2NE=
66
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2/go.mod h1:yPX8xuZH+py7eLJwOYj3VVI/4/Yuy5+x8Mhq8qezcPg=
77
github.com/WJQSERVER-STUDIO/httpc v0.9.0 h1:MpXcQQqukrSLHH/2tTfnXrhqD6nEDHB/gbzehXaS8o4=
@@ -22,13 +22,15 @@ github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 h1:vymEbVw
2222
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433/go.mod h1:tphK2c80bpPhMOI4v6bIc2xWywPfbqi1Z06+RcrMkDg=
2323
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
2424
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
25-
github.com/infinite-iroha/touka v0.5.1 h1:t4D1FmTL4noo9PsSG8i/pswIROpZHkAhQrQYS1ZGoSA=
26-
github.com/infinite-iroha/touka v0.5.1/go.mod h1:RJmpIpGslbQIZ0M5xJHIqTTd1RSBH6TwRQmREcF76pA=
25+
github.com/infinite-iroha/touka v0.5.1-0.20260409232140-271e54eb4d44 h1:VcFNhePZe8qhc9M3Qd0HzV6LjU3QCXxWjQokgFlp3TU=
26+
github.com/infinite-iroha/touka v0.5.1-0.20260409232140-271e54eb4d44/go.mod h1:6s1oUso8IQp9MbJ+hDvxx8AodbOF7YMel2DnW/x0qrg=
2727
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
2828
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
2929
github.com/wjqserver/modembed v0.0.1 h1:8ZDz7t9M5DLrUFlYgBUUmrMzxWsZPmHvOazkr/T2jEs=
3030
github.com/wjqserver/modembed v0.0.1/go.mod h1:sYbQJMAjSBsdYQrUsuHY380XXE1CuRh8g9yyCztTXOQ=
31-
golang.org/x/net v0.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0=
32-
golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw=
31+
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
32+
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
33+
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
34+
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
3335
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
3436
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ func main() {
517517
defer logger.Close()
518518

519519
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
520-
err := r.RunShutdown(addr)
520+
err := r.Run(touka.WithAddr(addr), touka.WithGracefulShutdownDefault())
521521
if err != nil {
522522
logger.Errorf("Server Run Error: %v", err)
523523
fmt.Printf("Server Run Error: %v\n", err)

proxy/docker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"io"
78
"net/http"
89
"net/url"
910
"strconv"
@@ -12,7 +13,6 @@ import (
1213
"ghproxy/config"
1314
"ghproxy/weakcache"
1415

15-
"github.com/WJQSERVER-STUDIO/go-utils/iox"
1616
"github.com/WJQSERVER-STUDIO/go-utils/limitreader"
1717
"github.com/go-json-experiment/json"
1818
"github.com/infinite-iroha/touka"
@@ -363,7 +363,7 @@ func GhcrRequest(ctx context.Context, c *touka.Context, u string, image *imageIn
363363
// 如果最终响应是 404, 则读取响应体并返回自定义错误页面
364364
if resp.StatusCode == 404 {
365365
defer resp.Body.Close() // 使用defer确保在函数返回前关闭响应体
366-
bodyBytes, err := iox.ReadAll(resp.Body)
366+
bodyBytes, err := io.ReadAll(resp.Body)
367367
if err != nil {
368368
c.Warnf("Failed to read upstream 404 response body: %v", err)
369369
} else {
@@ -481,7 +481,7 @@ func ChallengeReq(target string, image *imageInfo, ctx context.Context, c *touka
481481
defer authResp.Body.Close() // 确保响应体关闭
482482

483483
// 读取认证响应体
484-
bodyBytes, err := iox.ReadAll(authResp.Body)
484+
bodyBytes, err := io.ReadAll(authResp.Body)
485485
if err != nil {
486486
c.Errorf("Failed to read auth response body: %v", err)
487487
return

0 commit comments

Comments
 (0)