Skip to content

Commit

Permalink
Add custom domain resolution logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kiootic committed Nov 29, 2023
1 parent 1dc14ed commit e877874
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
7 changes: 5 additions & 2 deletions cmd/controller/app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/oursky/pageship/internal/db"
_ "github.com/oursky/pageship/internal/db/postgres"
_ "github.com/oursky/pageship/internal/db/sqlite"
"github.com/oursky/pageship/internal/domain"
domaindb "github.com/oursky/pageship/internal/domain/db"
"github.com/oursky/pageship/internal/handler/controller"
"github.com/oursky/pageship/internal/handler/site"
"github.com/oursky/pageship/internal/handler/site/middleware"
Expand Down Expand Up @@ -129,7 +129,10 @@ func (s *setup) checkDomain(name string) error {
}

func (s *setup) sites(conf StartSitesConfig) error {
domainResolver := &domain.ResolverNull{} // FIXME: custom domain
domainResolver := &domaindb.Resolver{
HostIDScheme: conf.HostIDScheme,
DB: s.database,
}
siteResolver := &sitedb.Resolver{
HostIDScheme: conf.HostIDScheme,
DB: s.database,
Expand Down
1 change: 1 addition & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type DeploymentsDB interface {
type DomainsDB interface {
CreateDomain(ctx context.Context, domain *models.Domain) error
GetDomainByName(ctx context.Context, domain string) (*models.Domain, error)
GetDomainBySite(ctx context.Context, appID string, siteName string) (*models.Domain, error)
DeleteDomain(ctx context.Context, id string, now time.Time) error
ListDomains(ctx context.Context, appID string) ([]*models.Domain, error)
}
Expand Down
17 changes: 17 additions & 0 deletions internal/db/postgres/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ func (q query[T]) GetDomainByName(ctx context.Context, domainName string) (*mode
return &domain, nil
}

func (q query[T]) GetDomainBySite(ctx context.Context, appID string, siteName string) (*models.Domain, error) {
var domain models.Domain

err := sqlx.GetContext(ctx, q.ext, &domain, `
SELECT d.id, d.created_at, d.updated_at, d.deleted_at, d.domain, d.app_id, d.site_name FROM domain_association d
JOIN app a ON (a.id = d.app_id AND a.deleted_at IS NULL)
WHERE d.app_id = $1 AND d.site_name = $2 AND d.deleted_at IS NULL
`, appID, siteName)
if errors.Is(err, sql.ErrNoRows) {
return nil, models.ErrDomainNotFound
} else if err != nil {
return nil, err
}

return &domain, nil
}

func (q query[T]) DeleteDomain(ctx context.Context, id string, now time.Time) error {
_, err := q.ext.ExecContext(ctx, `
UPDATE domain_association SET deleted_at = $1 WHERE id = $2
Expand Down
17 changes: 17 additions & 0 deletions internal/db/sqlite/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ func (q query[T]) GetDomainByName(ctx context.Context, domainName string) (*mode
return &domain, nil
}

func (q query[T]) GetDomainBySite(ctx context.Context, appID string, siteName string) (*models.Domain, error) {
var domain models.Domain

err := sqlx.GetContext(ctx, q.ext, &domain, `
SELECT d.id, d.created_at, d.updated_at, d.deleted_at, d.domain, d.app_id, d.site_name FROM domain_association d
JOIN app a ON (a.id = d.app_id AND a.deleted_at IS NULL)
WHERE d.app_id = ? AND d.site_name = ? AND d.deleted_at IS NULL
`, appID, siteName)
if errors.Is(err, sql.ErrNoRows) {
return nil, models.ErrDomainNotFound
} else if err != nil {
return nil, err
}

return &domain, nil
}

func (q query[T]) DeleteDomain(ctx context.Context, id string, now time.Time) error {
_, err := q.ext.ExecContext(ctx, `
UPDATE domain_association SET deleted_at = ? WHERE id = ?
Expand Down
38 changes: 38 additions & 0 deletions internal/domain/db/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package db

import (
"context"
"errors"

"github.com/oursky/pageship/internal/config"
"github.com/oursky/pageship/internal/db"
"github.com/oursky/pageship/internal/domain"
"github.com/oursky/pageship/internal/models"
)

type Resolver struct {
DB db.DB
HostIDScheme config.HostIDScheme
}

func (r *Resolver) Kind() string { return "database" }

func (r *Resolver) Resolve(ctx context.Context, hostname string) (string, error) {
dom, err := r.DB.GetDomainByName(ctx, hostname)
if errors.Is(err, models.ErrDomainNotFound) {
return "", domain.ErrDomainNotFound
}

app, err := r.DB.GetApp(ctx, dom.AppID)
if errors.Is(err, models.ErrAppNotFound) {
return "", domain.ErrDomainNotFound
}

sub := dom.SiteName
if dom.SiteName == app.Config.DefaultSite {
sub = ""
}
id := r.HostIDScheme.Make(dom.AppID, sub)

return id, nil
}
14 changes: 13 additions & 1 deletion internal/site/db/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,23 @@ func (r *Resolver) Resolve(ctx context.Context, matchedID string) (*site.Descrip
config.Access = app.Config.Deployments.Access
}

domain, err := r.DB.GetDomainBySite(ctx, app.ID, siteName)
if errors.Is(err, models.ErrDomainNotFound) {
domain = nil
} else if err != nil {
return nil, err
}

domainName := ""
if domain != nil {
domainName = domain.Domain
}

id := strings.Join([]string{deployment.AppID, siteName, deployment.ID}, "/")
desc := &site.Descriptor{
ID: id,
Config: &config,
Domain: "", // FIXME: custom domain
Domain: domainName,
FS: newStorageFS(r.Storage, deployment),
}

Expand Down

0 comments on commit e877874

Please sign in to comment.