Merge pull request #187 from WJQSERVER-STUDIO/feat/update-deps

chore(deps): update Go dependencies and refactor API responses
This commit is contained in:
WJQSERVER 2026-04-11 23:18:05 +08:00 committed by GitHub
commit 4c555ed50c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 138 additions and 53 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) { 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
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",
}
)

11
go.mod
View file

@ -5,12 +5,11 @@ go 1.26
require ( require (
github.com/BurntSushi/toml v1.6.0 github.com/BurntSushi/toml v1.6.0
github.com/WJQSERVER-STUDIO/httpc v0.9.0 github.com/WJQSERVER-STUDIO/httpc v0.9.0
golang.org/x/net v0.52.0 golang.org/x/net v0.53.0
golang.org/x/time v0.15.0 golang.org/x/time v0.15.0
) )
require ( require (
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2 github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2
github.com/WJQSERVER/wanf v0.0.8 github.com/WJQSERVER/wanf v0.0.8
github.com/fenthope/bauth v0.0.1 github.com/fenthope/bauth v0.0.1
@ -20,8 +19,12 @@ require (
github.com/fenthope/record v0.0.4 github.com/fenthope/record v0.0.4
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433 github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433
github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/infinite-iroha/touka v0.5.1 github.com/infinite-iroha/touka v0.5.1-0.20260409232140-271e54eb4d44
github.com/wjqserver/modembed v0.0.1 github.com/wjqserver/modembed v0.0.1
) )
require github.com/valyala/bytebufferpool v1.0.0 // indirect require (
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.3 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/text v0.36.0 // indirect
)

14
go.sum
View file

@ -1,7 +1,7 @@
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2 h1:AiIHXP21LpK7pFfqUlUstgQEWzjbekZgxOuvVwiMfyM= github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.3 h1:Hc1O6D50U3URkdSzfQ/SgeUU750wUBCYhefdvAbE2Ck=
github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.2/go.mod h1:mCLqYU32bTmEE6dpj37MKKiZgz70Jh/xyK9vVbq6pok= github.com/WJQSERVER-STUDIO/go-utils/iox v0.0.3/go.mod h1:nFQzepAwwdj5Hp5U+X19l4FVvsaOSBTW41BzfI/CkMA=
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2 h1:8bBkKk6E2Zr+I5szL7gyc5f0DK8N9agIJCpM1Cqw2NE= github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2 h1:8bBkKk6E2Zr+I5szL7gyc5f0DK8N9agIJCpM1Cqw2NE=
github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2/go.mod h1:yPX8xuZH+py7eLJwOYj3VVI/4/Yuy5+x8Mhq8qezcPg= github.com/WJQSERVER-STUDIO/go-utils/limitreader v0.0.2/go.mod h1:yPX8xuZH+py7eLJwOYj3VVI/4/Yuy5+x8Mhq8qezcPg=
github.com/WJQSERVER-STUDIO/httpc v0.9.0 h1:MpXcQQqukrSLHH/2tTfnXrhqD6nEDHB/gbzehXaS8o4= 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
github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433/go.mod h1:tphK2c80bpPhMOI4v6bIc2xWywPfbqi1Z06+RcrMkDg= github.com/go-json-experiment/json v0.0.0-20260214004413-d219187c3433/go.mod h1:tphK2c80bpPhMOI4v6bIc2xWywPfbqi1Z06+RcrMkDg=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/infinite-iroha/touka v0.5.1 h1:t4D1FmTL4noo9PsSG8i/pswIROpZHkAhQrQYS1ZGoSA= github.com/infinite-iroha/touka v0.5.1-0.20260409232140-271e54eb4d44 h1:VcFNhePZe8qhc9M3Qd0HzV6LjU3QCXxWjQokgFlp3TU=
github.com/infinite-iroha/touka v0.5.1/go.mod h1:RJmpIpGslbQIZ0M5xJHIqTTd1RSBH6TwRQmREcF76pA= github.com/infinite-iroha/touka v0.5.1-0.20260409232140-271e54eb4d44/go.mod h1:6s1oUso8IQp9MbJ+hDvxx8AodbOF7YMel2DnW/x0qrg=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/wjqserver/modembed v0.0.1 h1:8ZDz7t9M5DLrUFlYgBUUmrMzxWsZPmHvOazkr/T2jEs= github.com/wjqserver/modembed v0.0.1 h1:8ZDz7t9M5DLrUFlYgBUUmrMzxWsZPmHvOazkr/T2jEs=
github.com/wjqserver/modembed v0.0.1/go.mod h1:sYbQJMAjSBsdYQrUsuHY380XXE1CuRh8g9yyCztTXOQ= github.com/wjqserver/modembed v0.0.1/go.mod h1:sYbQJMAjSBsdYQrUsuHY380XXE1CuRh8g9yyCztTXOQ=
golang.org/x/net v0.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0= golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw= golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U= golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno= golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=

View file

@ -517,7 +517,7 @@ func main() {
defer logger.Close() defer logger.Close()
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port) addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
err := r.RunShutdown(addr) err := r.Run(touka.WithAddr(addr), touka.WithGracefulShutdownDefault())
if err != nil { if err != nil {
logger.Errorf("Server Run Error: %v", err) logger.Errorf("Server Run Error: %v", err)
fmt.Printf("Server Run Error: %v\n", err) fmt.Printf("Server Run Error: %v\n", err)

View file

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
@ -12,7 +13,6 @@ import (
"ghproxy/config" "ghproxy/config"
"ghproxy/weakcache" "ghproxy/weakcache"
"github.com/WJQSERVER-STUDIO/go-utils/iox"
"github.com/WJQSERVER-STUDIO/go-utils/limitreader" "github.com/WJQSERVER-STUDIO/go-utils/limitreader"
"github.com/go-json-experiment/json" "github.com/go-json-experiment/json"
"github.com/infinite-iroha/touka" "github.com/infinite-iroha/touka"
@ -363,7 +363,7 @@ func GhcrRequest(ctx context.Context, c *touka.Context, u string, image *imageIn
// 如果最终响应是 404, 则读取响应体并返回自定义错误页面 // 如果最终响应是 404, 则读取响应体并返回自定义错误页面
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
defer resp.Body.Close() // 使用defer确保在函数返回前关闭响应体 defer resp.Body.Close() // 使用defer确保在函数返回前关闭响应体
bodyBytes, err := iox.ReadAll(resp.Body) bodyBytes, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
c.Warnf("Failed to read upstream 404 response body: %v", err) c.Warnf("Failed to read upstream 404 response body: %v", err)
} else { } else {
@ -481,7 +481,7 @@ func ChallengeReq(target string, image *imageInfo, ctx context.Context, c *touka
defer authResp.Body.Close() // 确保响应体关闭 defer authResp.Body.Close() // 确保响应体关闭
// 读取认证响应体 // 读取认证响应体
bodyBytes, err := iox.ReadAll(authResp.Body) bodyBytes, err := io.ReadAll(authResp.Body)
if err != nil { if err != nil {
c.Errorf("Failed to read auth response body: %v", err) c.Errorf("Failed to read auth response body: %v", err)
return return