change options

This commit is contained in:
yanghao05
2023-04-28 14:17:44 +08:00
parent e6e3df17fa
commit 3a9d5492f8
11 changed files with 41 additions and 31 deletions

View File

@@ -7,7 +7,7 @@ import (
"github.com/mojocn/base64Captcha"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers"
"github.com/rogeecn/atom/utils/opt"
"github.com/spf13/viper"
)
@@ -22,7 +22,8 @@ type Captcha struct {
captcha *base64Captcha.Captcha
}
func Provide(o *providers.Options) error {
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var conf Config
if err := o.UnmarshalConfig(&conf); err != nil {
log.Fatal(err)

View File

@@ -4,15 +4,16 @@ import (
"database/sql"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers"
"github.com/rogeecn/atom/providers/log"
"github.com/rogeecn/atom/utils/opt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func Provide(o *providers.Options) error {
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var conf Config
if err := o.UnmarshalConfig(&conf); err != nil {
return err

View File

@@ -4,14 +4,15 @@ import (
"log"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers"
"github.com/rogeecn/atom/utils/opt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func Provide(o *providers.Options) error {
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var conf Config
if err := o.UnmarshalConfig(&conf); err != nil {
return err

View File

@@ -2,14 +2,15 @@ package sqlite
import (
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers"
"github.com/rogeecn/atom/utils/opt"
// "gorm.io/driver/sqlite"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
func Provide(o *providers.Options) error {
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var conf Config
if err := o.UnmarshalConfig(&conf); err != nil {
return err

View File

@@ -4,12 +4,13 @@ import (
"time"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers"
"github.com/rogeecn/atom/utils/opt"
"github.com/brianvoe/gofakeit/v6"
)
func Provide(o *providers.Options) error {
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
return container.Container.Provide(func() (*gofakeit.Faker, error) {
faker := gofakeit.New(time.Now().UnixNano())
gofakeit.SetGlobalFaker(faker)

View File

@@ -5,9 +5,9 @@ import (
"time"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers"
"github.com/rogeecn/atom/providers/http"
"github.com/rogeecn/atom/providers/log"
"github.com/rogeecn/atom/utils/opt"
"github.com/gin-gonic/gin"
)
@@ -32,7 +32,8 @@ func (e *Service) Serve() error {
return e.Engine.Run(e.conf.PortString())
}
func Provide(o *providers.Options) error {
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config http.Config
if err := o.UnmarshalConfig(&config); err != nil {
log.Fatal(err)

View File

@@ -6,8 +6,8 @@ import (
"time"
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers"
"github.com/rogeecn/atom/providers/log"
"github.com/rogeecn/atom/utils/opt"
jwt "github.com/golang-jwt/jwt/v4"
"golang.org/x/sync/singleflight"
@@ -44,7 +44,8 @@ var (
TokenInvalid = errors.New("Couldn't handle this token:")
)
func Provide(o *providers.Options) error {
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
log.Fatal(err)

View File

@@ -2,12 +2,13 @@ package log
import (
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers"
"github.com/rogeecn/atom/utils/opt"
"go.uber.org/zap"
)
func Provide(o *providers.Options) error {
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
return err

View File

@@ -1,62 +0,0 @@
package providers
import (
"github.com/spf13/viper"
"go.uber.org/dig"
)
type Options struct {
Config *viper.Viper
ConfigPrefix string
Name string
Group string
}
type Option func(o *Options)
func New(opts ...Option) *Options {
o := &Options{}
for _, opt := range opts {
opt(o)
}
return o
}
func (o *Options) UnmarshalConfig(config interface{}) error {
return o.Config.UnmarshalKey(o.ConfigPrefix, &config)
}
func (o *Options) DiOptions() []dig.ProvideOption {
options := []dig.ProvideOption{}
if o.Name != "" {
options = append(options, dig.Name(o.Name))
}
if o.Group != "" {
options = append(options, dig.Group(o.Group))
}
return options
}
func WithConfig(config *viper.Viper) Option {
return func(o *Options) {
o.Config = config
}
}
func WithName(name string) Option {
return func(o *Options) {
o.Name = name
}
}
func WithGroup(group string) Option {
return func(o *Options) {
o.Group = group
}
}
func WithConfigPrefix(prefix string) Option {
return func(o *Options) {
o.ConfigPrefix = prefix
}
}

View File

@@ -2,11 +2,12 @@ package single_flight
import (
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers"
"github.com/rogeecn/atom/utils/opt"
"golang.org/x/sync/singleflight"
)
func Provide(o *providers.Options) error {
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
return container.Container.Provide(func() (*singleflight.Group, error) {
return &singleflight.Group{}, nil
}, o.DiOptions()...)

View File

@@ -2,7 +2,7 @@ package uuid
import (
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers"
"github.com/rogeecn/atom/utils/opt"
"github.com/gofrs/uuid"
)
@@ -11,7 +11,8 @@ type Generator struct {
generator uuid.Generator
}
func Provide(o *providers.Options) error {
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
return container.Container.Provide(func() (*Generator, error) {
return &Generator{
generator: uuid.DefaultGenerator,