add hasher

This commit is contained in:
yanghao05
2023-05-30 11:19:09 +08:00
parent 2523e47840
commit 63e66378ee
2 changed files with 17 additions and 4 deletions

View File

@@ -18,6 +18,7 @@ func DefaultProvider() container.ProviderContainer {
} }
type Config struct { type Config struct {
Alphabet string
Salt string Salt string
MinLength uint MinLength uint
} }

View File

@@ -15,9 +15,21 @@ func Provide(opts ...opt.Option) error {
log.Fatal(err) log.Fatal(err)
} }
return container.Container.Provide(func() (*hashids.HashID, error) { return container.Container.Provide(func() (*hashids.HashID, error) {
return hashids.NewWithData(&hashids.HashIDData{ data := hashids.NewData()
MinLength: int(config.MinLength), data.MinLength = int(config.MinLength)
Salt: config.Salt, if data.MinLength == 0 {
}) data.MinLength = 5
}
data.Salt = config.Salt
if data.Salt == "" {
data.Salt = "default-salt-key"
}
if config.Alphabet != "" {
data.Alphabet = config.Alphabet
}
return hashids.NewWithData(data)
}, o.DiOptions()...) }, o.DiOptions()...)
} }