82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"ghproxy/config"
|
|
"ghproxy/logger"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
var (
|
|
router *gin.Engine
|
|
cfg *config.Config
|
|
)
|
|
|
|
// 日志模块
|
|
var (
|
|
logw = logger.Logw
|
|
logInfo = logger.LogInfo
|
|
logWarning = logger.LogWarning
|
|
logError = logger.LogError
|
|
)
|
|
|
|
func InitHandleRouter(cfg *config.Config, router *gin.Engine) {
|
|
// 设置路由
|
|
apiRouter := router.Group("api")
|
|
{
|
|
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)
|
|
})
|
|
}
|
|
logInfo("API router Init success")
|
|
}
|
|
|
|
func SizeLimitHandler(cfg *config.Config, c *gin.Context) {
|
|
sizeLimit := cfg.Server.SizeLimit / 1024 / 1024
|
|
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) {
|
|
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) {
|
|
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) {
|
|
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) {
|
|
c.Writer.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
|
|
"Status": "OK",
|
|
})
|
|
}
|