You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/transport/http/service.go

50 lines
1.3 KiB

4 years ago
package http
import (
"context"
"net/http"
)
// SupportPackageIsVersion1 These constants should not be referenced from any other code.
const SupportPackageIsVersion1 = true
type methodHandler func(srv interface{}, ctx context.Context, req *http.Request, dec func(interface{}) error) (out interface{}, err error)
4 years ago
// MethodDesc represents a Proto service's method specification.
type MethodDesc struct {
Path string
Method string
Handler methodHandler
}
// ServiceDesc represents a Proto service's specification.
type ServiceDesc struct {
ServiceName string
Methods []MethodDesc
Metadata interface{}
}
// ServiceRegistrar wraps a single method that supports service registration.
type ServiceRegistrar interface {
RegisterService(desc *ServiceDesc, impl interface{})
}
// RegisterService .
func (s *Server) RegisterService(desc *ServiceDesc, impl interface{}) {
for _, m := range desc.Methods {
h := m.Handler
s.router.HandleFunc(m.Path, func(res http.ResponseWriter, req *http.Request) {
out, err := h(impl, req.Context(), req, func(v interface{}) error {
return s.requestDecoder(req, v)
})
4 years ago
if err != nil {
s.errorEncoder(res, req, err)
return
}
if err := s.responseEncoder(res, req, out); err != nil {
s.errorEncoder(res, req, err)
}
}).Methods(m.Method)
}
}