good morning!!!!

Skip to content
Snippets Groups Projects
handler.go 628 B
Newer Older
a's avatar
a committed
package http

import (
	"net/http"
a's avatar
ok  
a committed
	"sync"
a's avatar
a committed

	"gfx.cafe/open/jrpc/pkg/server"
a's avatar
a committed
)

a's avatar
a committed
func HttpHandler(s *server.Server) http.Handler {
	return &Server{Server: s}
}

a's avatar
a committed
type Server struct {
a's avatar
a committed
	Server *server.Server
a's avatar
a committed
}

a's avatar
ok  
a committed
var codecPool = sync.Pool{
	New: func() any {
		return &Codec{}
	},
}

a's avatar
a committed
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if s.Server == nil {
		http.Error(w, "no server set", http.StatusInternalServerError)
		return
	}
a's avatar
ok  
a committed
	c := codecPool.Get().(*Codec)
	c.Reset(w, r)
a's avatar
a committed
	w.Header().Set("content-type", contentType)
a's avatar
a committed
	s.Server.ServeCodec(r.Context(), c)
a's avatar
ok  
a committed
	go func() {
		<-c.Closed()
		codecPool.Put(c)
	}()
a's avatar
a committed
}