Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: add canonical link whenever using custom domains (#348)

Guilherme Oenning0c455d64d87e
diff --git a/app/handlers/signin_test.go b/app/handlers/signin_test.go
index ccce5f765..5a0c4e78a 100644
--- a/app/handlers/signin_test.go
+++ b/app/handlers/signin_test.go
@@ -191,7 +191,7 @@ func TestCallbackHandler_NewUser_PrivateTenant(t *testing.T) {
Expect(user).To(BeNil())
Expect(code).To(Equal(http.StatusTemporaryRedirect))
- Expect(response.Header().Get("Location")).To(Equal("http://avengers.test.fider.io/not-invited"))
+ Expect(response.Header().Get("Location")).To(Equal("http://ideas.theavengers.com/not-invited"))
}
func TestSignInByEmailHandler_WithoutEmail(t *testing.T) {
diff --git a/app/middlewares/domain.go b/app/middlewares/domain.go
index 1ee31e85f..66e62eff8 100644
--- a/app/middlewares/domain.go
+++ b/app/middlewares/domain.go
@@ -1,6 +1,7 @@
package middlewares
import (
+ "fmt"
"net/http"
"net/url"
@@ -45,7 +46,7 @@ func MultiTenant() web.MiddlewareFunc {
hostname := stripPort(c.Request.Host)
// If no tenant is specified, redirect user to getfider.com
- // This is only vadid for fider.io hosting
+ // This is only valid for fider.io hosting
if (env.IsProduction() && hostname == "fider.io") ||
(env.IsDevelopment() && hostname == "dev.fider.io") {
return c.Redirect(http.StatusTemporaryRedirect, "https://getfider.com")
@@ -54,6 +55,10 @@ func MultiTenant() web.MiddlewareFunc {
tenant, err := c.Services().Tenants.GetByDomain(hostname)
if err == nil {
c.SetTenant(tenant)
+ if tenant.CNAME != "" && !c.IsAjax() {
+ link := c.TenantBaseURL(tenant) + c.Request.URL.RequestURI()
+ c.Response.Header().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"", link))
+ }
return next(c)
}
diff --git a/app/middlewares/domain_test.go b/app/middlewares/domain_test.go
index 8eb5520ce..fcbe722f4 100644
--- a/app/middlewares/domain_test.go
+++ b/app/middlewares/domain_test.go
@@ -77,6 +77,60 @@ func TestMultiTenant_UnknownDomain(t *testing.T) {
Expect(status).To(Equal(http.StatusNotFound))
}
+func TestMultiTenant_CanonicalHeader(t *testing.T) {
+ RegisterTestingT(t)
+
+ var testCases = []struct {
+ input string
+ output string
+ isAjax bool
+ }{
+ {
+ "http://avengers.test.fider.io/",
+ "<http://ideas.theavengers.com/>; rel=\"canonical\"",
+ false,
+ },
+ {
+ "http://avengers.test.fider.io/",
+ "",
+ true,
+ },
+ {
+ "http://avengers.test.fider.io/ideas",
+ "<http://ideas.theavengers.com/ideas>; rel=\"canonical\"",
+ false,
+ },
+ {
+ "http://avengers.test.fider.io/ideas?q=1",
+ "<http://ideas.theavengers.com/ideas?q=1>; rel=\"canonical\"",
+ false,
+ },
+ {
+ "http://demo.test.fider.io",
+ "",
+ false,
+ },
+ }
+
+ for _, testCase := range testCases {
+ server, _ := mock.NewServer()
+ server.Use(middlewares.MultiTenant())
+
+ if testCase.isAjax {
+ server.AddHeader("Accept", "application/json")
+ }
+ status, response := server.
+ WithURL(testCase.input).
+ Execute(func(c web.Context) error {
+ return c.Ok(web.Map{})
+ })
+
+ Expect(status).To(Equal(http.StatusOK))
+ Expect(response.HeaderMap.Get("Link")).To(Equal(testCase.output))
+ }
+
+}
+
func TestSingleTenant_NoTenants_RedirectToSignUp(t *testing.T) {
RegisterTestingT(t)
diff --git a/app/pkg/mock/setup.go b/app/pkg/mock/setup.go
index a21a7cb19..d965a136b 100644
--- a/app/pkg/mock/setup.go
+++ b/app/pkg/mock/setup.go
@@ -59,6 +59,7 @@ func createServices(seed bool) *app.Services {
if seed {
DemoTenant, _ = services.Tenants.Add("Demonstration", "demo", models.TenantActive)
AvengersTenant, _ = services.Tenants.Add("Avengers", "avengers", models.TenantActive)
+ AvengersTenant.CNAME = "ideas.theavengers.com"
JonSnow = &models.User{
Name: "Jon Snow",
diff --git a/app/pkg/web/context.go b/app/pkg/web/context.go
index 3158bb635..8b8557853 100644
--- a/app/pkg/web/context.go
+++ b/app/pkg/web/context.go
@@ -347,10 +347,18 @@ func (ctx *Context) TenantBaseURL(tenant *models.Tenant) string {
if ctx.Request.TLS != nil || ctx.Request.Header.Get("X-Forwarded-Proto") == "https" {
protocol = "https"
}
- address := protocol + "://" + tenant.Subdomain + env.MultiTenantDomain()
+
+ address := protocol + "://"
+ if tenant.CNAME != "" {
+ address += tenant.CNAME
+ } else {
+ address += tenant.Subdomain + env.MultiTenantDomain()
+ }
+
if port != "" {
address += ":" + port
}
+
return address
}
diff --git a/app/pkg/web/context_test.go b/app/pkg/web/context_test.go
index 8add371e9..c1f6d9cc1 100644
--- a/app/pkg/web/context_test.go
+++ b/app/pkg/web/context_test.go
@@ -42,9 +42,21 @@ func TestTenantURL(t *testing.T) {
ctx := newGetContext(nil)
tenant := &models.Tenant{
ID: 1,
- Subdomain: "lotr",
+ Subdomain: "theavengers",
}
- Expect(ctx.TenantBaseURL(tenant)).To(Equal("http://lotr.test.fider.io:3000"))
+ Expect(ctx.TenantBaseURL(tenant)).To(Equal("http://theavengers.test.fider.io:3000"))
+}
+
+func TestTenantURL_WithCNAME(t *testing.T) {
+ RegisterTestingT(t)
+
+ ctx := newGetContext(nil)
+ tenant := &models.Tenant{
+ ID: 1,
+ Subdomain: "theavengers",
+ CNAME: "ideas.theavengers.com",
+ }
+ Expect(ctx.TenantBaseURL(tenant)).To(Equal("http://ideas.theavengers.com:3000"))
}
func TestTenantURL_SingleTenantMode(t *testing.T) {
@@ -54,7 +66,7 @@ func TestTenantURL_SingleTenantMode(t *testing.T) {
ctx := newGetContext(nil)
tenant := &models.Tenant{
ID: 1,
- Subdomain: "lotr",
+ Subdomain: "theavengers",
}
Expect(ctx.TenantBaseURL(tenant)).To(Equal("http://demo.test.fider.io:3000"))
}