Skip to content

Commit

Permalink
feat: add support for proxying additional http/https ports
Browse files Browse the repository at this point in the history
  • Loading branch information
janosmiko committed Mar 17, 2022
1 parent 011e582 commit a0e3206
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 34 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.2.28-beta
v0.2.29-beta
36 changes: 36 additions & 0 deletions docs/configuration/additional-port.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Open additional shared http/https port(s)

It's possible to configure additional shared HTTP/s port(s) and proxy the requests to the PHP containers.

First, configure Traefik. Add the port to the Reward Configuration (default: `~/.reward.yml`).

```yaml
reward_traefik_bind_additional_http_ports: [ 8080 ]
reward_traefik_bind_additional_https_ports: [ 8443, 9998 ]
```
When it's done, restart Traefik.
```shell
reward svc down
reward svc up
```

Open [Traefik dashboard](https://traefik.reward.test) to check if the new port(s) exist in the entrypoints section.

![Traefik Additional HTTPS Port](screenshots/traefik-additional-port.png)

When it's done configure this additional port on the PHP container as well. Add the following line to the
project's `.env` file. (It's also possible to add multiple ports, separated by comma.)

```
REWARD_HTTP_PROXY_PORTS=8080
REWARD_HTTPS_PROXY_PORTS=8443,9998
```

And restart the environment.

```shell
reward env down
reward env up
```
49 changes: 47 additions & 2 deletions docs/configuration/livereload.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## LiveReload Setup

LiveReload routing is currently supported only on the `magento2` environment type. Other environment types may utilize
LiveReload via per-project compose configurations to set up the routing for LiveReload JS and WebSocket endpoints.
LiveReload routing is currently supported on the `magento2`, `pwa-studio` and `shopware` environment types. Other
environment types may utilize LiveReload via per-project compose configurations to set up the routing for LiveReload JS
and WebSocket endpoints.

### Configuration for Magento 2

Expand Down Expand Up @@ -75,3 +76,47 @@ On a working setup with `grunt watch` running within `reward shell` you should s
network inspector after reloading the project in a web browser.

![LiveReload Network Requests](screenshots/livereload.png)

### Configuration for Shopware

Shopware Hot Reload functionality requires an additional port on the same hostname.

To configure the additional port, you will have to configure Traefik.

Open Reward Global Configuration (default: `~/.reward.yml`) and add the following line:

```yaml
reward_traefik_bind_additional_https_ports: [ 9998 ]
```
When it's done, restart Traefik.
```shell
reward svc down
reward svc up
```

If you open [Traefik dashboard](https://traefik.reward.test), you should see the new port in the entrypoints section.

![Traefik Additional HTTPS Port](screenshots/traefik-additional-port.png)

When it's done you have to configure this additional port on the PHP container as well. Add the following line to the
project's `.env` file:

```
REWARD_HTTPS_PROXY_PORTS=9998
```

And restart the environment.

```shell
reward env down
reward env up
```

Now if you start the Live Reload server, the requests will be proxied to it.

```
reward shell
./psh.phar storefront:hot-proxy
```
19 changes: 0 additions & 19 deletions docs/configuration/mercure.md

This file was deleted.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions internal/commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ debug: false
# These services are disabled by default.
#reward_adminer: 1
# You can configure Traefik to bind additional http ports on top of the default port (80).
# reward_traefik_bind_additional_http_ports: [8080]
reward_traefik_bind_additional_http_ports: []
# You can configure Traefik to bind additional https ports on top of the default port (443).
# reward_traefik_bind_additional_https_ports: [8443,9443]
reward_traefik_bind_additional_https_ports: []
# By default Reward makes it possible to resolve the environment's domain to the nginx container's IP address
# inside the docker network. To disable this behaviour you can uncomment the following line.
#reward_resolve_domain_to_traefik: 0
Expand Down
39 changes: 33 additions & 6 deletions internal/commands/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func SvcCmd(args []string) error {
}
}

if !core.CheckFileExists(filepath.Join(core.GetAppHomeDir(), "etc/traefik/traefik.yml")) {
err := SvcGenerateTraefikConfig()
if err != nil {
return err
}
// if !core.CheckFileExists(filepath.Join(core.GetAppHomeDir(), "etc/traefik/traefik.yml")) {
err := SvcGenerateTraefikConfig()
if err != nil {
return err
}
// }

err := SvcGenerateTraefikDynamicConfig()
err = SvcGenerateTraefikDynamicConfig()
if err != nil {
return err
}
Expand All @@ -70,6 +70,33 @@ func SvcCmd(args []string) error {
args = newArgs
}

if core.ContainsString(args, "restart") {
sslDir := filepath.Join(core.GetAppHomeDir(), core.SslBaseDir)

serviceDomain := core.GetServiceDomain()

log.Debugln("Service Domain:", serviceDomain)

if !core.CheckFileExists(filepath.Join(sslDir, "certs", serviceDomain+".crt.pem")) {
err := SignCertificateCmd([]string{serviceDomain})
if err != nil {
return err
}
}

// if !core.CheckFileExists(filepath.Join(core.GetAppHomeDir(), "etc/traefik/traefik.yml")) {
err := SvcGenerateTraefikConfig()
if err != nil {
return err
}
// }

err = SvcGenerateTraefikDynamicConfig()
if err != nil {
return err
}
}

// pass orchestration through to docker-compose
err := SvcRunDockerCompose(args, false)
if err != nil {
Expand Down
25 changes: 20 additions & 5 deletions internal/core/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ func AppendTemplatesFromPaths(t *template.Template, templateList *list.List, pat
continue
}

child, err := template.New(templatePath).Funcs(sprig.TxtFuncMap()).Funcs(customTemplateFuncs).ParseFiles(filePath)
child, err := template.New(templatePath).
Funcs(sprig.TxtFuncMap()).
Funcs(customTemplateFuncs).
ParseFiles(filePath)
if err != nil {
return err
}
Expand All @@ -77,7 +80,10 @@ func AppendTemplatesFromPaths(t *template.Template, templateList *list.List, pat
continue
}

child, err := template.New(templatePath).Funcs(sprig.TxtFuncMap()).Funcs(customTemplateFuncs).ParseFiles(filePath)
child, err := template.New(templatePath).
Funcs(sprig.TxtFuncMap()).
Funcs(customTemplateFuncs).
ParseFiles(filePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,7 +123,11 @@ func AppendTemplatesFromPathsStatic(t *template.Template, templateList *list.Lis
log.Traceln(err)
} else {
log.Traceln("creating template:", templatePath)
child, err := template.New(templatePath).Funcs(sprig.TxtFuncMap()).Funcs(customTemplateFuncs).Parse(string(content))

child, err := template.New(templatePath).
Funcs(sprig.TxtFuncMap()).
Funcs(customTemplateFuncs).
Parse(string(content))
if err != nil {
return err
}
Expand Down Expand Up @@ -198,7 +208,10 @@ func AppendMutagenTemplates(t *template.Template, templateList *list.List, parti
} else {
log.Traceln("appending template:", v)

child, err := template.New(v).Funcs(sprig.TxtFuncMap()).Funcs(customTemplateFuncs).Parse(string(content))
child, err := template.New(v).
Funcs(sprig.TxtFuncMap()).
Funcs(customTemplateFuncs).
Parse(string(content))
if err != nil {
return err
}
Expand All @@ -219,7 +232,9 @@ func ExecuteTemplate(t *template.Template, buffer io.Writer) error {
log.Traceln(viper.AllSettings())
log.Traceln(t.DefinedTemplates())

err := t.Funcs(sprig.TxtFuncMap()).Funcs(customTemplateFuncs).ExecuteTemplate(buffer, t.Name(), viper.AllSettings())
err := t.Funcs(sprig.TxtFuncMap()).
Funcs(customTemplateFuncs).
ExecuteTemplate(buffer, t.Name(), viper.AllSettings())

return err
}
Expand Down
12 changes: 12 additions & 0 deletions templates/_services/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{{- /* @formatter:off */ -}}

{{- $traefik_listen := .traefik_listen -}}

version: "3.5"
services:
traefik:
Expand All @@ -8,6 +10,16 @@ services:
ports:
- "{{ default "127.0.0.1" .traefik_listen }}:80:80" # The HTTP port
- "{{ default "127.0.0.1" .traefik_listen }}:443:443" # The HTTPS port
{{- if .reward_traefik_bind_additional_http_ports -}}
{{- range $i, $v := .reward_traefik_bind_additional_http_ports }}
{{- printf `- "%s:%d:%d"` (default "127.0.0.1" $traefik_listen) $v $v | nindent 6 -}}
{{- end -}}
{{- end -}}
{{- if .reward_traefik_bind_additional_https_ports -}}
{{- range $i, $v := .reward_traefik_bind_additional_https_ports }}
{{- printf `- "%s:%d:%d"` (default "127.0.0.1" $traefik_listen) $v $v | nindent 6 -}}
{{- end -}}
{{- end }}
volumes:
- ./etc/traefik/traefik.yml:/etc/traefik/traefik.yml
- ./etc/traefik/dynamic.yml:/etc/traefik/dynamic.yml
Expand Down
12 changes: 12 additions & 0 deletions templates/_traefik/traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ entryPoints:
scheme: https
https:
address: ":443"
{{- if .reward_traefik_bind_additional_http_ports -}}
{{- range $i, $v := .reward_traefik_bind_additional_http_ports }}
{{- printf "http-additional-%d:" $v | nindent 2 -}}
{{- printf `address: ":%d"` $v | nindent 4 -}}
{{- end -}}
{{- end -}}
{{- if .reward_traefik_bind_additional_https_ports -}}
{{- range $i, $v := .reward_traefik_bind_additional_https_ports }}
{{- printf "https-additional-%d:" $v | nindent 2 -}}
{{- printf `address: ":%d"` $v | nindent 4 -}}
{{- end -}}
{{- end }}
log:
level: info
global:
Expand Down
2 changes: 1 addition & 1 deletion templates/environments/includes/mercure.base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ services:
{{ end }}
labels:
- traefik.enable=true
- traefik.http.routers.{{ .reward_env_name }}-mercure.rule=Host(`{{ .traefik_domain }}`) && PathPrefix(`/.well-known/mercure`)
- traefik.http.routers.{{ .reward_env_name }}-mercure.tls=true
- traefik.http.routers.{{ .reward_env_name }}-mercure.rule=Host(`{{ .traefik_domain }}`) && PathPrefix(`/.well-known/mercure`)
- traefik.http.services.{{ .reward_env_name }}-mercure.loadbalancer.server.port=80
- dev.reward.container.name=mercure
- dev.reward.environment.name={{ .reward_env_name }}
Expand Down
62 changes: 62 additions & 0 deletions templates/environments/includes/php-fpm.base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ services:
{{- if .xdebug_version -}}
{{- $xdebug_image_tag = (printf "-xdebug%s" .xdebug_version) -}}
{{- end }}
{{ $reward_env_name := .reward_env_name }}

php-fpm:
hostname: "{{ .reward_env_name }}-php-fpm"
Expand All @@ -63,10 +64,71 @@ services:
- traefik.http.services.{{ .reward_env_name }}-php-fpm.loadbalancer.server.port=80
- dev.reward.container.name=php-fpm
- dev.reward.environment.name={{ .reward_env_name }}
{{ if .reward_http_proxy_ports }}
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-http.tls=false
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-http.entrypoints={{ printf "http-additional-%v" (join ",http-additional-" (splitList "," .reward_http_proxy_ports)) }}
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-http.priority=3
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-http.rule=
HostRegexp(`{subdomain:.+}.{{ .traefik_domain }}`) || Host(`{{ .traefik_domain }}`)
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-http.service={{ .reward_env_name }}-php-fpm-extra-http
{{ range $i, $v := (splitList "," .reward_http_proxy_ports) }}
- traefik.http.services.{{ $reward_env_name }}-php-fpm-extra-http.loadbalancer.server.port={{ $v }}
{{ end }}
{{ end }}
{{ if .reward_https_proxy_ports }}
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-https.tls=true
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-https.entrypoints={{ printf "https-additional-%v" (join ",https-additional-" (splitList "," .reward_https_proxy_ports)) }}
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-https.priority=3
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-https.rule=
HostRegexp(`{subdomain:.+}.{{ .traefik_domain }}`) || Host(`{{ .traefik_domain }}`)
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-https.service={{ .reward_env_name }}-php-fpm-extra-https
{{ range $i, $v := (splitList "," .reward_https_proxy_ports) }}
- traefik.http.services.{{ $reward_env_name }}-php-fpm-extra-https.loadbalancer.server.port={{ $v }}
{{ end }}
{{ end }}
{{ else }}
labels:
- dev.reward.container.name=php-fpm
- dev.reward.environment.name={{ .reward_env_name }}
{{ if ( or .reward_http_proxy_ports .reward_https_proxy_ports ) }}
- traefik.enable=true
{{ end }}

{{ if .reward_http_proxy_ports }}
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-http.tls=false
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-http.entrypoints={{ printf "http-additional-%v" (join ",http-additional-" (splitList "," .reward_http_proxy_ports)) }}
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-http.priority=3
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-http.rule=
( HostRegexp(`{subdomain:.+}.{{ .traefik_domain }}`) ) || ( Host(`{{ .traefik_domain }}`) )
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-http.service={{ .reward_env_name }}-php-fpm-extra-http
{{ range $i, $v := (splitList "," .reward_http_proxy_ports) }}
- traefik.http.services.{{ $reward_env_name }}-php-fpm-extra-http.loadbalancer.server.port={{ $v }}
{{ end }}
{{ end }}
{{ if .reward_https_proxy_ports }}
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-https.tls=true
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-https.entrypoints={{ printf "https-additional-%v" (join ",https-additional-" (splitList "," .reward_https_proxy_ports)) }}
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-https.priority=3
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-https.rule=
( HostRegexp(`{subdomain:.+}.{{ .traefik_domain }}`) ) || ( Host(`{{ .traefik_domain }}`) )
- traefik.http.routers.{{ .reward_env_name }}-php-fpm-extra-https.service={{ .reward_env_name }}-php-fpm-extra-https
{{ range $i, $v := (splitList "," .reward_https_proxy_ports) }}
- traefik.http.services.{{ $reward_env_name }}-php-fpm-extra-https.loadbalancer.server.port={{ $v }}
{{ end }}
{{ end }}
{{ end }}
{{ if ( or .reward_http_proxy_ports .reward_https_proxy_ports ) }}
ports:
{{ if .reward_http_proxy_ports }}
{{ range $i, $v := (splitList "," .reward_http_proxy_ports) }}
- {{ $v }}
{{ end }}
{{ end }}
{{ if .reward_https_proxy_ports }}
{{ range $i, $v := (splitList "," .reward_https_proxy_ports) }}
- {{ $v }}
{{ end }}
{{ end }}
{{ end }}
volumes: *volumes
extra_hosts: *extra_hosts
Expand Down

0 comments on commit a0e3206

Please sign in to comment.