add option to set http handlers

This commit is contained in:
Asim Aslam
2018-11-18 16:32:53 +00:00
parent 98bb4a69c2
commit 1d8047a272
3 changed files with 37 additions and 0 deletions

View File

@@ -357,6 +357,8 @@ func (h *httpTransportListener) Close() error {
func (h *httpTransportListener) Accept(fn func(Socket)) error {
// create handler mux
mux := http.NewServeMux()
// register our transport handler
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var buf *bufio.ReadWriter
var con net.Conn
@@ -403,6 +405,16 @@ func (h *httpTransportListener) Accept(fn func(Socket)) error {
})
})
// get optional handlers
if h.ht.opts.Context != nil {
handlers, ok := h.ht.opts.Context.Value("http_handlers").(map[string]http.Handler)
if ok {
for pattern, handler := range handlers {
mux.Handle(pattern, handler)
}
}
}
// default http2 server
srv := &http.Server{
Handler: mux,