Skip to content

Commit

Permalink
update: deprecate namespace and switch to multi-tenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
kainonly committed Oct 22, 2023
1 parent 1b5ad92 commit 7fafa2a
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 130 deletions.
27 changes: 4 additions & 23 deletions captcha/captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,15 @@ import (
)

type Captcha struct {
Namespace string
RDb *redis.Client
RDb *redis.Client
}

func New(options ...Option) *Captcha {
x := new(Captcha)
for _, v := range options {
v(x)
}
return x
}

type Option func(x *Captcha)

func SetNamespace(v string) Option {
return func(x *Captcha) {
x.Namespace = v
}
}

func SetRedis(v *redis.Client) Option {
return func(x *Captcha) {
x.RDb = v
}
func New(rdb *redis.Client) *Captcha {
return &Captcha{RDb: rdb}
}

func (x *Captcha) Key(name string) string {
return fmt.Sprintf(`%s:captcha:%s`, x.Namespace, name)
return fmt.Sprintf(`captcha:%s`, name)
}

func (x *Captcha) Create(ctx context.Context, name string, code string, ttl time.Duration) string {
Expand Down
6 changes: 1 addition & 5 deletions captcha/captcha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ import (
var x *captcha.Captcha

func TestMain(m *testing.M) {
namespace := os.Getenv("NAMESPACE")
opts, err := redis.ParseURL(os.Getenv("DATABASE_REDIS"))
if err != nil {
log.Fatalln(err)
}
x = captcha.New(
captcha.SetNamespace(namespace),
captcha.SetRedis(redis.NewClient(opts)),
)
x = captcha.New(redis.NewClient(opts))
os.Exit(m.Run())
}

Expand Down
27 changes: 4 additions & 23 deletions locker/locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,15 @@ import (
)

type Locker struct {
Namespace string
RDb *redis.Client
RDb *redis.Client
}

func New(options ...Option) *Locker {
x := new(Locker)
for _, v := range options {
v(x)
}
return x
}

type Option func(x *Locker)

func SetNamespace(v string) Option {
return func(x *Locker) {
x.Namespace = v
}
}

func SetRedis(v *redis.Client) Option {
return func(x *Locker) {
x.RDb = v
}
func New(rdb *redis.Client) *Locker {
return &Locker{RDb: rdb}
}

func (x *Locker) Key(name string) string {
return fmt.Sprintf(`%s:locker:%s`, x.Namespace, name)
return fmt.Sprintf(`locker:%s`, name)
}

func (x *Locker) Update(ctx context.Context, name string, ttl time.Duration) int64 {
Expand Down
6 changes: 1 addition & 5 deletions locker/locker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ import (
var x *locker.Locker

func TestMain(m *testing.M) {
namespace := os.Getenv("NAMESPACE")
opts, err := redis.ParseURL(os.Getenv("DATABASE_REDIS"))
if err != nil {
log.Fatalln(err)
}
x = locker.New(
locker.SetNamespace(namespace),
locker.SetRedis(redis.NewClient(opts)),
)
x = locker.New(redis.NewClient(opts))
os.Exit(m.Run())
}

Expand Down
6 changes: 0 additions & 6 deletions rest/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ func New(options ...Option) *Service {

type Option func(x *Service)

func SetNamespace(v string) Option {
return func(x *Service) {
x.Namespace = v
}
}

func SetMongoClient(v *mongo.Client) Option {
return func(x *Service) {
x.Mgo = v
Expand Down
2 changes: 0 additions & 2 deletions rest/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ func TestMain(m *testing.M) {
if err := UseNats(ctx); err != nil {
panic(err)
}
namespace := os.Getenv("NAMESPACE")
xcipher, _ := cipher.New("6ixSiEXaqxsJTozbnxQ76CWdZXB2JazK")
service = rest.New(
rest.SetNamespace(namespace),
rest.SetMongoClient(mgo),
rest.SetDatabase(db),
rest.SetRedis(rdb),
Expand Down
45 changes: 22 additions & 23 deletions rest/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
"net/http"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -55,7 +54,7 @@ func (x *Controller) Create(ctx context.Context, c *app.RequestContext) {
return
}

c.Status(http.StatusNoContent)
c.Status(204)
return
}

Expand All @@ -65,7 +64,7 @@ func (x *Controller) Create(ctx context.Context, c *app.RequestContext) {
return
}

c.JSON(http.StatusCreated, r)
c.JSON(201, r)
}

type BulkCreateDto struct {
Expand Down Expand Up @@ -108,7 +107,7 @@ func (x *Controller) BulkCreate(ctx context.Context, c *app.RequestContext) {
return
}

c.Status(http.StatusNoContent)
c.Status(204)
return
}

Expand All @@ -118,7 +117,7 @@ func (x *Controller) BulkCreate(ctx context.Context, c *app.RequestContext) {
return
}

c.JSON(http.StatusCreated, r)
c.JSON(201, r)
}

type SizeDto struct {
Expand Down Expand Up @@ -151,7 +150,7 @@ func (x *Controller) Size(ctx context.Context, c *app.RequestContext) {
}

c.Header("x-total", strconv.Itoa(int(size)))
c.Status(http.StatusNoContent)
c.Status(204)
}

type FindDto struct {
Expand Down Expand Up @@ -220,7 +219,7 @@ func (x *Controller) Find(ctx context.Context, c *app.RequestContext) {
}

c.Header("x-total", strconv.Itoa(int(size)))
c.JSON(http.StatusOK, data)
c.JSON(200, data)
}

type FindOneDto struct {
Expand Down Expand Up @@ -256,7 +255,7 @@ func (x *Controller) FindOne(ctx context.Context, c *app.RequestContext) {
return
}

c.JSON(http.StatusOK, data)
c.JSON(200, data)
}

type FindByIdDto struct {
Expand Down Expand Up @@ -287,7 +286,7 @@ func (x *Controller) FindById(ctx context.Context, c *app.RequestContext) {
return
}

c.JSON(http.StatusOK, data)
c.JSON(200, data)
}

type UpdateDto struct {
Expand Down Expand Up @@ -335,7 +334,7 @@ func (x *Controller) Update(ctx context.Context, c *app.RequestContext) {
return
}

c.Status(http.StatusNoContent)
c.Status(204)
return
}

Expand All @@ -345,7 +344,7 @@ func (x *Controller) Update(ctx context.Context, c *app.RequestContext) {
return
}

c.JSON(http.StatusOK, r)
c.JSON(200, r)
}

type UpdateByIdDto struct {
Expand Down Expand Up @@ -389,7 +388,7 @@ func (x *Controller) UpdateById(ctx context.Context, c *app.RequestContext) {
return
}

c.Status(http.StatusNoContent)
c.Status(204)
return
}

Expand All @@ -399,7 +398,7 @@ func (x *Controller) UpdateById(ctx context.Context, c *app.RequestContext) {
return
}

c.JSON(http.StatusOK, r)
c.JSON(200, r)
}

type ReplaceDto struct {
Expand Down Expand Up @@ -441,7 +440,7 @@ func (x *Controller) Replace(ctx context.Context, c *app.RequestContext) {
return
}

c.Status(http.StatusNoContent)
c.Status(204)
return
}

Expand All @@ -451,7 +450,7 @@ func (x *Controller) Replace(ctx context.Context, c *app.RequestContext) {
return
}

c.JSON(http.StatusOK, r)
c.JSON(200, r)
}

type DeleteDto struct {
Expand Down Expand Up @@ -484,7 +483,7 @@ func (x *Controller) Delete(ctx context.Context, c *app.RequestContext) {
return
}

c.Status(http.StatusNoContent)
c.Status(204)
return
}

Expand All @@ -494,7 +493,7 @@ func (x *Controller) Delete(ctx context.Context, c *app.RequestContext) {
return
}

c.JSON(http.StatusOK, r)
c.JSON(200, r)
}

type BulkDeleteDto struct {
Expand Down Expand Up @@ -531,7 +530,7 @@ func (x *Controller) BulkDelete(ctx context.Context, c *app.RequestContext) {
return
}

c.Status(http.StatusNoContent)
c.Status(204)
return
}

Expand All @@ -541,7 +540,7 @@ func (x *Controller) BulkDelete(ctx context.Context, c *app.RequestContext) {
return
}

c.JSON(http.StatusOK, r)
c.JSON(200, r)
}

type SortDto struct {
Expand Down Expand Up @@ -577,7 +576,7 @@ func (x *Controller) Sort(ctx context.Context, c *app.RequestContext) {
return
}

c.Status(http.StatusNoContent)
c.Status(204)
return
}

Expand All @@ -587,13 +586,13 @@ func (x *Controller) Sort(ctx context.Context, c *app.RequestContext) {
return
}

c.Status(http.StatusNoContent)
c.Status(204)
}

func (x *Controller) Transaction(ctx context.Context, c *app.RequestContext) {
txn := help.Uuid()
x.Service.Transaction(ctx, txn)
c.JSON(http.StatusCreated, utils.H{
c.JSON(201, utils.H{
"txn": txn,
})
}
Expand All @@ -615,5 +614,5 @@ func (x *Controller) Commit(ctx context.Context, c *app.RequestContext) {
return
}

c.JSON(http.StatusOK, r)
c.JSON(200, r)
}
9 changes: 4 additions & 5 deletions rest/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
)

type Service struct {
Namespace string
Mgo *mongo.Client
Db *mongo.Database
RDb *redis.Client
Expand Down Expand Up @@ -209,7 +208,7 @@ func (x *Service) Sort(ctx context.Context, name string, key string, ids []primi
}

func (x *Service) Transaction(ctx context.Context, txn string) {
key := fmt.Sprintf(`%s:transaction:%s`, x.Namespace, txn)
key := fmt.Sprintf(`transaction:%s`, txn)
x.RDb.LPush(ctx, key, time.Now().Format(time.RFC3339)).Val()
x.RDb.Expire(ctx, key, time.Hour*5).Val()
}
Expand All @@ -234,7 +233,7 @@ func (x *Service) TxnNotExists(ctx context.Context, key string) (err error) {
}

func (x *Service) Pending(ctx context.Context, txn string, dto PendingDto) (err error) {
key := fmt.Sprintf(`%s:transaction:%s`, x.Namespace, txn)
key := fmt.Sprintf(`transaction:%s`, txn)
if err = x.TxnNotExists(ctx, key); err != nil {
return
}
Expand All @@ -249,7 +248,7 @@ func (x *Service) Pending(ctx context.Context, txn string, dto PendingDto) (err
}

func (x *Service) Commit(ctx context.Context, txn string) (_ interface{}, err error) {
key := fmt.Sprintf(`%s:transaction:%s`, x.Namespace, txn)
key := fmt.Sprintf(`transaction:%s`, txn)
if err = x.TxnNotExists(ctx, key); err != nil {
return
}
Expand Down Expand Up @@ -461,7 +460,7 @@ func (x *Service) Publish(ctx context.Context, name string, dto PublishDto) (err
}

b, _ := sonic.Marshal(dto)
subject := fmt.Sprintf(`%s.events.%s`, x.Namespace, name)
subject := fmt.Sprintf(`events.%s`, name)
if _, err = x.JetStream.Publish(subject, b, nats.Context(ctx)); err != nil {
return
}
Expand Down
Loading

0 comments on commit 7fafa2a

Please sign in to comment.