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

View File

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

View File

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

View File

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

View File

@@ -4,12 +4,13 @@ import (
"time" "time"
"github.com/rogeecn/atom/container" "github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers" "github.com/rogeecn/atom/utils/opt"
"github.com/brianvoe/gofakeit/v6" "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) { return container.Container.Provide(func() (*gofakeit.Faker, error) {
faker := gofakeit.New(time.Now().UnixNano()) faker := gofakeit.New(time.Now().UnixNano())
gofakeit.SetGlobalFaker(faker) gofakeit.SetGlobalFaker(faker)

View File

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

View File

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

View File

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

View File

@@ -2,11 +2,12 @@ package single_flight
import ( import (
"github.com/rogeecn/atom/container" "github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers" "github.com/rogeecn/atom/utils/opt"
"golang.org/x/sync/singleflight" "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 container.Container.Provide(func() (*singleflight.Group, error) {
return &singleflight.Group{}, nil return &singleflight.Group{}, nil
}, o.DiOptions()...) }, o.DiOptions()...)

View File

@@ -2,7 +2,7 @@ package uuid
import ( import (
"github.com/rogeecn/atom/container" "github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers" "github.com/rogeecn/atom/utils/opt"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
) )
@@ -11,7 +11,8 @@ type Generator struct {
generator uuid.Generator 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 container.Container.Provide(func() (*Generator, error) {
return &Generator{ return &Generator{
generator: uuid.DefaultGenerator, generator: uuid.DefaultGenerator,

View File

@@ -1,4 +1,4 @@
package providers package opt
import ( import (
"github.com/spf13/viper" "github.com/spf13/viper"
@@ -7,7 +7,7 @@ import (
type Options struct { type Options struct {
Config *viper.Viper Config *viper.Viper
ConfigPrefix string Prefix string
Name string Name string
Group string Group string
} }
@@ -23,7 +23,7 @@ func New(opts ...Option) *Options {
} }
func (o *Options) UnmarshalConfig(config interface{}) error { func (o *Options) UnmarshalConfig(config interface{}) error {
return o.Config.UnmarshalKey(o.ConfigPrefix, &config) return o.Config.UnmarshalKey(o.Prefix, &config)
} }
func (o *Options) DiOptions() []dig.ProvideOption { func (o *Options) DiOptions() []dig.ProvideOption {
@@ -37,26 +37,26 @@ func (o *Options) DiOptions() []dig.ProvideOption {
return options return options
} }
func WithConfig(config *viper.Viper) Option { func Config(config *viper.Viper) Option {
return func(o *Options) { return func(o *Options) {
o.Config = config o.Config = config
} }
} }
func WithName(name string) Option { func Name(name string) Option {
return func(o *Options) { return func(o *Options) {
o.Name = name o.Name = name
} }
} }
func WithGroup(group string) Option { func Group(group string) Option {
return func(o *Options) { return func(o *Options) {
o.Group = group o.Group = group
} }
} }
func WithConfigPrefix(prefix string) Option { func Prefix(prefix string) Option {
return func(o *Options) { return func(o *Options) {
o.ConfigPrefix = prefix o.Prefix = prefix
} }
} }