Skip to content

Commit

Permalink
fix: domain
Browse files Browse the repository at this point in the history
  • Loading branch information
yuaanlin committed Oct 24, 2024
1 parent 28f71b7 commit 3fe45aa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
7 changes: 6 additions & 1 deletion internal/cmd/domain/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,17 @@ func runCreateDomainInteractive(f *cmdutil.Factory, opts *Options) error {
opts.domainName = domainInput
}

project, err := f.ApiClient.GetProject(context.Background(), zctx.GetProject().GetID(), "", "")
if err != nil {
return err
}

s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
spinner.WithColor(cmdutil.SpinnerColor),
spinner.WithSuffix(" Checking domain availability ..."),
)
s.Start()
available, _, err := f.ApiClient.CheckDomainAvailable(context.Background(), opts.domainName, opts.IsGenerated)
available, _, err := f.ApiClient.CheckDomainAvailable(context.Background(), opts.domainName, opts.IsGenerated, project.Region.ID)
if err != nil {
return err
}
Expand Down
22 changes: 17 additions & 5 deletions internal/cmd/template/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,46 @@ func runDeploy(f *cmdutil.Factory, opts *Options) error {
return fmt.Errorf("parse yaml failed: %w", err)
}

project, err := f.ApiClient.GetProject(context.Background(), opts.projectID, "", "")
if err != nil {
return fmt.Errorf("get project info failed: %w", err)
}

vars := model.Map{}
for _, v := range raw.Spec.Variables {
switch v.Type {
case "DOMAIN":
// only flex shared cluster (hkg1, sfo1, hnd1 ...) support generated domain
// Notice: flex shared cluster in China mainland (sha1) does not support generated domain
if len(project.Region.ID) != 4 || project.Region.ID == "sha1" {
f.Log.Warnf("Selected region does not support generated domain, please bind a custom domain after template deployed.\n")
continue
}

for {
val, err := f.Prompter.InputWithHelp(v.Description, "For example, if you enter \"myapp\", the domain will be \"myapp.zeabur.app\"", "")
val, err := f.Prompter.InputWithHelp(v.Description, "For example, if you enter \"myapp\", the domain will be \"myapp."+project.Region.ID+".zeabur.app\"", "")
if err != nil {
return err
}

s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
spinner.WithColor(cmdutil.SpinnerColor),
spinner.WithSuffix(" Checking if domain "+val+".zeabur.app is available ..."),
spinner.WithSuffix(" Checking if domain "+val+"."+project.Region.ID+".zeabur.app is available ..."),
)

s.Start()
available, _, err := f.ApiClient.CheckDomainAvailable(context.Background(), val, true)
available, _, err := f.ApiClient.CheckDomainAvailable(context.Background(), val, true, project.Region.ID)
if err != nil {
return err
}
s.Stop()

if !available {
f.Log.Warnf("Domain %s.zeabur.app is not available, please try another one.\n", val)
f.Log.Warnf("Domain %s.%s.zeabur.app is not available, please try another one.\n", val, project.Region.ID)
continue
}

f.Log.Infof("Domain %s.zeabur.app is available!\n", val)
f.Log.Infof("Domain %s.%s.zeabur.app is available!\n", val, project.Region.ID)
vars[v.Key] = val
break
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *client) RemoveDomain(ctx context.Context, domain string) (bool, error)
return mutation.RemoveDomain, nil
}

func (c *client) CheckDomainAvailable(ctx context.Context, domain string, isGenerated bool) (bool, string, error) {
func (c *client) CheckDomainAvailable(ctx context.Context, domain string, isGenerated bool, region string) (bool, string, error) {
var mutation struct {
CheckDomainAvailable struct {
IsAvailable bool
Expand All @@ -97,6 +97,7 @@ func (c *client) CheckDomainAvailable(ctx context.Context, domain string, isGene
err := c.Mutate(ctx, &mutation, V{
"domain": domain,
"isGenerated": isGenerated,
"region": region,
})

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type (
AddDomain(ctx context.Context, serviceID string, environmentID string, isGenerated bool, domain string, options ...string) (*string, error)
ListDomains(ctx context.Context, serviceID string, environmentID string) (model.Domains, error)
RemoveDomain(ctx context.Context, domain string) (bool, error)
CheckDomainAvailable(ctx context.Context, domain string, isGenerated bool) (bool, string, error)
CheckDomainAvailable(ctx context.Context, domain string, isGenerated bool, region string) (bool, string, error)
}

DeploymentAPI interface {
Expand Down
1 change: 1 addition & 0 deletions pkg/model/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Project struct {
//Collaborators []User `graphql:"collaborators"`
IconURL string `graphql:"iconURL"`
//Services []Service `graphql:"services"`
Region Region `graphql:"region"`
}

// ProjectConnection is a connection to a list of items.
Expand Down

0 comments on commit 3fe45aa

Please sign in to comment.