Skip to content

Instantly share code, notes, and snippets.

@shrmpy
Created November 19, 2021 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shrmpy/536a78d1b3e79b7bac50a1af2deb8672 to your computer and use it in GitHub Desktop.
Save shrmpy/536a78d1b3e79b7bac50a1af2deb8672 to your computer and use it in GitHub Desktop.
package middleware to manage headers and support the CORS tests
package ebs
import (
"net/http"
"github.com/aws/aws-lambda-go/events"
)
type HandlerFunc func(events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)
/*
func MiddlewareTemplate(next HandlerFunc) HandlerFunc {
return func(ev events.APIGatewayProxyRequest)
(events.APIGatewayProxyResponse, error) {
return next(ev)
}
}
*/
func MiddlewareCORS(conf *Config, next HandlerFunc) HandlerFunc {
return func(ev events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
// preflight check is short-circuited
if ev.HTTPMethod == "OPTIONS" {
return blankResponse(conf, "", http.StatusOK), nil
}
// without next, just act same as preflight
if next == nil {
return blankResponse(conf, "", http.StatusOK), nil
}
// run next handler along chain
resp, err := next(ev)
if err != nil {
return resp, err
}
// post-process
resp.Headers = enableCors(conf, resp.Headers)
return resp, nil
}
}
func enableCors(conf *Config, headers map[string]string) map[string]string {
m := map[string]string{
"Access-Control-Allow-Origin": conf.Hostname(),
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT",
"Access-Control-Allow-Headers": "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization",
}
// TODO merge, if CORS headers exist
for key, val := range headers {
m[key] = val
}
return m
}
func blankResponse(conf *Config, descr string, status int) events.APIGatewayProxyResponse {
h := enableCors(conf, make(map[string]string))
h["Content-Type"] = "application/json"
return events.APIGatewayProxyResponse{
Headers: h,
StatusCode: status,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment