-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* v1 wallet * . * . * rename some fields * rename some fields * change prefix * rename comment * shuffle * create id if not specified * typo * direction * . * . * . * cleanup * move out redis * check secure conn * fix balance * fix time format * add examples and publicapi json * add config json * add counter prefix
- Loading branch information
Showing
17 changed files
with
2,540 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/go-redis/redis/v8" | ||
"github.com/micro/micro/v3/service/config" | ||
log "github.com/micro/micro/v3/service/logger" | ||
) | ||
|
||
type Counter struct { | ||
sync.RWMutex | ||
|
||
prefix string | ||
client *redis.Client | ||
} | ||
|
||
type Config struct { | ||
Address string `json:"address"` | ||
Username string `json:"username,omitempty"` | ||
Password string `json:"password,omitempty"` | ||
Secure bool `json:"secure,omitempty"` | ||
} | ||
|
||
const Nil = redis.Nil | ||
|
||
func Key(args ...string) string { | ||
return strings.Join(args, ":") | ||
} | ||
|
||
func (c *Counter) Incr(ctx context.Context, key, path string, delta int64) (int64, error) { | ||
return c.client.IncrBy(ctx, fmt.Sprintf("%s:%s:%s", c.prefix, key, path), delta).Result() | ||
} | ||
|
||
func (c *Counter) Decr(ctx context.Context, key, path string, delta int64) (int64, error) { | ||
return c.client.DecrBy(ctx, fmt.Sprintf("%s:%s:%s", c.prefix, key, path), delta).Result() | ||
} | ||
|
||
func (c *Counter) Read(ctx context.Context, key, path string) (int64, error) { | ||
ret, err := c.client.Get(ctx, fmt.Sprintf("%s:%s:%s", c.prefix, key, path)).Int64() | ||
if err == redis.Nil { | ||
return 0, nil | ||
} | ||
return ret, err | ||
} | ||
|
||
func (c *Counter) Reset(ctx context.Context, key, path string) error { | ||
return c.client.Set(ctx, fmt.Sprintf("%s:%s:%s", c.prefix, key, path), 0, 0).Err() | ||
} | ||
|
||
func (c *Counter) Delete(ctx context.Context, key string) error { | ||
keys, err := c.client.Keys(ctx, fmt.Sprintf("%s:%s:*", c.prefix, key)).Result() | ||
if err != nil { | ||
if err == redis.Nil { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
if len(keys) == 0 { | ||
return nil | ||
} | ||
|
||
if err := c.client.Del(ctx, keys...).Err(); err != nil && err != redis.Nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func NewCounter(prefix string) *Counter { | ||
var redisConfig Config | ||
|
||
val, err := config.Get("micro.redis") | ||
if err != nil { | ||
log.Fatalf("No redis config found %s", err) | ||
} | ||
|
||
if err := val.Scan(&redisConfig); err != nil { | ||
log.Fatalf("Error parsing redis config %s", err) | ||
} | ||
|
||
log.Infof("Using %v", redisConfig) | ||
|
||
opts := &redis.Options{ | ||
Addr: redisConfig.Address, | ||
Username: redisConfig.Username, | ||
Password: redisConfig.Password, | ||
} | ||
if redisConfig.Secure { | ||
opts.TLSConfig = &tls.Config{ | ||
InsecureSkipVerify: false, | ||
} | ||
} | ||
|
||
rc := redis.NewClient(opts) | ||
|
||
_, err = rc.Ping(context.Background()).Result() | ||
if err != nil { | ||
log.Fatalf("Failed to ping redis: %v", err) | ||
} | ||
|
||
return &Counter{ | ||
prefix: Key(prefix, "counter"), | ||
client: rc, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
wallet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FROM alpine | ||
ADD wallet /wallet | ||
ENTRYPOINT [ "/wallet" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
GOPATH:=$(shell go env GOPATH) | ||
.PHONY: init | ||
init: | ||
go get -u github.com/golang/protobuf/proto | ||
go get -u github.com/golang/protobuf/protoc-gen-go | ||
go get github.com/micro/micro/v3/cmd/protoc-gen-micro | ||
|
||
.PHONY: api | ||
api: | ||
protoc --openapi_out=. --proto_path=. proto/wallet.proto | ||
|
||
.PHONY: proto | ||
proto: | ||
protoc --proto_path=. --micro_out=. --go_out=:. proto/wallet.proto | ||
|
||
.PHONY: build | ||
build: | ||
go build -o wallet *.go | ||
|
||
.PHONY: test | ||
test: | ||
go test -v ./... -cover | ||
|
||
.PHONY: docker | ||
docker: | ||
docker build . -t wallet:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Digital Wallet | ||
|
||
# Wallet Service | ||
|
||
A digital wallet you can manage multiple currencies from. Credit, debit, transfer and more. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"micro": { | ||
"redis": { | ||
"address":"127.0.0.1:6379" | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.