Skip to content

Use existing or third party net http handlers

srfrog edited this page Aug 9, 2014 · 3 revisions

Service.Handler() returns two values: the path of the service (base URI path) and the service handler function.

myservice := relax.NewService("/api/v1")
// split the return of Handler()
path, handler := myservice.Handler()

// now use handler as you would with any other http.Handler...

// for example, using TimeoutHandler to limit the time for requests.
http.Handle(path, http.TimeoutHandler(handler, 3, "Time out!"))

log.Fatal(http.ListenAndServe(":8000", nil))

Update Alternatively, you can use Service.Adapter() which returns the service handler itself.

http.Handle(path, http.TimeoutHandler(myservice.Adapter(), 3, "Time out!"))