-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from thslopes/master
feat: tenant by host middleware
- Loading branch information
Showing
6 changed files
with
172 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,5 @@ testdata/ | |
|
||
restql.yml | ||
|
||
.DS_Store | ||
.DS_Store | ||
.vscode/ |
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
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,48 @@ | ||
package middleware | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/b2wdigital/restQL-golang/v6/pkg/restql" | ||
|
||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
type tenantByHost struct { | ||
log restql.Logger | ||
tenantsByHost map[string]string | ||
defaultTenant string | ||
} | ||
|
||
func newTenantByHost(log restql.Logger, defaultTenant string, tenantsByHost map[string]string) Middleware { | ||
return tenantByHost{ | ||
log: log, | ||
tenantsByHost: tenantsByHost, | ||
defaultTenant: defaultTenant, | ||
} | ||
} | ||
|
||
func (r tenantByHost) Apply(h fasthttp.RequestHandler) fasthttp.RequestHandler { | ||
return func(ctx *fasthttp.RequestCtx) { | ||
r.setTenant(ctx) | ||
h(ctx) | ||
} | ||
} | ||
|
||
func (u tenantByHost) setTenant(ctx *fasthttp.RequestCtx) { | ||
if string(ctx.QueryArgs().Peek("tenant")) != "" { | ||
u.log.Debug("tenant already set", "tenant", string(ctx.QueryArgs().Peek("tenant"))) | ||
return | ||
} | ||
|
||
for k, v := range u.tenantsByHost { | ||
if strings.Contains(string(ctx.Request.Host()), k) { | ||
u.log.Debug("setting tenant", "tenant", v, "host", string(ctx.Request.Host())) | ||
ctx.QueryArgs().Set("tenant", v) | ||
return | ||
} | ||
} | ||
u.log.Debug("setting default tenant", "tenant", u.defaultTenant, "host", string(ctx.Request.Host())) | ||
ctx.QueryArgs().Set("tenant", u.defaultTenant) | ||
|
||
} |
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,87 @@ | ||
package middleware | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/b2wdigital/restQL-golang/v6/pkg/restql" | ||
"github.com/valyala/fasthttp" | ||
) | ||
|
||
func Test_tenantByHost_setTenant(t *testing.T) { | ||
type fields struct { | ||
log restql.Logger | ||
tenantsByHosts map[string]string | ||
defaultTenant string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
host string | ||
tenant string | ||
wantTenant string | ||
}{ | ||
{ | ||
name: "should set tenant", | ||
fields: fields{ | ||
log: noOpLogger{}, | ||
tenantsByHosts: map[string]string{ | ||
"americanas.test": "acom-npf", | ||
}, | ||
defaultTenant: "default-tenant", | ||
}, | ||
host: "americanas.test", | ||
wantTenant: "acom-npf", | ||
}, | ||
{ | ||
name: "should not set tenant if already set", | ||
fields: fields{ | ||
log: noOpLogger{}, | ||
tenantsByHosts: map[string]string{ | ||
"americanas.test": "acom-npf", | ||
}, | ||
defaultTenant: "default-tenant", | ||
}, | ||
tenant: "previous-set-tenant", | ||
host: "americanas.test", | ||
wantTenant: "previous-set-tenant", | ||
}, | ||
{ | ||
name: "should set default tenant", | ||
fields: fields{ | ||
log: noOpLogger{}, | ||
tenantsByHosts: map[string]string{ | ||
"americanas.test": "acom-npf", | ||
}, | ||
defaultTenant: "default-tenant", | ||
}, | ||
host: "unknown.test", | ||
wantTenant: "default-tenant", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
u := tenantByHost{ | ||
log: tt.fields.log, | ||
tenantsByHost: tt.fields.tenantsByHosts, | ||
defaultTenant: tt.fields.defaultTenant, | ||
} | ||
ctx := &fasthttp.RequestCtx{} | ||
ctx.Request.SetHost(tt.host) | ||
ctx.QueryArgs().Add("tenant", tt.tenant) | ||
u.setTenant(ctx) | ||
if got := string(ctx.QueryArgs().Peek("tenant")); got != tt.wantTenant { | ||
t.Errorf("tenantByHost.setTenant() = %v, want %v", got, tt.wantTenant) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type noOpLogger struct{} | ||
|
||
func (n noOpLogger) Panic(msg string, fields ...interface{}) {} | ||
func (n noOpLogger) Fatal(msg string, fields ...interface{}) {} | ||
func (n noOpLogger) Error(msg string, err error, fields ...interface{}) {} | ||
func (n noOpLogger) Warn(msg string, fields ...interface{}) {} | ||
func (n noOpLogger) Info(msg string, fields ...interface{}) {} | ||
func (n noOpLogger) Debug(msg string, fields ...interface{}) {} | ||
func (n noOpLogger) With(key string, value interface{}) restql.Logger { return n } |