From 2523e478404a686305ce021f298b80f25bcc1603 Mon Sep 17 00:00:00 2001 From: yanghao05 Date: Tue, 30 May 2023 10:59:16 +0800 Subject: [PATCH] add hash ids provider --- go.mod | 1 + go.sum | 2 ++ providers/hashids/config.go | 23 +++++++++++++++++++++++ providers/hashids/hashids.go | 23 +++++++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 providers/hashids/config.go create mode 100644 providers/hashids/hashids.go diff --git a/go.mod b/go.mod index d9ab207..a319e06 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 5b8ba8b..027f6a4 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/providers/hashids/config.go b/providers/hashids/config.go new file mode 100644 index 0000000..8c9f713 --- /dev/null +++ b/providers/hashids/config.go @@ -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 +} diff --git a/providers/hashids/hashids.go b/providers/hashids/hashids.go new file mode 100644 index 0000000..582bbda --- /dev/null +++ b/providers/hashids/hashids.go @@ -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()...) +}