From 8c98130e658717944194f8b21c01e304936e8525 Mon Sep 17 00:00:00 2001 From: loveuer Date: Thu, 2 Jan 2025 22:44:20 +0800 Subject: [PATCH] feat: add banner; add async init fns --- module.go | 16 +++++++++++----- run.go | 34 ++++++++++++++++++++++++---------- upp.go | 23 +++++++++++++++++++++-- 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/module.go b/module.go index 19712f5..ec07452 100644 --- a/module.go +++ b/module.go @@ -85,12 +85,18 @@ func InitTaskChan(ch <-chan func(upp interfaces.Upp) error) module { } } +// sync functions +// 添加 同步执行函数 func InitFn(fns ...func(interfaces.Upp)) module { return func(u *upp) { - if u.initFns == nil { - u.initFns = make([]func(interfaces.Upp), 0) - } - - u.initFns = append(u.initFns, fns...) + u.initFns._sync = append(u.initFns._sync, fns...) + } +} + +// async functions +// 添加 异步执行函数 +func InitAsyncFn(fns ...func(interfaces.Upp)) module { + return func(u *upp) { + u.initFns._async = append(u.initFns._async, fns...) } } diff --git a/run.go b/run.go index 0752620..764f654 100644 --- a/run.go +++ b/run.go @@ -2,6 +2,7 @@ package upp import ( "context" + "fmt" "os/signal" "syscall" @@ -9,13 +10,13 @@ import ( "github.com/loveuer/upp/pkg/tool" ) -func (u *upp) StartAPI(ctx context.Context) { +func (u *upp) startAPI(ctx context.Context) { address := env.ListenHttp if 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 func() { <-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 { go func(ch <-chan func(interfaces.Upp) error) { var err error @@ -48,12 +50,18 @@ func (u *upp) Run(ctx context.Context) { u.RunSignal(ctx) } -func (u *upp) RunInitFns(ctx context.Context) { - for _, fn := range u.initFns { +func (u *upp) runInitFns(ctx context.Context) { + for _, fn := range u.initFns._sync { 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) { c := context.Background() if len(ctxs) > 0 { @@ -65,21 +73,27 @@ func (u *upp) RunSignal(ctxs ...context.Context) { u.ctx = ctx - if len(u.initFns) > 0 { - u.RunInitFns(ctx) + print(Banner) + + if len(u.initFns._sync) > 0 { + u.runInitFns(ctx) + } + + if len(u.initFns._async) > 0 { + u.startInitFns(ctx) } if u.api != nil { - u.StartAPI(ctx) + u.startAPI(ctx) } if len(u.taskCh) > 0 { - u.StartTask(ctx) + u.startTask(ctx) } <-ctx.Done() - u.UseLogger().Warn(" UPP | quit by signal...") + u.UseLogger().Warn(" Upp | quit by signal...") if u.cache != nil { u.cache.Close() } diff --git a/upp.go b/upp.go index 97b661c..5c31e58 100644 --- a/upp.go +++ b/upp.go @@ -12,6 +12,15 @@ import ( "gorm.io/gorm" ) +const Banner = ` + __ __ + / / / /__ ___ +/ /_/ / _ \/ _ \ +\____/ .__/ .__/ + /_/ /_/ + +` + type uppApi struct { engine *api.App config ApiConfig @@ -25,8 +34,11 @@ type upp struct { cache cache.Cache es *elasticsearch.Client api *uppApi - initFns []func(interfaces.Upp) - taskCh []<-chan func(interfaces.Upp) error + initFns struct { + _sync []func(interfaces.Upp) + _async []func(interfaces.Upp) + } + taskCh []<-chan func(interfaces.Upp) error } func (u *upp) With(modules ...module) { @@ -44,6 +56,13 @@ func New(configs ...Config) *upp { app := &upp{ 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 {