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

1
go.mod
View File

@@ -15,6 +15,7 @@ require (
github.com/mojocn/base64Captcha v1.3.5
github.com/pkg/errors v0.9.1
github.com/rogeecn/gen v1.0.11
github.com/speps/go-hashids/v2 v2.0.1
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
go-micro.dev/v4 v4.10.2

2
go.sum
View File

@@ -473,6 +473,8 @@ github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/speps/go-hashids/v2 v2.0.1 h1:ViWOEqWES/pdOSq+C1SLVa8/Tnsd52XC34RY7lt7m4g=
github.com/speps/go-hashids/v2 v2.0.1/go.mod h1:47LKunwvDZki/uRVD6NImtyk712yFzIs3UF3KlHohGw=
github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=

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()...)
}