There are a lot of mux routers for golang. All of the ones I've found assume that I'm building my own HTTP server in go. However, I would like to use aws apigateway as the external layer and have it forward the method, path, query parameters to a lambda function that I have deployed with apex (go shim for aws lambda functions). All the API gateway endpoints will forward to one lambda function so that there are fewer things to hook up, like permissions and so forth.
So I would like to use nice mux libraries for their ability to parse regex or path variables, but use them inside the Lambda and be able to invoke the correct handler based on the url path.
Most of the mux routers have a usage like this:
router := NewRouter()
router.Add("GET", "/my_path/:id", MyHandler)
Where MyHandler
is a type of http.HandlerFunc
Then the server is started with something like http.ListenAndServe(port, router)
But in AWS Lambda there is no server to start, I would just like to use the mux to find the handler that I should be calling.
Package gorilla/mux implements a request router and dispatcher for matching incoming requests to their respective handler. The name mux stands for "HTTP request multiplexer". Like the standard http.
The name mux stands for "HTTP request multiplexer". Like the standard http. ServeMux, mux. Router matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions.
ServeMux is an HTTP request multiplexer. It is used for request routing and dispatching. The request routing is based on URL patterns. Each incoming request's URL is matched against a list of registered patterns. A handler for the pattern that most closely fits the URL is called.
Mux. Mux (gorilla) implements a request router and dispatcher for matching incoming requests to their respective handler. Features. It implements the http. Handler interface so it is compatible with the standard http.
I have created a lib for this purpose.
The basic idea is to transform apigateway request context which is a json object into http.Request
The most common approach is to use apex/gateway
which is a drop in replacement for net/http
on AWS Lambda. This is started by calling with the same parameters.
gateway.ListenAndServe(port, router)
Here is a sample showing it's use:
func main() {
http.HandleFunc("/", hello)
log.Fatal(gateway.ListenAndServe(":3000", nil))
}
func hello(w http.ResponseWriter, r *http.Request) {
// example retrieving values from the api gateway proxy request context.
requestContext, ok := gateway.RequestContext(r.Context())
if !ok || requestContext.Authorizer["sub"] == nil {
fmt.Fprint(w, "Hello World from Go")
return
}
userID := requestContext.Authorizer["sub"].(string)
fmt.Fprintf(w, "Hello %s from Go", userID)
}
It's also common to use this with the chi
router.
Some other options include:
davyzhang/agw
(listed in Davy's response)davidsbond/lux
EtienneBruines/fasthttplambda
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With