add backend tpl
This commit is contained in:
75
backend_v1/pkg/container/container.go
Normal file
75
backend_v1/pkg/container/container.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.ipao.vip/rogeecn/mp-qvyun/pkg/opt"
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
var (
|
||||
Container *dig.Container = dig.New()
|
||||
Cancel context.CancelFunc
|
||||
closeable []func()
|
||||
)
|
||||
|
||||
func init() {
|
||||
closeable = make([]func(), 0)
|
||||
|
||||
if err := Container.Provide(func() context.Context {
|
||||
signals := []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL}
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), signals...)
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
go time.AfterFunc(time.Second*5, func() {
|
||||
os.Exit(1)
|
||||
})
|
||||
Close()
|
||||
Cancel()
|
||||
}()
|
||||
Cancel = cancel
|
||||
return ctx
|
||||
}); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func AddCloseAble(c func()) {
|
||||
closeable = append(closeable, c)
|
||||
}
|
||||
|
||||
func Close() {
|
||||
for _, c := range closeable {
|
||||
c()
|
||||
}
|
||||
}
|
||||
|
||||
type ProviderContainer struct {
|
||||
Provider func(...opt.Option) error
|
||||
Options []opt.Option
|
||||
}
|
||||
|
||||
type Providers []ProviderContainer
|
||||
|
||||
func (p Providers) With(pcs ...Providers) Providers {
|
||||
for _, pc := range pcs {
|
||||
p = append(p, pc...)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (p Providers) Provide(config *viper.Viper) error {
|
||||
for _, provider := range p {
|
||||
provider.Options = append(provider.Options, opt.Config(config))
|
||||
if err := provider.Provider(provider.Options...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user