mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-11 01:36:37 +08:00
feat(core): bind request services and implement registered tasks
Wire plugin services through Bind/InjectFrom and AppContext so HTTP and workers resolve dependencies after Apply. Register TaskHandler objects with persisted results, and implement send_email_code, mail:send, cleanup_inactive_users, and dispatch_bot_msg.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
// Copyright 2026 Arctel.net
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package core
|
||||
|
||||
import "context"
|
||||
|
||||
type appContextKey struct{}
|
||||
|
||||
// WithAppContext attaches the micro-kernel Context to a standard context.Context
|
||||
// so request and worker handlers can Inject services without package-level setters.
|
||||
func WithAppContext(ctx context.Context, app *Context) context.Context {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
if app == nil {
|
||||
return ctx
|
||||
}
|
||||
return context.WithValue(ctx, appContextKey{}, app.Root())
|
||||
}
|
||||
|
||||
// AppContext extracts the micro-kernel Context from ctx, if present.
|
||||
func AppContext(ctx context.Context) *Context {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
if c, ok := ctx.(*Context); ok {
|
||||
return c
|
||||
}
|
||||
app, _ := ctx.Value(appContextKey{}).(*Context)
|
||||
return app
|
||||
}
|
||||
|
||||
// InjectFrom resolves T from ctx when it carries a micro-kernel Context
|
||||
// (*Context itself, or a value attached by WithAppContext).
|
||||
func InjectFrom[T any](ctx context.Context) (T, error) {
|
||||
var zero T
|
||||
app := AppContext(ctx)
|
||||
if app == nil {
|
||||
return zero, ErrNilContext
|
||||
}
|
||||
return Inject[T](app)
|
||||
}
|
||||
Reference in New Issue
Block a user