Skip to content

Commit

Permalink
feat: rm common
Browse files Browse the repository at this point in the history
  • Loading branch information
goxiaoy committed Jun 23, 2022
1 parent 705dff0 commit 392df71
Show file tree
Hide file tree
Showing 47 changed files with 260 additions and 246 deletions.
2 changes: 1 addition & 1 deletion common/basic_tenant_info.go → basic_tenant_info.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package saas

type TenantInfo interface {
GetId() string
Expand Down
2 changes: 1 addition & 1 deletion common/cache.go → cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package saas

import (
"container/list"
Expand Down
2 changes: 1 addition & 1 deletion common/context.go → context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package saas

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion common/db_name_generator.go → db_name_generator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common
package saas

import (
"context"
Expand Down
5 changes: 3 additions & 2 deletions ent/ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"context"
"database/sql"
"entgo.io/ent"
"github.com/goxiaoy/go-saas/common"
"github.com/goxiaoy/go-saas"

"github.com/goxiaoy/go-saas/data"
)

Expand All @@ -14,7 +15,7 @@ func Saas(next ent.Mutator) ent.Mutator {
}
return ent.MutateFunc(func(ctx context.Context, mutation ent.Mutation) (ent.Value, error) {
if hf, ok := mutation.(hasTenant); ok {
ct, _ := common.FromCurrentTenant(ctx)
ct, _ := saas.FromCurrentTenant(ctx)
at := data.FromAutoSetTenantId(ctx)
if ok && at {
if ct.GetId() != "" {
Expand Down
1 change: 1 addition & 0 deletions examples/ent/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/hashicorp/hcl/v2 v2.10.0 h1:1S1UnuhDGlv3gRFV4+0EdwB+znNP5HmcGbIqwnSCByg=
github.com/hashicorp/hcl/v2 v2.10.0/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down
34 changes: 17 additions & 17 deletions examples/ent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ package main
import (
"context"
"github.com/gin-gonic/gin"
"github.com/goxiaoy/go-saas/common"
"github.com/goxiaoy/go-saas"
"github.com/goxiaoy/go-saas/data"
sent "github.com/goxiaoy/go-saas/ent"
"github.com/goxiaoy/go-saas/examples/ent/shared/ent"
_ "github.com/goxiaoy/go-saas/examples/ent/shared/ent/runtime"
ent2 "github.com/goxiaoy/go-saas/examples/ent/tenant/ent"
_ "github.com/goxiaoy/go-saas/examples/ent/tenant/ent/runtime"
"github.com/goxiaoy/go-saas/gin/saas"
sgin "github.com/goxiaoy/go-saas/gin"
"github.com/goxiaoy/go-saas/seed"
_ "github.com/mattn/go-sqlite3"
)

type SharedDbProvider common.DbProvider[*ent.Client]
type TenantDbProvider common.DbProvider[*ent2.Client]
type SharedDbProvider saas.DbProvider[*ent.Client]
type TenantDbProvider saas.DbProvider[*ent2.Client]

func main() {
r := gin.Default()

cache := common.NewCache[string, *ent.Client]()
cache := saas.NewCache[string, *ent.Client]()
defer cache.Flush()
cache2 := common.NewCache[string, *ent2.Client]()
cache2 := saas.NewCache[string, *ent2.Client]()
defer cache.Flush()

sharedClientProvider := common.ClientProviderFunc[*ent.Client](func(ctx context.Context, s string) (*ent.Client, error) {
sharedClientProvider := saas.ClientProviderFunc[*ent.Client](func(ctx context.Context, s string) (*ent.Client, error) {
v, _, err := cache.GetOrSet(s, func() (*ent.Client, error) {
client, err := ent.Open("sqlite3", s, ent.Debug())
if err != nil {
Expand All @@ -37,7 +37,7 @@ func main() {
})
return v, err
})
tenantClientProvider := common.ClientProviderFunc[*ent2.Client](func(ctx context.Context, s string) (*ent2.Client, error) {
tenantClientProvider := saas.ClientProviderFunc[*ent2.Client](func(ctx context.Context, s string) (*ent2.Client, error) {

v, _, err := cache2.GetOrSet(s, func() (*ent2.Client, error) {
client, err := ent2.Open("sqlite3", s, ent2.Debug())
Expand All @@ -54,24 +54,24 @@ func main() {
//default database
conn.SetDefault("./shared.db?_fk=1")

var tenantStore common.TenantStore
var tenantStore saas.TenantStore

//host (shared) database use connection string from config
sharedDbProvider := common.NewDbProvider[*ent.Client](conn, sharedClientProvider)
sharedDbProvider := saas.NewDbProvider[*ent.Client](conn, sharedClientProvider)

tenantStore = &TenantStore{shared: sharedDbProvider}
mr := common.NewMultiTenancyConnStrResolver(tenantStore, conn)

mr := saas.NewMultiTenancyConnStrResolver(tenantStore, conn)
// tenant database use connection string from tenantStore
tenantDbProvider := common.NewDbProvider[*ent2.Client](mr, tenantClientProvider)
tenantDbProvider := saas.NewDbProvider[*ent2.Client](mr, tenantClientProvider)

r.Use(saas.MultiTenancy(tenantStore))
r.Use(sgin.MultiTenancy(tenantStore))

//return current tenant
r.GET("/tenant/current", func(c *gin.Context) {
rCtx := c.Request.Context()
tenantInfo, _ := common.FromCurrentTenant(rCtx)
trR := common.FromTenantResolveRes(rCtx)
tenantInfo, _ := saas.FromCurrentTenant(rCtx)
trR := saas.FromTenantResolveRes(rCtx)
c.JSON(200, gin.H{
"tenantId": tenantInfo.GetId(),
"resolvers": trR.AppliedResolvers,
Expand All @@ -80,7 +80,7 @@ func main() {

r.GET("/posts", func(c *gin.Context) {
ctx := c.Request.Context()
tenantInfo, _ := common.FromCurrentTenant(ctx)
tenantInfo, _ := saas.FromCurrentTenant(ctx)
if tenantInfo.GetId() == "" {
db := sharedDbProvider.Get(ctx, "")
e, err := db.Post.Query().All(ctx)
Expand Down
5 changes: 3 additions & 2 deletions examples/ent/shared/ent/schema/has_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"database/sql"
"entgo.io/ent"
"entgo.io/ent/entql"
"github.com/goxiaoy/go-saas"
"github.com/goxiaoy/go-saas/examples/ent/shared/ent/privacy"

"entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin"
"github.com/goxiaoy/go-saas/common"

"github.com/goxiaoy/go-saas/data"
)

Expand Down Expand Up @@ -37,7 +38,7 @@ func FilterTenantRule() privacy.QueryMutationRule {
WhereTenantID(p entql.StringP)
}
return privacy.FilterFunc(func(ctx context.Context, f privacy.Filter) error {
ct, _ := common.FromCurrentTenant(ctx)
ct, _ := saas.FromCurrentTenant(ctx)
e := data.FromMultiTenancyDataFilter(ctx)
hf, ok := f.(hasTenant)
if e && ok {
Expand Down
5 changes: 3 additions & 2 deletions examples/ent/tenant/ent/schema/has_tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"database/sql"
"entgo.io/ent"
"entgo.io/ent/entql"
"github.com/goxiaoy/go-saas"
"github.com/goxiaoy/go-saas/examples/ent/tenant/ent/privacy"

"entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin"
"github.com/goxiaoy/go-saas/common"

"github.com/goxiaoy/go-saas/data"
)

Expand Down Expand Up @@ -37,7 +38,7 @@ func FilterTenantRule() privacy.QueryMutationRule {
WhereTenantID(p entql.StringP)
}
return privacy.FilterFunc(func(ctx context.Context, f privacy.Filter) error {
ct, _ := common.FromCurrentTenant(ctx)
ct, _ := saas.FromCurrentTenant(ctx)
e := data.FromMultiTenancyDataFilter(ctx)
hf, ok := f.(hasTenant)
if e && ok {
Expand Down
11 changes: 6 additions & 5 deletions examples/ent/tenant_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package main

import (
"context"
"github.com/goxiaoy/go-saas/common"
"github.com/goxiaoy/go-saas"

"github.com/goxiaoy/go-saas/examples/ent/shared/ent"
"github.com/goxiaoy/go-saas/examples/ent/shared/ent/tenant"
"strconv"
Expand All @@ -12,8 +13,8 @@ type TenantStore struct {
shared SharedDbProvider
}

func (t *TenantStore) GetByNameOrId(ctx context.Context, nameOrId string) (*common.TenantConfig, error) {
ctx = common.NewCurrentTenant(ctx, "", "")
func (t *TenantStore) GetByNameOrId(ctx context.Context, nameOrId string) (*saas.TenantConfig, error) {
ctx = saas.NewCurrentTenant(ctx, "", "")
db := t.shared.Get(ctx, "")
i, err := strconv.Atoi(nameOrId)
var te *ent.Tenant
Expand All @@ -24,10 +25,10 @@ func (t *TenantStore) GetByNameOrId(ctx context.Context, nameOrId string) (*comm
}
if err != nil {
if ent.IsNotFound(err) {
return nil, common.ErrTenantNotFound
return nil, saas.ErrTenantNotFound
}
}
ret := common.NewTenantConfig(strconv.Itoa(te.ID), te.Name, te.Region)
ret := saas.NewTenantConfig(strconv.Itoa(te.ID), te.Name, te.Region)
conns, err := te.QueryConn().All(ctx)
if err != nil {
return nil, err
Expand Down
25 changes: 13 additions & 12 deletions examples/gorm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"github.com/gin-gonic/gin"
mysql2 "github.com/go-sql-driver/mysql"
"github.com/google/uuid"
"github.com/goxiaoy/go-saas/common"
"github.com/goxiaoy/go-saas"
sgin "github.com/goxiaoy/go-saas/gin"

"github.com/goxiaoy/go-saas/data"
"github.com/goxiaoy/go-saas/gin/saas"
sgorm "github.com/goxiaoy/go-saas/gorm"
"github.com/goxiaoy/go-saas/seed"
"gorm.io/driver/mysql"
Expand Down Expand Up @@ -38,14 +39,14 @@ func init() {
func main() {
flag.Parse()

cache := common.NewCache[string, *sgorm.DbWrap]()
cache := saas.NewCache[string, *sgorm.DbWrap]()
defer cache.Flush()

var connStrGen common.ConnStrGenerator
var connStrGen saas.ConnStrGenerator
switch driver {
case sqlite.DriverName:
sharedDsn = defaultSqliteSharedDsn
connStrGen = common.NewConnStrGenerator("./example-%s.db")
connStrGen = saas.NewConnStrGenerator("./example-%s.db")
case "mysql":
if len(sharedDsn) == 0 {
sharedDsn = defaultMysqlSharedDsn
Expand All @@ -56,7 +57,7 @@ func main() {
}
hostDbName := dd.DBName
dd.DBName = hostDbName + "-%s"
connStrGen = common.NewConnStrGenerator(dd.FormatDSN())
connStrGen = saas.NewConnStrGenerator(dd.FormatDSN())

ensureDbExist = func(s string) error {
dsn, err := mysql2.ParseDSN(s)
Expand Down Expand Up @@ -127,18 +128,18 @@ func main() {
//tenantStore use connection string from conn
tenantStore := &TenantStore{dbProvider: sgorm.NewDbProvider(conn, clientProvider)}

mr := common.NewMultiTenancyConnStrResolver(tenantStore, conn)
mr := saas.NewMultiTenancyConnStrResolver(tenantStore, conn)

//tenant dbProvider use connection string from tenant store
dbProvider := sgorm.NewDbProvider(mr, clientProvider)

r.Use(saas.MultiTenancy(tenantStore))
r.Use(sgin.MultiTenancy(tenantStore))

//return current tenant
r.GET("/tenant/current", func(c *gin.Context) {
rCtx := c.Request.Context()
tenantInfo, _ := common.FromCurrentTenant(rCtx)
trR := common.FromTenantResolveRes(rCtx)
tenantInfo, _ := saas.FromCurrentTenant(rCtx)
trR := saas.FromTenantResolveRes(rCtx)
c.JSON(200, gin.H{
"tenantId": tenantInfo.GetId(),
"resolvers": trR.AppliedResolvers,
Expand Down Expand Up @@ -175,15 +176,15 @@ func main() {
}
ctx := c.Request.Context()
//change to host side
ctx = common.NewCurrentTenant(ctx, "", "")
ctx = saas.NewCurrentTenant(ctx, "", "")
db := dbProvider.Get(ctx, "")
t := &Tenant{
ID: uuid.New().String(),
Name: json.Name,
DisplayName: json.Name,
}
if json.SeparateDb {
t3Conn, _ := connStrGen.Gen(ctx, common.NewBasicTenantInfo(t.ID, t.Name))
t3Conn, _ := connStrGen.Gen(ctx, saas.NewBasicTenantInfo(t.ID, t.Name))
t.Conn = []TenantConn{
{Key: data.Default, Value: t3Conn}, // use tenant3.db
}
Expand Down
9 changes: 5 additions & 4 deletions examples/gorm/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package main
import (
"context"
"fmt"
"github.com/goxiaoy/go-saas/common"
"github.com/goxiaoy/go-saas"

"github.com/goxiaoy/go-saas/data"
"github.com/goxiaoy/go-saas/gorm"
"github.com/goxiaoy/go-saas/seed"
Expand All @@ -13,10 +14,10 @@ import (

type Seed struct {
dbProvider gorm.DbProvider
connStrGen common.ConnStrGenerator
connStrGen saas.ConnStrGenerator
}

func NewSeed(dbProvider gorm.DbProvider, connStrGen common.ConnStrGenerator) *Seed {
func NewSeed(dbProvider gorm.DbProvider, connStrGen saas.ConnStrGenerator) *Seed {
return &Seed{dbProvider: dbProvider, connStrGen: connStrGen}
}

Expand All @@ -26,7 +27,7 @@ func (s *Seed) Seed(ctx context.Context, sCtx *seed.Context) error {
if sCtx.TenantId == "" {
//seed host
t3 := Tenant{ID: "3", Name: "Test3"}
t3Conn, _ := s.connStrGen.Gen(ctx, common.NewBasicTenantInfo(t3.ID, t3.Name))
t3Conn, _ := s.connStrGen.Gen(ctx, saas.NewBasicTenantInfo(t3.ID, t3.Name))
t3.Conn = []TenantConn{
{Key: data.Default, Value: t3Conn}, // use tenant3.db
}
Expand Down
13 changes: 7 additions & 6 deletions examples/gorm/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package main
import (
"context"
"errors"
"github.com/goxiaoy/go-saas/common"
"github.com/goxiaoy/go-saas"

sgorm "github.com/goxiaoy/go-saas/gorm"
"gorm.io/gorm"
"time"
Expand Down Expand Up @@ -41,24 +42,24 @@ type TenantStore struct {
dbProvider sgorm.DbProvider
}

func (t *TenantStore) GetByNameOrId(ctx context.Context, nameOrId string) (*common.TenantConfig, error) {
func (t *TenantStore) GetByNameOrId(ctx context.Context, nameOrId string) (*saas.TenantConfig, error) {
//change to host side
ctx = common.NewCurrentTenant(ctx, "", "")
ctx = saas.NewCurrentTenant(ctx, "", "")
db := t.dbProvider.Get(ctx, "")
var tenant Tenant
err := db.Model(&Tenant{}).Preload("Conn").Where("id = ? OR name = ?", nameOrId, nameOrId).First(&tenant).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, common.ErrTenantNotFound
return nil, saas.ErrTenantNotFound
} else {
return nil, err
}
}
ret := common.NewTenantConfig(tenant.ID, tenant.Name, tenant.Region)
ret := saas.NewTenantConfig(tenant.ID, tenant.Name, tenant.Region)
for _, conn := range tenant.Conn {
ret.Conn[conn.Key] = conn.Value
}
return ret, nil
}

var _ common.TenantStore = (*TenantStore)(nil)
var _ saas.TenantStore = (*TenantStore)(nil)
Loading

0 comments on commit 392df71

Please sign in to comment.