parent
adb24eaf5d
commit
5d00d1987b
@ -0,0 +1,55 @@ |
||||
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) |
||||
} |
@ -0,0 +1,15 @@ |
||||
package app |
||||
|
||||
import "github.com/labstack/echo/v4" |
||||
|
||||
type echoContext struct { |
||||
echo.Context |
||||
ctx *Context |
||||
} |
||||
|
||||
func (e *echoContext) Get(key string) any { |
||||
if val, ok := e.ctx.Get(key); ok && val != nil { |
||||
return val |
||||
} |
||||
return e.Context.Get(key) |
||||
} |
@ -0,0 +1,16 @@ |
||||
package app |
||||
|
||||
import "github.com/labstack/echo/v4" |
||||
|
||||
type Service interface { |
||||
// Init 初始化服务
|
||||
Init(ctx *Context) error |
||||
// Start 启动服务
|
||||
Start() error |
||||
// Stop 停止服务
|
||||
Stop() error |
||||
} |
||||
|
||||
type Routable interface { |
||||
InitRoutes(r *echo.Group) |
||||
} |
Loading…
Reference in new issue