good morning!!!!

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

import (
Garet Halliday's avatar
Garet Halliday committed
	"context"
	"errors"
a's avatar
a committed
	"net/http"
a's avatar
ok  
a committed
	"sync"
a's avatar
a committed

a's avatar
a committed
	"golang.org/x/net/http2"
	"golang.org/x/net/http2/h2c"
Garet Halliday's avatar
Garet Halliday committed

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

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

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 := NewCodec(w, r)
a's avatar
a committed
	w.Header().Set("content-type", contentType)
a's avatar
a committed
	err := s.Server.ServeCodec(r.Context(), c)
Garet Halliday's avatar
Garet Halliday committed
	if err != nil && !errors.Is(err, context.Canceled) {
		//  slog.Error("codec err", "err", err)
		http.Error(w, "Internal Error", http.StatusInternalServerError)
a's avatar
a committed
	}
a's avatar
ok  
a committed
	<-c.Closed()
a's avatar
a committed
}