feat: add banner; add async init fns

This commit is contained in:
loveuer 2025-01-02 22:44:20 +08:00
parent 0ada03ee80
commit 8c98130e65
3 changed files with 56 additions and 17 deletions

View File

@ -85,12 +85,18 @@ func InitTaskChan(ch <-chan func(upp interfaces.Upp) error) module {
} }
} }
// sync functions
// 添加 同步执行函数
func InitFn(fns ...func(interfaces.Upp)) module { func InitFn(fns ...func(interfaces.Upp)) module {
return func(u *upp) { return func(u *upp) {
if u.initFns == nil { u.initFns._sync = append(u.initFns._sync, fns...)
u.initFns = make([]func(interfaces.Upp), 0) }
} }
u.initFns = append(u.initFns, fns...) // async functions
// 添加 异步执行函数
func InitAsyncFn(fns ...func(interfaces.Upp)) module {
return func(u *upp) {
u.initFns._async = append(u.initFns._async, fns...)
} }
} }

34
run.go
View File

@ -2,6 +2,7 @@ package upp
import ( import (
"context" "context"
"fmt"
"os/signal" "os/signal"
"syscall" "syscall"
@ -9,13 +10,13 @@ import (
"github.com/loveuer/upp/pkg/tool" "github.com/loveuer/upp/pkg/tool"
) )
func (u *upp) StartAPI(ctx context.Context) { func (u *upp) startAPI(ctx context.Context) {
address := env.ListenHttp address := env.ListenHttp
if address == "" { if address == "" {
address = u.api.config.Address address = u.api.config.Address
} }
u.UseLogger().Info("UPP | run api at %s", address) fmt.Printf("Upp | api listen at %s\n", address)
go u.api.engine.Run(address) go u.api.engine.Run(address)
go func() { go func() {
<-ctx.Done() <-ctx.Done()
@ -23,7 +24,8 @@ func (u *upp) StartAPI(ctx context.Context) {
}() }()
} }
func (u *upp) StartTask(ctx context.Context) { func (u *upp) startTask(ctx context.Context) {
fmt.Printf("Upp | start task channel[%02d]", len(u.taskCh))
for _, _ch := range u.taskCh { for _, _ch := range u.taskCh {
go func(ch <-chan func(interfaces.Upp) error) { go func(ch <-chan func(interfaces.Upp) error) {
var err error var err error
@ -48,12 +50,18 @@ func (u *upp) Run(ctx context.Context) {
u.RunSignal(ctx) u.RunSignal(ctx)
} }
func (u *upp) RunInitFns(ctx context.Context) { func (u *upp) runInitFns(ctx context.Context) {
for _, fn := range u.initFns { for _, fn := range u.initFns._sync {
fn(u) fn(u)
} }
} }
func (u *upp) startInitFns(ctx context.Context) {
for _, fn := range u.initFns._async {
go fn(u)
}
}
func (u *upp) RunSignal(ctxs ...context.Context) { func (u *upp) RunSignal(ctxs ...context.Context) {
c := context.Background() c := context.Background()
if len(ctxs) > 0 { if len(ctxs) > 0 {
@ -65,21 +73,27 @@ func (u *upp) RunSignal(ctxs ...context.Context) {
u.ctx = ctx u.ctx = ctx
if len(u.initFns) > 0 { print(Banner)
u.RunInitFns(ctx)
if len(u.initFns._sync) > 0 {
u.runInitFns(ctx)
}
if len(u.initFns._async) > 0 {
u.startInitFns(ctx)
} }
if u.api != nil { if u.api != nil {
u.StartAPI(ctx) u.startAPI(ctx)
} }
if len(u.taskCh) > 0 { if len(u.taskCh) > 0 {
u.StartTask(ctx) u.startTask(ctx)
} }
<-ctx.Done() <-ctx.Done()
u.UseLogger().Warn(" UPP | quit by signal...") u.UseLogger().Warn(" Upp | quit by signal...")
if u.cache != nil { if u.cache != nil {
u.cache.Close() u.cache.Close()
} }

23
upp.go
View File

@ -12,6 +12,15 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
const Banner = `
__ __
/ / / /__ ___
/ /_/ / _ \/ _ \
\____/ .__/ .__/
/_/ /_/
`
type uppApi struct { type uppApi struct {
engine *api.App engine *api.App
config ApiConfig config ApiConfig
@ -25,8 +34,11 @@ type upp struct {
cache cache.Cache cache cache.Cache
es *elasticsearch.Client es *elasticsearch.Client
api *uppApi api *uppApi
initFns []func(interfaces.Upp) initFns struct {
taskCh []<-chan func(interfaces.Upp) error _sync []func(interfaces.Upp)
_async []func(interfaces.Upp)
}
taskCh []<-chan func(interfaces.Upp) error
} }
func (u *upp) With(modules ...module) { func (u *upp) With(modules ...module) {
@ -44,6 +56,13 @@ func New(configs ...Config) *upp {
app := &upp{ app := &upp{
logger: upp_logger_pool, logger: upp_logger_pool,
initFns: struct {
_sync []func(interfaces.Upp)
_async []func(interfaces.Upp)
}{
_sync: make([]func(interfaces.Upp), 0),
_async: make([]func(interfaces.Upp), 0),
},
} }
if config.Debug || env.Debug { if config.Debug || env.Debug {