Skip to content

Commit

Permalink
feat: allow selecting local subdomain (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop authored Aug 19, 2024
1 parent a83d934 commit ad79f8c
Show file tree
Hide file tree
Showing 22 changed files with 214 additions and 156 deletions.
8 changes: 8 additions & 0 deletions clienv/clienv.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type CliEnv struct {
nhclient *nhostclient.Client
nhpublicclient *nhostclient.Client
projectName string
localSubdomain string
}

func New(
Expand All @@ -36,6 +37,7 @@ func New(
domain string,
branch string,
projectName string,
localSubdomain string,
) *CliEnv {
return &CliEnv{
stdout: stdout,
Expand All @@ -46,6 +48,7 @@ func New(
nhclient: nil,
nhpublicclient: nil,
projectName: projectName,
localSubdomain: localSubdomain,
}
}

Expand All @@ -69,13 +72,18 @@ func FromCLI(cCtx *cli.Context) *CliEnv {
projectName: sanitizeName(cCtx.String(flagProjectName)),
nhclient: nil,
nhpublicclient: nil,
localSubdomain: cCtx.String(flagLocalSubdomain),
}
}

func (ce *CliEnv) ProjectName() string {
return ce.projectName
}

func (ce *CliEnv) LocalSubdomain() string {
return ce.localSubdomain
}

func (ce *CliEnv) Domain() string {
return ce.domain
}
Expand Down
7 changes: 7 additions & 0 deletions clienv/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
flagDataFolder = "data-folder"
flagNhostFolder = "nhost-folder"
flagDotNhostFolder = "dot-nhost-folder"
flagLocalSubdomain = "local-subdomain"
)

func getGitBranchName() string {
Expand Down Expand Up @@ -98,5 +99,11 @@ func Flags() ([]cli.Flag, error) { //nolint:funlen
Value: filepath.Base(fullWorkingDir),
EnvVars: []string{"NHOST_PROJECT_NAME"},
},
&cli.StringFlag{ //nolint:exhaustruct
Name: flagLocalSubdomain,
Usage: "Local subdomain to reach the development environment",
Value: "local",
EnvVars: []string{"NHOST_LOCAL_SUBDOMAIN"},
},
}, nil
}
1 change: 1 addition & 0 deletions cmd/config/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func TestValidate(t *testing.T) {
"fakedomain",
"fakebranch",
"",
"local",
)

var secrets model.Secrets
Expand Down
1 change: 1 addition & 0 deletions cmd/dev/hasura.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func commandHasura(cCtx *cli.Context) error {
docker := dockercompose.NewDocker()
return docker.HasuraWrapper( //nolint:wrapcheck
cCtx.Context,
ce.LocalSubdomain(),
ce.Path.NhostFolder(),
*cfg.Hasura.Version,
cCtx.Args().Slice()...,
Expand Down
36 changes: 19 additions & 17 deletions cmd/dev/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ func up( //nolint:funlen,cyclop
ce.Infoln("Setting up Nhost development environment...")
composeFile, err := dockercompose.ComposeFileFromConfig(
cfg,
ce.LocalSubdomain(),
ce.ProjectName(),
httpPort,
useTLS,
Expand Down Expand Up @@ -393,7 +394,7 @@ func up( //nolint:funlen,cyclop
"metadata", "export",
"--skip-update-check",
"--log-level", "ERROR",
"--endpoint", dockercompose.URL("hasura", httpPort, useTLS),
"--endpoint", dockercompose.URL(ce.LocalSubdomain(), "hasura", httpPort, useTLS),
"--admin-secret", cfg.Hasura.AdminSecret,
); err != nil {
return fmt.Errorf("failed to create metadata: %w", err)
Expand All @@ -404,11 +405,12 @@ func up( //nolint:funlen,cyclop
}

ce.Infoln("Nhost development environment started.")
printInfo(httpPort, postgresPort, useTLS, runServicesCfg)
printInfo(ce.LocalSubdomain(), httpPort, postgresPort, useTLS, runServicesCfg)
return nil
}

func printInfo(
subdomain string,
httpPort, postgresPort uint,
useTLS bool,
runServices []*dockercompose.RunService,
Expand All @@ -419,20 +421,20 @@ func printInfo(
"- Postgres:\t\tpostgres://postgres:postgres@localhost:%d/local\n",
postgresPort,
)
fmt.Fprintf(w, "- Hasura:\t\t%s\n", dockercompose.URLNewFormat(
"local", "hasura", httpPort, useTLS))
fmt.Fprintf(w, "- GraphQL:\t\t%s\n", dockercompose.URLNewFormat(
"local", "graphql", httpPort, useTLS))
fmt.Fprintf(w, "- Auth:\t\t%s\n", dockercompose.URLNewFormat(
"local", "auth", httpPort, useTLS))
fmt.Fprintf(w, "- Storage:\t\t%s\n", dockercompose.URLNewFormat(
"local", "storage", httpPort, useTLS))
fmt.Fprintf(w, "- Functions:\t\t%s\n", dockercompose.URLNewFormat(
"local", "functions", httpPort, useTLS))
fmt.Fprintf(w, "- Dashboard:\t\t%s\n", dockercompose.URLNewFormat(
"local", "dashboard", httpPort, useTLS))
fmt.Fprintf(w, "- Mailhog:\t\t%s\n", dockercompose.URLNewFormat(
"local", "mailhog", httpPort, useTLS))
fmt.Fprintf(w, "- Hasura:\t\t%s\n", dockercompose.URL(
subdomain, "hasura", httpPort, useTLS))
fmt.Fprintf(w, "- GraphQL:\t\t%s\n", dockercompose.URL(
subdomain, "graphql", httpPort, useTLS))
fmt.Fprintf(w, "- Auth:\t\t%s\n", dockercompose.URL(
subdomain, "auth", httpPort, useTLS))
fmt.Fprintf(w, "- Storage:\t\t%s\n", dockercompose.URL(
subdomain, "storage", httpPort, useTLS))
fmt.Fprintf(w, "- Functions:\t\t%s\n", dockercompose.URL(
subdomain, "functions", httpPort, useTLS))
fmt.Fprintf(w, "- Dashboard:\t\t%s\n", dockercompose.URL(
subdomain, "dashboard", httpPort, useTLS))
fmt.Fprintf(w, "- Mailhog:\t\t%s\n", dockercompose.URL(
subdomain, "mailhog", httpPort, useTLS))

for _, svc := range runServices {
for _, port := range svc.Config.GetPorts() {
Expand All @@ -457,7 +459,7 @@ func printInfo(

fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "SDK Configuration:\n")
fmt.Fprintf(w, " Subdomain:\tlocal\n")
fmt.Fprintf(w, " Subdomain:\t%s\n", subdomain)
fmt.Fprintf(w, " Region:\tlocal\n")
fmt.Fprintf(w, "")
fmt.Fprintf(w, "Run `nhost up` to reload the development environment\n")
Expand Down
1 change: 1 addition & 0 deletions cmd/dockercredentials/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func getToken(ctx context.Context, domain string) (string, error) {
domain,
"unneeded",
"unneeded",
"unneeded",
)
session, err := ce.LoadSession(ctx)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion dockercompose/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

func ai(
cfg *model.ConfigConfig,
subdomain string,
) *Service {
envars := appconfig.AIEnv(
cfg,
Expand Down Expand Up @@ -38,7 +39,7 @@ func ai(
"serve",
},
Environment: env,
ExtraHosts: extraHosts(),
ExtraHosts: extraHosts(subdomain),
Labels: nil,
Ports: nil,
Restart: "always",
Expand Down
14 changes: 7 additions & 7 deletions dockercompose/ai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func expectedAI() *Service {
},
ExtraHosts: []string{
"host.docker.internal:host-gateway",
"local.auth.nhost.run:host-gateway",
"local.db.nhost.run:host-gateway",
"local.functions.nhost.run:host-gateway",
"local.graphql.nhost.run:host-gateway",
"local.hasura.nhost.run:host-gateway",
"local.storage.nhost.run:host-gateway",
"dev.auth.local.nhost.run:host-gateway",
"dev.db.local.nhost.run:host-gateway",
"dev.functions.local.nhost.run:host-gateway",
"dev.graphql.local.nhost.run:host-gateway",
"dev.hasura.local.nhost.run:host-gateway",
"dev.storage.local.nhost.run:host-gateway",
},
HealthCheck: &HealthCheck{
Test: []string{"CMD", "graphite", "healthcheck"},
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestAI(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

got := ai(tc.cfg())
got := ai(tc.cfg(), "dev")
if diff := cmp.Diff(tc.expected(), got); diff != "" {
t.Error(diff)
}
Expand Down
5 changes: 3 additions & 2 deletions dockercompose/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func authPatchPre022(svc Service, useTLS bool) *Service {

func auth( //nolint:funlen
cfg *model.ConfigConfig,
subdomain string,
httpPort uint,
useTLS bool,
nhostFolder string,
Expand All @@ -36,7 +37,7 @@ func auth( //nolint:funlen
envars, err := appconfig.HasuraAuthEnv(
cfg,
"http://graphql:8080/v1/graphql",
URL("auth", httpPort, useTLS)+"/v1",
URL(subdomain, "auth", httpPort, useTLS)+"/v1",
"postgres://nhost_hasura@postgres:5432/local",
"postgres://nhost_auth_admin@postgres:5432/local",
&model.ConfigSmtp{
Expand Down Expand Up @@ -70,7 +71,7 @@ func auth( //nolint:funlen
EntryPoint: nil,
Command: nil,
Environment: env,
ExtraHosts: extraHosts(),
ExtraHosts: extraHosts(subdomain),
HealthCheck: &HealthCheck{
Test: []string{"CMD", "wget", "--spider", "-S", "http://localhost:4000/healthz"},
Timeout: "60s",
Expand Down
15 changes: 9 additions & 6 deletions dockercompose/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func expectedAuth() *Service {
"AUTH_PROVIDER_WORKOS_ENABLED": "true",
"AUTH_REFRESH_TOKEN_EXPIRES_IN": "99",
"AUTH_REQUIRE_ELEVATED_CLAIM": "required",
"AUTH_SERVER_URL": "http://local.auth.nhost.run:1336/v1",
"AUTH_SERVER_URL": "http://dev.auth.local.nhost.run:1336/v1",
"AUTH_SMS_PASSWORDLESS_ENABLED": "true",
"AUTH_SMS_PROVIDER": "twilio",
"AUTH_SMS_TWILIO_ACCOUNT_SID": "smsAccountSid",
Expand Down Expand Up @@ -133,10 +133,13 @@ func expectedAuth() *Service {
"HASURA_GRAPHQL_JWT_SECRET": `{"claims_map":{"x-hasura-allowed-roles":{"path":"$.roles"},"x-hasura-default-role":"viewer","x-hasura-org-id":{"default":"public","path":"$.org"},"x-hasura-user-id":{"path":"$.sub"}},"key":"jwtSecretKey","type":"HS256"}`,
},
ExtraHosts: []string{
"host.docker.internal:host-gateway", "local.auth.nhost.run:host-gateway",
"local.db.nhost.run:host-gateway", "local.functions.nhost.run:host-gateway",
"local.graphql.nhost.run:host-gateway", "local.hasura.nhost.run:host-gateway",
"local.storage.nhost.run:host-gateway",
"host.docker.internal:host-gateway",
"dev.auth.local.nhost.run:host-gateway",
"dev.db.local.nhost.run:host-gateway",
"dev.functions.local.nhost.run:host-gateway",
"dev.graphql.local.nhost.run:host-gateway",
"dev.hasura.local.nhost.run:host-gateway",
"dev.storage.local.nhost.run:host-gateway",
},
HealthCheck: &HealthCheck{
Test: []string{"CMD", "wget", "--spider", "-S", "http://localhost:4000/healthz"},
Expand Down Expand Up @@ -206,7 +209,7 @@ func TestAuth(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

got, err := auth(tc.cfg(), 1336, tc.useTlS, "/tmp/nhost", 0)
got, err := auth(tc.cfg(), "dev", 1336, tc.useTlS, "/tmp/nhost", 0)
if err != nil {
t.Errorf("got error: %v", err)
}
Expand Down
Loading

0 comments on commit ad79f8c

Please sign in to comment.