Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Custom host #21

Merged
merged 1 commit into from
Dec 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions core/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ import (
"log"
"net/http"
"net/url"
"os"
"strings"

"github.com/aws/aws-lambda-go/events"
)

// CustomHostVariable is the name of the environment variable that contains
// the custom hostname for the request. If this variable is not set the framework
// reverts to `DefaultServerAddress`. The value for a custom host should include
// a protocol: http://my-custom.host.com
const CustomHostVariable = "GO_API_HOST"

// DefaultServerAddress is prepended to the path of each incoming reuqest
const DefaultServerAddress = "https://aws-serverless-go-api.com"

Expand Down Expand Up @@ -128,13 +135,17 @@ func (r *RequestAccessor) ProxyEventToHTTPRequest(req events.APIGatewayProxyRequ
if r.stripBasePath != "" && len(r.stripBasePath) > 1 {
if strings.HasPrefix(path, r.stripBasePath) {
path = strings.Replace(path, r.stripBasePath, "", 1)
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
}
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
serverAddress := DefaultServerAddress
if customAddress, ok := os.LookupEnv(CustomHostVariable); ok {
serverAddress = customAddress
}

path = DefaultServerAddress + path
path = serverAddress + path

httpRequest, err := http.NewRequest(
strings.ToUpper(req.HTTPMethod),
Expand Down
37 changes: 37 additions & 0 deletions core/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"io/ioutil"
"math/rand"
"os"

"github.com/aws/aws-lambda-go/events"
"github.com/awslabs/aws-lambda-go-api-proxy/core"
Expand Down Expand Up @@ -149,6 +150,42 @@ var _ = Describe("RequestAccessor tests", func() {
Expect("value1").To(Equal(stageVars["var1"]))
Expect("value2").To(Equal(stageVars["var2"]))
})

It("Populates the default hostname correctly", func() {
basicRequest := getProxyRequest("orders", "GET")
accessor := core.RequestAccessor{}
httpReq, err := accessor.ProxyEventToHTTPRequest(basicRequest)
Expect(err).To(BeNil())

Expect(core.DefaultServerAddress).To(Equal("https://" + httpReq.Host))
Expect(core.DefaultServerAddress).To(Equal("https://" + httpReq.URL.Host))
})

It("Uses a custom hostname", func() {
myCustomHost := "http://my-custom-host.com"
os.Setenv(core.CustomHostVariable, myCustomHost)
basicRequest := getProxyRequest("orders", "GET")
accessor := core.RequestAccessor{}
httpReq, err := accessor.ProxyEventToHTTPRequest(basicRequest)
Expect(err).To(BeNil())

Expect(myCustomHost).To(Equal("http://" + httpReq.Host))
Expect(myCustomHost).To(Equal("http://" + httpReq.URL.Host))
os.Unsetenv(core.CustomHostVariable)
})

It("Strips terminating / from hostname", func() {
myCustomHost := "http://my-custom-host.com"
os.Setenv(core.CustomHostVariable, myCustomHost+"/")
basicRequest := getProxyRequest("orders", "GET")
accessor := core.RequestAccessor{}
httpReq, err := accessor.ProxyEventToHTTPRequest(basicRequest)
Expect(err).To(BeNil())

Expect(myCustomHost).To(Equal("http://" + httpReq.Host))
Expect(myCustomHost).To(Equal("http://" + httpReq.URL.Host))
os.Unsetenv(core.CustomHostVariable)
})
})
})

Expand Down
2 changes: 1 addition & 1 deletion core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func GatewayTimeout() events.APIGatewayProxyResponse {
return events.APIGatewayProxyResponse{StatusCode: http.StatusGatewayTimeout}
}

// New Logged Error
// NewLoggedError generates a new error and logs it to stdout
func NewLoggedError(format string, a ...interface{}) error {
err := fmt.Errorf(format, a...)
fmt.Println(err.Error())
Expand Down
3 changes: 0 additions & 3 deletions gorillamux/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gorillamux_test

import (
"fmt"
"log"
"net/http"

"github.com/aws/aws-lambda-go/events"
Expand All @@ -16,8 +15,6 @@ import (
var _ = Describe("GorillaMuxAdapter tests", func() {
Context("Simple ping request", func() {
It("Proxies the event correctly", func() {
log.Println("Starting test")

homeHandler := func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("unfortunately-required-header", "")
fmt.Fprintf(w, "Home Page")
Expand Down