Skip to content

Commit 5e02399

Browse files
authored
Merge pull request awslabs#21 from awslabs/devel
Custom host
2 parents e7766d8 + b7d1f06 commit 5e02399

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

core/request.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ import (
1111
"log"
1212
"net/http"
1313
"net/url"
14+
"os"
1415
"strings"
1516

1617
"github.com/aws/aws-lambda-go/events"
1718
)
1819

20+
// CustomHostVariable is the name of the environment variable that contains
21+
// the custom hostname for the request. If this variable is not set the framework
22+
// reverts to `DefaultServerAddress`. The value for a custom host should include
23+
// a protocol: http://my-custom.host.com
24+
const CustomHostVariable = "GO_API_HOST"
25+
1926
// DefaultServerAddress is prepended to the path of each incoming reuqest
2027
const DefaultServerAddress = "https://aws-serverless-go-api.com"
2128

@@ -128,13 +135,17 @@ func (r *RequestAccessor) ProxyEventToHTTPRequest(req events.APIGatewayProxyRequ
128135
if r.stripBasePath != "" && len(r.stripBasePath) > 1 {
129136
if strings.HasPrefix(path, r.stripBasePath) {
130137
path = strings.Replace(path, r.stripBasePath, "", 1)
131-
if !strings.HasPrefix(path, "/") {
132-
path = "/" + path
133-
}
134138
}
135139
}
140+
if !strings.HasPrefix(path, "/") {
141+
path = "/" + path
142+
}
143+
serverAddress := DefaultServerAddress
144+
if customAddress, ok := os.LookupEnv(CustomHostVariable); ok {
145+
serverAddress = customAddress
146+
}
136147

137-
path = DefaultServerAddress + path
148+
path = serverAddress + path
138149

139150
httpRequest, err := http.NewRequest(
140151
strings.ToUpper(req.HTTPMethod),

core/request_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/base64"
55
"io/ioutil"
66
"math/rand"
7+
"os"
78

89
"github.com/aws/aws-lambda-go/events"
910
"github.com/awslabs/aws-lambda-go-api-proxy/core"
@@ -149,6 +150,42 @@ var _ = Describe("RequestAccessor tests", func() {
149150
Expect("value1").To(Equal(stageVars["var1"]))
150151
Expect("value2").To(Equal(stageVars["var2"]))
151152
})
153+
154+
It("Populates the default hostname correctly", func() {
155+
basicRequest := getProxyRequest("orders", "GET")
156+
accessor := core.RequestAccessor{}
157+
httpReq, err := accessor.ProxyEventToHTTPRequest(basicRequest)
158+
Expect(err).To(BeNil())
159+
160+
Expect(core.DefaultServerAddress).To(Equal("https://" + httpReq.Host))
161+
Expect(core.DefaultServerAddress).To(Equal("https://" + httpReq.URL.Host))
162+
})
163+
164+
It("Uses a custom hostname", func() {
165+
myCustomHost := "http://my-custom-host.com"
166+
os.Setenv(core.CustomHostVariable, myCustomHost)
167+
basicRequest := getProxyRequest("orders", "GET")
168+
accessor := core.RequestAccessor{}
169+
httpReq, err := accessor.ProxyEventToHTTPRequest(basicRequest)
170+
Expect(err).To(BeNil())
171+
172+
Expect(myCustomHost).To(Equal("http://" + httpReq.Host))
173+
Expect(myCustomHost).To(Equal("http://" + httpReq.URL.Host))
174+
os.Unsetenv(core.CustomHostVariable)
175+
})
176+
177+
It("Strips terminating / from hostname", func() {
178+
myCustomHost := "http://my-custom-host.com"
179+
os.Setenv(core.CustomHostVariable, myCustomHost+"/")
180+
basicRequest := getProxyRequest("orders", "GET")
181+
accessor := core.RequestAccessor{}
182+
httpReq, err := accessor.ProxyEventToHTTPRequest(basicRequest)
183+
Expect(err).To(BeNil())
184+
185+
Expect(myCustomHost).To(Equal("http://" + httpReq.Host))
186+
Expect(myCustomHost).To(Equal("http://" + httpReq.URL.Host))
187+
os.Unsetenv(core.CustomHostVariable)
188+
})
152189
})
153190
})
154191

core/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func GatewayTimeout() events.APIGatewayProxyResponse {
1212
return events.APIGatewayProxyResponse{StatusCode: http.StatusGatewayTimeout}
1313
}
1414

15-
// New Logged Error
15+
// NewLoggedError generates a new error and logs it to stdout
1616
func NewLoggedError(format string, a ...interface{}) error {
1717
err := fmt.Errorf(format, a...)
1818
fmt.Println(err.Error())

gorillamux/adapter_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gorillamux_test
22

33
import (
44
"fmt"
5-
"log"
65
"net/http"
76

87
"github.com/aws/aws-lambda-go/events"
@@ -16,8 +15,6 @@ import (
1615
var _ = Describe("GorillaMuxAdapter tests", func() {
1716
Context("Simple ping request", func() {
1817
It("Proxies the event correctly", func() {
19-
log.Println("Starting test")
20-
2118
homeHandler := func(w http.ResponseWriter, req *http.Request) {
2219
w.Header().Add("unfortunately-required-header", "")
2320
fmt.Fprintf(w, "Home Page")

0 commit comments

Comments
 (0)