Skip to content

Commit

Permalink
ALPHABET
Browse files Browse the repository at this point in the history
  • Loading branch information
icowan committed Mar 28, 2020
1 parent 1c21346 commit 50b57e3
Show file tree
Hide file tree
Showing 4 changed files with 370 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
fs = flag.NewFlagSet("hello", flag.ExitOnError)
httpAddr = fs.String("http-addr", ":8080", "HTTP listen address")
dbDrive = fs.String("db-drive", "mongo", "db drive type, default: mongo")
mongoAddr = fs.String("mongo-addr", "mongodb://localhost:32768", "mongodb uri, default: mongodb://localhost:27017")
mongoAddr = fs.String("mongo-addr", "mongodb://root:admin@localhost:27017/?authSource=admin", "mongodb uri, default: mongodb://localhost:27017")
redisDrive = fs.String("redis-drive", "single", "redis drive: single or cluster")
redisHosts = fs.String("redis-hosts", "localhost:6379", "redis hosts, many ';' split")
redisPassword = fs.String("redis-password", "", "redis password")
Expand All @@ -46,7 +46,7 @@ var (
devCors = fs.String("dev-cors", "false", "is develop")
rateBucketNum = fs.Int("rate-bucket", 10, "rate bucket num")
maxLength = fs.Int("max-length", -1, "code length")
alphabet = fs.String("alphabet", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "alphabet length")
alphabet = fs.String("alphabet", "", "alphabet length")
err error
)

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.4.2
github.com/tebeka/strftime v0.1.3 // indirect
github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf
github.com/tidwall/pretty v1.0.0 // indirect
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
github.com/xdg/stringprep v1.0.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ package service
import (
"context"
"github.com/go-kit/kit/log"
"github.com/icowan/shorter/pkg/shortid"
"github.com/pkg/errors"
"github.com/teris-io/shortid"
"strings"
"time"
)
Expand Down Expand Up @@ -57,11 +57,13 @@ func (s *service) Post(ctx context.Context, domain string) (redirect *Redirect,
now := time.Now()
now = now.In(time.Local)
var code string
code = shortid.MustGenerate()
if s.alphabet != "" {
code, _ = shortid.MustNew(0, s.alphabet, 1).Generate()
} else {
code = shortid.MustGenerate()
for _, v := range s.alphabet {
code = strings.ReplaceAll(code, string(v), "")
}
}

// todo 考虑如何处理垃圾数据的问题 得复的url 不同的code

if s.maxLen > 0 {
Expand Down
Loading

0 comments on commit 50b57e3

Please sign in to comment.