代码拉取完成,页面将自动刷新
package cMiddleware
import (
"net/http"
"reflect"
"sync"
"github.com/gin-gonic/gin"
)
type MiddlewareContainer struct {
lock sync.RWMutex
instances map[string]any
errorHandler func(ctx *gin.Context, err error)
}
func (i *MiddlewareContainer) Save(instance any) bool {
i.lock.Lock()
defer i.lock.Unlock()
app, name := instance.(MiddlewareInterface).MiddlewareName()
index := app + "." + name
i.instances[index] = instance
return true
}
func (i *MiddlewareContainer) Get(name string) any {
i.lock.RLock()
defer i.lock.RUnlock()
return i.instances[name]
}
func (i *MiddlewareContainer) Remove(name string) bool {
i.lock.Lock()
defer i.lock.Unlock()
delete(i.instances, name)
return true
}
func (i *MiddlewareContainer) Is(instance any) bool {
return reflect.TypeOf(instance).Implements(reflect.TypeOf((*MiddlewareInterface)(nil)).Elem())
}
func (i *MiddlewareContainer) Range(f func(instance any)) {
i.lock.RLock()
defer i.lock.RUnlock()
for _, item := range i.instances {
f(item)
}
}
func (i *MiddlewareContainer) SetErrorHandler(f func(ctx *gin.Context, err error)) {
i.errorHandler = f
}
var container = &MiddlewareContainer{
lock: sync.RWMutex{},
instances: make(map[string]any),
errorHandler: func(ctx *gin.Context, err error) {
ctx.String(http.StatusInternalServerError, "%s", err)
},
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。