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 {
Alphabet string
Salt string
MinLength uint
}

View File

@@ -15,9 +15,21 @@ func Provide(opts ...opt.Option) error {
log.Fatal(err)
}
return container.Container.Provide(func() (*hashids.HashID, error) {
return hashids.NewWithData(&hashids.HashIDData{
MinLength: int(config.MinLength),
Salt: config.Salt,
})
data := hashids.NewData()
data.MinLength = int(config.MinLength)
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()...)
}