go项目脚手架
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.
sorbet/pkg/app/context.go

56 lines
932 B

package app
import (
"context"
"github.com/labstack/echo/v4"
"sync"
)
type Context struct {
context.Context
prefix string
store map[any]any
router *echo.Group
mu sync.RWMutex
}
// Prefix 设置路由前缀
func (c *Context) Prefix(prefix string) {
c.mu.Lock()
defer c.mu.Unlock()
if c.prefix != "" {
panic("already prefixed")
}
c.prefix = prefix
}
// 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)
}