add hash ids provider

This commit is contained in:
yanghao05
2023-05-30 10:59:16 +08:00
parent 7d6b29c47d
commit 2523e47840
4 changed files with 49 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
package hashids
import (
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers/http"
"github.com/rogeecn/atom/utils/opt"
)
const DefaultPrefix = "HashIDs"
func DefaultProvider() container.ProviderContainer {
return container.ProviderContainer{
Provider: Provide,
Options: []opt.Option{
opt.Prefix(http.DefaultPrefix),
},
}
}
type Config struct {
Salt string
MinLength uint
}

View File

@@ -0,0 +1,23 @@
package hashids
import (
"github.com/rogeecn/atom/container"
"github.com/rogeecn/atom/providers/log"
"github.com/rogeecn/atom/utils/opt"
"github.com/speps/go-hashids/v2"
)
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
log.Fatal(err)
}
return container.Container.Provide(func() (*hashids.HashID, error) {
return hashids.NewWithData(&hashids.HashIDData{
MinLength: int(config.MinLength),
Salt: config.Salt,
})
}, o.DiOptions()...)
}