2 Star 0 Fork 0

hero/momo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Igin.go 6.95 KB
一键复制 编辑 原始数据 按行查看 历史
hero 提交于 2024-11-06 17:32 . upd:igin
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}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/linqwen/momo.git
git@gitee.com:linqwen/momo.git
linqwen
momo
momo
v1.1.18

搜索帮助

0d507c66 1850385 C8b1a773 1850385