76 lines
1.3 KiB
Go
76 lines
1.3 KiB
Go
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
|
|
}
|