Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way use mux routing to select a handler without using http listenAndServe for golang?

Tags:

lambda

go

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.

like image 633
Homan Avatar asked Nov 22 '17 00:11

Homan


People also ask

How does Gorilla mux work?

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.

What is mux in Go lang?

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.

What is a ServeMux?

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.

What is MUX framework?

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.


2 Answers

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

like image 156
davyzhang Avatar answered Oct 13 '22 09:10

davyzhang


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
like image 29
Grokify Avatar answered Oct 13 '22 08:10

Grokify