代码拉取完成,页面将自动刷新
package igin
import (
"io"
"mime/multipart"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
var (
Mode = gin.Mode
)
// LogFormatterParams 封装 gin.LogFormatterParams
type LogFormatterParams struct {
gin.LogFormatterParams
}
func SetLogFile(filePath string) error {
// 创建或打开日志文件
file, err := os.Create(filePath)
if err != nil {
return err // 返回错误,便于调用者处理
}
// 设置 Gin 的默认写入器为 MultiWriter,它会将日志同时写入文件和标准输出
gin.DefaultWriter = io.MultiWriter(file, os.Stdout)
return nil // 成功时不返回错误
}
// LoggerWithFormatter 封装 gin.LoggerWithFormatter
func LoggerWithFormatter(fn func(params LogFormatterParams) string) gin.HandlerFunc {
return gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
// 创建一个 LogFormatterParams 封装 gin.LogFormatterParams
params := LogFormatterParams{param}
return fn(params)
})
}
func SetMode(value string) {
gin.SetMode(value)
}
func DisableConsoleColor() {
gin.DisableConsoleColor()
}
type IContext interface {
JSON(code int, obj any)
Param(key string) string
BindJSON(v interface{}) error
ShouldBind(v interface{}) error
ShouldBindJSON(v interface{}) error
ShouldBindQuery(v interface{}) error
Get(key string) (value interface{}, exists bool)
Set(key string, value interface{})
Next()
Request() *http.Request
Writer() gin.ResponseWriter
FullPath() string
GetHeader(key string) string
Query(key string) string
Abort()
Header(key, value string)
Data(code int, contentType string, data []byte)
String(code int, format string, values ...any)
GetInt64(key string) int64
FormFile(formName string) (*multipart.FileHeader, error)
SaveUploadedFile(file *multipart.FileHeader, dst string) error
}
type IRouter interface {
GET(relativePath string, handlers ...HandlerFunc)
POST(relativePath string, handlers ...HandlerFunc)
PUT(relativePath string, handlers ...HandlerFunc)
DELETE(relativePath string, handlers ...HandlerFunc)
Group(relativePath string, handlers ...HandlerFunc) IRouterGroup
Static(relativePath, rootPath string)
}
type IRouterGroup interface {
GET(relativePath string, handlers ...HandlerFunc)
POST(relativePath string, handlers ...HandlerFunc)
PUT(relativePath string, handlers ...HandlerFunc)
DELETE(relativePath string, handlers ...HandlerFunc)
Group(relativePath string, handlers ...HandlerFunc) IRouterGroup
}
type HandlerFunc func(c IContext)
type Engine struct {
*gin.Engine
}
func NewGinEngine() Engine {
return Engine{gin.New()}
}
func (ge *Engine) Run(addr string) error {
return ge.Engine.Run(addr)
}
type ginContextAdapter struct {
*gin.Context
}
// func NewGinContextAdapter(ctx *gin.Context) IContext {
// return &ginContextAdapter{Context: ctx}
// }
func (gca *ginContextAdapter) GetInt64(key string) int64 {
return gca.Context.GetInt64(key)
}
func (gca *ginContextAdapter) JSON(code int, obj any) {
gca.Context.JSON(code, obj)
}
func (gca *ginContextAdapter) String(code int, format string, values ...any) {
gca.Context.String(code, format, values)
}
func (gca *ginContextAdapter) Param(key string) string {
return gca.Context.Param(key)
}
func (gca *ginContextAdapter) BindJSON(v interface{}) error {
return gca.Context.BindJSON(v)
}
func (gca *ginContextAdapter) ShouldBind(v interface{}) error {
return gca.Context.ShouldBind(v)
}
func (gca *ginContextAdapter) ShouldBindJSON(v interface{}) error {
return gca.Context.ShouldBindJSON(v)
}
func (gca *ginContextAdapter) ShouldBindQuery(v interface{}) error {
return gca.Context.ShouldBindQuery(v)
}
func (gca *ginContextAdapter) Get(key string) (value interface{}, exists bool) {
return gca.Context.Get(key)
}
func (gca *ginContextAdapter) Set(key string, value interface{}) {
gca.Context.Set(key, value)
}
func (gca *ginContextAdapter) Next() {
gca.Context.Next()
}
func (gca *ginContextAdapter) Request() *http.Request {
return gca.Context.Request
}
func (gca *ginContextAdapter) Writer() gin.ResponseWriter {
return gca.Context.Writer
}
func (gca *ginContextAdapter) FullPath() string {
return gca.Context.FullPath()
}
func (gca *ginContextAdapter) GetHeader(key string) string {
return gca.Context.GetHeader(key)
}
func (gca *ginContextAdapter) Query(key string) string {
return gca.Context.Query(key)
}
func (gca *ginContextAdapter) Abort() {
gca.Context.Abort()
}
func (gca *ginContextAdapter) Header(key, value string) {
gca.Context.Header(key, value)
}
func (gca *ginContextAdapter) Data(code int, contentType string, data []byte) {
gca.Context.Data(code, contentType, data)
}
func (gca *ginContextAdapter) FormFile(formName string) (*multipart.FileHeader, error) {
return gca.Context.FormFile(formName)
}
func (gca *ginContextAdapter) SaveUploadedFile(file *multipart.FileHeader, dst string) error {
return gca.Context.SaveUploadedFile(file, dst)
}
type GinRouterAdapter struct {
*gin.Engine
}
func (gra *GinRouterAdapter) GET(relativePath string, handlers ...HandlerFunc) {
gra.Engine.GET(relativePath, wrapHandlers(handlers)...)
}
func (gra *GinRouterAdapter) POST(relativePath string, handlers ...HandlerFunc) {
gra.Engine.POST(relativePath, wrapHandlers(handlers)...)
}
func (gra *GinRouterAdapter) PUT(relativePath string, handlers ...HandlerFunc) {
gra.Engine.PUT(relativePath, wrapHandlers(handlers)...)
}
func (gra *GinRouterAdapter) DELETE(relativePath string, handlers ...HandlerFunc) {
gra.Engine.DELETE(relativePath, wrapHandlers(handlers)...)
}
func (gra *GinRouterAdapter) Group(relativePath string, handlers ...HandlerFunc) IRouterGroup {
group := gra.Engine.Group(relativePath, wrapHandlers(handlers)...)
return &GinRouterGroupAdapter{group}
}
func (gra *GinRouterAdapter) Static(relativePath, rootPath string) {
gra.Engine.Static(relativePath, rootPath)
}
func wrapHandlers(handlers []HandlerFunc) []gin.HandlerFunc {
wrappedHandlers := make([]gin.HandlerFunc, len(handlers))
for i, handler := range handlers {
wrappedHandlers[i] = func(c *gin.Context) {
handler(&ginContextAdapter{Context: c})
}
}
return wrappedHandlers
}
func NewGinRouterAdapter(engine *gin.Engine) IRouter {
return &GinRouterAdapter{Engine: engine}
}
type GinRouterGroupAdapter struct {
*gin.RouterGroup
}
func (grga *GinRouterGroupAdapter) GET(relativePath string, handlers ...HandlerFunc) {
grga.RouterGroup.GET(relativePath, wrapHandlers(handlers)...)
}
func (grga *GinRouterGroupAdapter) POST(relativePath string, handlers ...HandlerFunc) {
grga.RouterGroup.POST(relativePath, wrapHandlers(handlers)...)
}
func (grga *GinRouterGroupAdapter) PUT(relativePath string, handlers ...HandlerFunc) {
grga.RouterGroup.PUT(relativePath, wrapHandlers(handlers)...)
}
func (grga *GinRouterGroupAdapter) DELETE(relativePath string, handlers ...HandlerFunc) {
grga.RouterGroup.DELETE(relativePath, wrapHandlers(handlers)...)
}
func (grga *GinRouterGroupAdapter) Group(relativePath string, handlers ...HandlerFunc) IRouterGroup {
group := grga.RouterGroup.Group(relativePath, wrapHandlers(handlers)...)
return &GinRouterGroupAdapter{group}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。