-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom domain for local resolution
- Loading branch information
Showing
23 changed files
with
195 additions
and
56 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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
[sites."main"] | ||
context="main" | ||
context = "main" | ||
|
||
[sites."dev"] | ||
context="dev" | ||
context = "dev" | ||
|
||
[sites."custom"] | ||
context = "dev" | ||
domain = "example.com" |
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,15 @@ | ||
package local | ||
|
||
import ( | ||
"github.com/oursky/pageship/internal/config" | ||
"github.com/oursky/pageship/internal/domain" | ||
) | ||
|
||
func NewResolver(defaultSite string, sites map[string]config.SitesConfigEntry) (domain.Resolver, error) { | ||
if len(sites) == 0 { | ||
// Custom domain resolution is not supported for ad-hoc sites. | ||
return &domain.ResolverNull{}, nil | ||
} | ||
|
||
return newResolverStatic(defaultSite, sites) | ||
} |
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,44 @@ | ||
package local | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/oursky/pageship/internal/config" | ||
"github.com/oursky/pageship/internal/domain" | ||
) | ||
|
||
type resolverStatic struct { | ||
domains map[string]string | ||
} | ||
|
||
func newResolverStatic(defaultSite string, sites map[string]config.SitesConfigEntry) (*resolverStatic, error) { | ||
domains := make(map[string]string) | ||
for id, site := range sites { | ||
if site.Domain == "" { | ||
continue | ||
} | ||
|
||
if _, exists := domains[site.Domain]; exists { | ||
return nil, fmt.Errorf("duplicated domain: %q", site.Domain) | ||
} | ||
|
||
if defaultSite != "-" && id == defaultSite { | ||
id = "" | ||
} | ||
domains[site.Domain] = id | ||
} | ||
|
||
return &resolverStatic{domains: domains}, nil | ||
} | ||
|
||
func (h *resolverStatic) Kind() string { return "static config" } | ||
|
||
func (h *resolverStatic) Resolve(ctx context.Context, hostname string) (string, error) { | ||
id, ok := h.domains[hostname] | ||
if !ok { | ||
return "", domain.ErrDomainNotFound | ||
} | ||
|
||
return id, nil | ||
} |
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,13 @@ | ||
package domain | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
var ErrDomainNotFound = errors.New("domain not found") | ||
|
||
type Resolver interface { | ||
Kind() string | ||
Resolve(ctx context.Context, hostname string) (string, error) | ||
} |
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,13 @@ | ||
package domain | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type ResolverNull struct{} | ||
|
||
func (h *ResolverNull) Kind() string { return "null" } | ||
|
||
func (h *ResolverNull) Resolve(ctx context.Context, hostname string) (string, error) { | ||
return "", ErrDomainNotFound | ||
} |
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,20 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/oursky/pageship/internal/site" | ||
) | ||
|
||
func RedirectCustomDomain(site *site.Descriptor, next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if site.Domain != "" && r.Host != site.Domain { | ||
// Site with custom domain must be accessed through the custom domain. | ||
url := *r.URL | ||
url.Host = site.Domain | ||
http.Redirect(w, r, url.String(), http.StatusMovedPermanently) | ||
return | ||
} | ||
next.ServeHTTP(w, r) | ||
}) | ||
} |
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
Oops, something went wrong.