You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
|
|
|
context.Context
|
|
|
|
store map[any]any
|
|
|
|
router *echo.Group
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewContext(ctx context.Context, router *echo.Group) *Context {
|
|
|
|
return &Context{
|
|
|
|
Context: ctx,
|
|
|
|
store: make(map[any]any),
|
|
|
|
router: router,
|
|
|
|
mu: sync.RWMutex{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Routes 注册路由
|
|
|
|
func (c *Context) Routes(routes Routable) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
routes.InitRoutes(c.router)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set 设置值
|
|
|
|
func (c *Context) Set(key, val any) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
c.store[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 获取值,只会获取通过 Set 方法设置的值
|
|
|
|
func (c *Context) Get(key any) (any, bool) {
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
val, ok := c.store[key]
|
|
|
|
return val, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value 获取值
|
|
|
|
func (c *Context) Value(key any) any {
|
|
|
|
if val, ok := c.Get(key); ok {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return c.Value(key)
|
|
|
|
}
|