Skip to content

Commit

Permalink
Merge pull request #2169 from superfly/feat/service-httpoptions
Browse files Browse the repository at this point in the history
Port services.ports.http_options to v2 apps
  • Loading branch information
dangra committed Apr 24, 2023
2 parents 2cacfc1 + 8f2a8bf commit 82e5a6a
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 19 deletions.
24 changes: 17 additions & 7 deletions api/machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,13 @@ type MachineCheckStatus struct {
}

type MachinePort struct {
Port *int `json:"port,omitempty" toml:"port,omitempty"`
StartPort *int `json:"start_port,omitempty" toml:"start_port,omitempty"`
EndPort *int `json:"end_port,omitempty" toml:"end_port,omitempty"`
Handlers []string `json:"handlers,omitempty" toml:"handlers,omitempty"`
ForceHttps bool `json:"force_https,omitempty" toml:"force_https,omitempty"`
TlsOptions *TlsOptions `json:"tls_options,omitempty" toml:"tls_options,omitempty"`
Port *int `json:"port,omitempty" toml:"port,omitempty"`
StartPort *int `json:"start_port,omitempty" toml:"start_port,omitempty"`
EndPort *int `json:"end_port,omitempty" toml:"end_port,omitempty"`
Handlers []string `json:"handlers,omitempty" toml:"handlers,omitempty"`
ForceHTTPS bool `json:"force_https,omitempty" toml:"force_https,omitempty"`
TLSOptions *TLSOptions `json:"tls_options,omitempty" toml:"tls_options,omitempty"`
HTTPOptions *HTTPOptions `json:"http_options,omitempty" toml:"tls_options,omitempty"`
}

func (mp *MachinePort) ContainsPort(port int) bool {
Expand Down Expand Up @@ -412,11 +413,20 @@ func (mp *MachinePort) HasNonHttpPorts() bool {
return false
}

type TlsOptions struct {
type TLSOptions struct {
Alpn []string `json:"alpn,omitempty" toml:"alpn,omitempty"`
Versions []string `json:"versions,omitempty" toml:"version,omitempty"`
}

type HTTPOptions struct {
Compress *bool `json:"compress,omitempty" toml:"compress,omitempty"`
Response *HTTPResponseOptions `json:"response,omitempty" toml:"response,omitempty"`
}

type HTTPResponseOptions struct {
Headers map[string]any `json:"headers,omitempty" toml:"headers,omitempty"`
}

type MachineService struct {
Protocol string `json:"protocol,omitempty" toml:"protocol,omitempty"`
InternalPort int `json:"internal_port,omitempty" toml:"internal_port,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions internal/appconfig/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestFromDefinition(t *testing.T) {
{
Port: api.Pointer(80),
Handlers: []string{"http"},
ForceHttps: true,
ForceHTTPS: true,
},
{
Port: api.Pointer(443),
Expand All @@ -108,7 +108,7 @@ func TestFromDefinition(t *testing.T) {
},
},
},
configFilePath: "--config path unset--",
configFilePath: "--config path unset--",
defaultGroupName: "app",
RawDefinition: map[string]any{
"env": map[string]any{},
Expand Down
16 changes: 8 additions & 8 deletions internal/appconfig/machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func TestToMachineConfig(t *testing.T) {
Protocol: "tcp",
InternalPort: 8080,
Ports: []api.MachinePort{
{Port: api.Pointer(80), Handlers: []string{"http"}, ForceHttps: true},
{Port: api.Pointer(443), Handlers: []string{"http", "tls"}, ForceHttps: false},
{Port: api.Pointer(80), Handlers: []string{"http"}, ForceHTTPS: true},
{Port: api.Pointer(443), Handlers: []string{"http", "tls"}, ForceHTTPS: false},
},
},
},
Expand Down Expand Up @@ -79,8 +79,8 @@ func TestToMachineConfig_nullifyManagedFields(t *testing.T) {
Protocol: "tcp",
InternalPort: 8080,
Ports: []api.MachinePort{
{Port: api.Pointer(80), Handlers: []string{"http"}, ForceHttps: true},
{Port: api.Pointer(443), Handlers: []string{"http", "tls"}, ForceHttps: false},
{Port: api.Pointer(80), Handlers: []string{"http"}, ForceHTTPS: true},
{Port: api.Pointer(443), Handlers: []string{"http", "tls"}, ForceHTTPS: false},
},
},
},
Expand Down Expand Up @@ -207,8 +207,8 @@ func TestToMachineConfig_defaultV2flytoml(t *testing.T) {
Protocol: "tcp",
InternalPort: 8080,
Ports: []api.MachinePort{
{Port: api.Pointer(80), Handlers: []string{"http"}, ForceHttps: true},
{Port: api.Pointer(443), Handlers: []string{"http", "tls"}, ForceHttps: false},
{Port: api.Pointer(80), Handlers: []string{"http"}, ForceHTTPS: true},
{Port: api.Pointer(443), Handlers: []string{"http", "tls"}, ForceHTTPS: false},
},
},
},
Expand Down Expand Up @@ -267,8 +267,8 @@ func TestToMachineConfig_services(t *testing.T) {
Autostart: api.Pointer(true),
Autostop: api.Pointer(true),
Ports: []api.MachinePort{
{Port: api.Pointer(80), Handlers: []string{"http"}, ForceHttps: true},
{Port: api.Pointer(443), Handlers: []string{"http", "tls"}, ForceHttps: false},
{Port: api.Pointer(80), Handlers: []string{"http"}, ForceHTTPS: true},
{Port: api.Pointer(443), Handlers: []string{"http", "tls"}, ForceHTTPS: false},
},
},
{
Expand Down
32 changes: 31 additions & 1 deletion internal/appconfig/serde_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ func TestLoadTOMLAppConfigWithEmptyService(t *testing.T) {
assert.Nil(t, p.Services)
}

func TestLoadTOMLAppConfigServicePorts(t *testing.T) {
const path = "./testdata/services-ports.toml"

p, err := LoadConfig(path)
require.NoError(t, err)
want := []Service{{
Protocol: "tcp",
InternalPort: 8080,
Ports: []api.MachinePort{{
Port: api.Pointer(80),
TLSOptions: &api.TLSOptions{
Alpn: []string{"h2", "http/1.1"},
Versions: []string{"TLSv1.2", "TLSv1.3"},
},
HTTPOptions: &api.HTTPOptions{
Compress: api.Pointer(true),
Response: &api.HTTPResponseOptions{
Headers: map[string]any{
"fly-request-id": false,
"fly-wasnt-here": "yes, it was",
"multi-valued": []any{"value1", "value2"},
},
},
},
}},
}}

assert.Equal(t, want, p.Services)
}

func TestLoadTOMLAppConfigInvalidV2(t *testing.T) {
const path = "./testdata/always-invalid-v2.toml"
cfg, err := LoadConfig(path)
Expand Down Expand Up @@ -404,7 +434,7 @@ func TestLoadTOMLAppConfigReferenceFormat(t *testing.T) {
StartPort: api.Pointer(100),
EndPort: api.Pointer(200),
Handlers: []string{"https"},
ForceHttps: true,
ForceHTTPS: true,
},
},

Expand Down
2 changes: 1 addition & 1 deletion internal/appconfig/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *HTTPService) ToService() *Service {
Ports: []api.MachinePort{{
Port: api.IntPointer(80),
Handlers: []string{"http"},
ForceHttps: s.ForceHTTPS,
ForceHTTPS: s.ForceHTTPS,
}, {
Port: api.IntPointer(443),
Handlers: []string{"http", "tls"},
Expand Down
21 changes: 21 additions & 0 deletions internal/appconfig/testdata/services-ports.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
app = "foo"

[[services]]
internal_port = 8080
protocol = "tcp"

[[services.ports]]
port = 80

[services.ports.tls_options]
alpn = ["h2", "http/1.1"]
versions = ["TLSv1.2", "TLSv1.3"]

# https://community.fly.io/t/new-feature-basic-http-response-header-modification/3594
[services.ports.http_options]
compress = true

[services.ports.http_options.response.headers]
fly-request-id = false
fly-wasnt-here = "yes, it was"
multi-valued = ["value1", "value2"]

0 comments on commit 82e5a6a

Please sign in to comment.