2.1.0 --- - RELEASE: v2.1.0正式版发布; - CHANGE: 加入`FreeBSD`与`Darwin`系统支持 - CHANGE: 更新安全政策, v1和24w版本序列生命周期正式结束 - ADD: 加入`timing`中间件记录响应时间 - ADD: 加入`loggin`中间件包装日志输出 - CHANGE: 更新logger版本至v1.3.0 - CHANGE: 改进日志相关 - ADD: 加入日志等级配置项
130 lines
4.2 KiB
Go
130 lines
4.2 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"ghproxy/config"
|
|
|
|
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
var (
|
|
router *gin.Engine
|
|
cfg *config.Config
|
|
)
|
|
|
|
var (
|
|
logw = logger.Logw
|
|
LogDump = logger.LogDump
|
|
logDebug = logger.LogDebug
|
|
logInfo = logger.LogInfo
|
|
logWarning = logger.LogWarning
|
|
logError = logger.LogError
|
|
)
|
|
|
|
func NoCacheMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
// 设置禁止缓存的响应头
|
|
c.Header("Cache-Control", "no-store, no-cache, must-revalidate")
|
|
c.Header("Pragma", "no-cache")
|
|
c.Header("Expires", "0")
|
|
c.Next() // 继续处理请求
|
|
}
|
|
}
|
|
|
|
func InitHandleRouter(cfg *config.Config, router *gin.Engine, version string) {
|
|
apiRouter := router.Group("api", NoCacheMiddleware())
|
|
{
|
|
apiRouter.GET("/size_limit", func(c *gin.Context) {
|
|
SizeLimitHandler(cfg, c)
|
|
})
|
|
apiRouter.GET("/whitelist/status", func(c *gin.Context) {
|
|
WhiteListStatusHandler(c, cfg)
|
|
})
|
|
apiRouter.GET("/blacklist/status", func(c *gin.Context) {
|
|
BlackListStatusHandler(c, cfg)
|
|
})
|
|
apiRouter.GET("/cors/status", func(c *gin.Context) {
|
|
CorsStatusHandler(c, cfg)
|
|
})
|
|
apiRouter.GET("/healthcheck", func(c *gin.Context) {
|
|
HealthcheckHandler(c)
|
|
})
|
|
apiRouter.GET("/version", func(c *gin.Context) {
|
|
VersionHandler(c, version)
|
|
})
|
|
apiRouter.GET("/rate_limit/status", func(c *gin.Context) {
|
|
RateLimitStatusHandler(c, cfg)
|
|
})
|
|
apiRouter.GET("/rate_limit/limit", func(c *gin.Context) {
|
|
RateLimitLimitHandler(c, cfg)
|
|
})
|
|
}
|
|
logInfo("API router Init success")
|
|
}
|
|
|
|
func SizeLimitHandler(cfg *config.Config, c *gin.Context) {
|
|
sizeLimit := cfg.Server.SizeLimit
|
|
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto)
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
|
"MaxResponseBodySize": sizeLimit,
|
|
})
|
|
}
|
|
|
|
func WhiteListStatusHandler(c *gin.Context, cfg *config.Config) {
|
|
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto)
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
|
"Whitelist": cfg.Whitelist.Enabled,
|
|
})
|
|
}
|
|
|
|
func BlackListStatusHandler(c *gin.Context, cfg *config.Config) {
|
|
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto)
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
|
"Blacklist": cfg.Blacklist.Enabled,
|
|
})
|
|
}
|
|
|
|
func CorsStatusHandler(c *gin.Context, cfg *config.Config) {
|
|
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto)
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
|
"Cors": cfg.CORS.Enabled,
|
|
})
|
|
}
|
|
|
|
func HealthcheckHandler(c *gin.Context) {
|
|
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto)
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
|
"Status": "OK",
|
|
})
|
|
}
|
|
|
|
func VersionHandler(c *gin.Context, version string) {
|
|
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto)
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
|
"Version": version,
|
|
})
|
|
}
|
|
|
|
func RateLimitStatusHandler(c *gin.Context, cfg *config.Config) {
|
|
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto)
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
|
"RateLimit": cfg.RateLimit.Enabled,
|
|
})
|
|
}
|
|
|
|
func RateLimitLimitHandler(c *gin.Context, cfg *config.Config) {
|
|
logInfo("%s %s %s %s %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.UserAgent(), c.Request.Proto)
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
|
"RatePerMinute": cfg.RateLimit.RatePerMinute,
|
|
})
|
|
}
|