martins
martins

Reputation: 10009

How do I pass extra arguments to an Echo mux handler?

How do I pass along some extra variables to an Echo mux handler?

I have registered a route like this in main.go:

 e.GET("/search/:query", handlers.Search(*kindex, app.infoLog))

As you might see this is not the correct signature for the handler. It should have been passed without any arguments. i.e handlers.Search

How can I access kindex and infoLog from my Search handler?

func Search(c echo.Context, kindex string, infoLog *log.Logger) error {
  # Should I access a global variable from here?
  infoLog.Printf("Kendra Index: %v\n", kindex)
  # cut..
}

Upvotes: 2

Views: 2169

Answers (1)

maxim_ge
maxim_ge

Reputation: 1263

You can create an anonymous function (closure) of given type and pass it to Echo:

    handler := func(c echo.Context) error {
      return Search(c, *kindex, infoLog)
    }
    e.GET("/search/:query", handler)

Upvotes: 6

Related Questions