2016-06-30 22:21:57 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2021-02-05 18:31:51 +03:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-02-06 18:53:25 +03:00
|
|
|
"io"
|
2021-02-05 18:31:51 +03:00
|
|
|
"net/http"
|
|
|
|
"reflect"
|
2024-05-04 19:38:21 +03:00
|
|
|
"slices"
|
2024-04-23 22:21:27 +03:00
|
|
|
"strconv"
|
2021-02-05 18:31:51 +03:00
|
|
|
"strings"
|
2021-04-16 17:14:27 +03:00
|
|
|
"sync"
|
2024-04-23 22:21:27 +03:00
|
|
|
"time"
|
2021-02-05 18:31:51 +03:00
|
|
|
|
2021-10-26 22:36:04 +03:00
|
|
|
"go.unistack.org/micro/v3/errors"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/metadata"
|
2024-09-20 16:05:48 +03:00
|
|
|
"go.unistack.org/micro/v3/meter"
|
2024-04-23 22:21:27 +03:00
|
|
|
"go.unistack.org/micro/v3/options"
|
2021-10-26 22:36:04 +03:00
|
|
|
"go.unistack.org/micro/v3/register"
|
2024-04-23 22:21:27 +03:00
|
|
|
"go.unistack.org/micro/v3/semconv"
|
2021-10-26 22:36:04 +03:00
|
|
|
"go.unistack.org/micro/v3/server"
|
2024-04-23 22:21:27 +03:00
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2021-10-26 22:36:04 +03:00
|
|
|
rhttp "go.unistack.org/micro/v3/util/http"
|
|
|
|
rflutil "go.unistack.org/micro/v3/util/reflect"
|
2021-02-05 18:31:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
DefaultErrorHandler = func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int) {
|
|
|
|
w.WriteHeader(status)
|
2021-04-26 00:43:06 +03:00
|
|
|
if _, cerr := w.Write([]byte(err.Error())); cerr != nil {
|
2024-10-12 13:24:32 +03:00
|
|
|
logger.DefaultLogger.Error(ctx, "write error", cerr)
|
2021-04-26 00:43:06 +03:00
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
DefaultContentType = "application/json"
|
2016-06-30 22:21:57 +03:00
|
|
|
)
|
|
|
|
|
2021-02-05 18:31:51 +03:00
|
|
|
type patHandler struct {
|
|
|
|
mtype *methodType
|
|
|
|
rcvr reflect.Value
|
2021-10-26 22:36:04 +03:00
|
|
|
name string
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 22:21:57 +03:00
|
|
|
type httpHandler struct {
|
2021-03-09 14:19:59 +03:00
|
|
|
opts server.HandlerOptions
|
|
|
|
hd interface{}
|
2022-01-22 01:10:24 +03:00
|
|
|
handlers *rhttp.Trie
|
2021-04-25 12:02:18 +03:00
|
|
|
name string
|
|
|
|
eps []*register.Endpoint
|
|
|
|
sopts server.Options
|
2021-04-16 17:14:27 +03:00
|
|
|
sync.RWMutex
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 22:21:57 +03:00
|
|
|
func (h *httpHandler) Name() string {
|
2021-02-05 18:31:51 +03:00
|
|
|
return h.name
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpHandler) Handler() interface{} {
|
|
|
|
return h.hd
|
|
|
|
}
|
|
|
|
|
2021-01-29 14:32:32 +03:00
|
|
|
func (h *httpHandler) Endpoints() []*register.Endpoint {
|
2017-04-03 17:03:46 +03:00
|
|
|
return h.eps
|
2016-06-30 22:21:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpHandler) Options() server.HandlerOptions {
|
|
|
|
return h.opts
|
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
|
2023-05-09 21:14:55 +03:00
|
|
|
func (h *Server) HTTPHandlerFunc(handler interface{}) (http.HandlerFunc, error) {
|
2023-05-08 22:23:34 +03:00
|
|
|
if handler == nil {
|
|
|
|
return nil, fmt.Errorf("invalid handler specified: %v", handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
rtype := reflect.TypeOf(handler)
|
|
|
|
if rtype.NumIn() != 3 {
|
|
|
|
return nil, fmt.Errorf("invalid handler, NumIn != 3: %v", rtype.NumIn())
|
|
|
|
}
|
|
|
|
|
|
|
|
argType := rtype.In(1)
|
|
|
|
replyType := rtype.In(2)
|
|
|
|
|
|
|
|
// First arg need not be a pointer.
|
|
|
|
if !isExportedOrBuiltinType(argType) {
|
|
|
|
return nil, fmt.Errorf("invalid handler, argument type not exported: %v", argType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if replyType.Kind() != reflect.Ptr {
|
|
|
|
return nil, fmt.Errorf("invalid handler, reply type not a pointer: %v", replyType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reply type must be exported.
|
|
|
|
if !isExportedOrBuiltinType(replyType) {
|
|
|
|
return nil, fmt.Errorf("invalid handler, reply type not exported: %v", replyType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rtype.NumOut() != 1 {
|
|
|
|
return nil, fmt.Errorf("invalid handler, has wrong number of outs: %v", rtype.NumOut())
|
|
|
|
}
|
|
|
|
|
|
|
|
// The return type of the method must be error.
|
|
|
|
if returnType := rtype.Out(0); returnType != typeOfError {
|
|
|
|
return nil, fmt.Errorf("invalid handler, returns %v not error", returnType.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ct := DefaultContentType
|
|
|
|
if htype := r.Header.Get(metadata.HeaderContentType); htype != "" {
|
|
|
|
ct = htype
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.WithValue(r.Context(), rspCodeKey{}, &rspCodeVal{})
|
|
|
|
ctx = context.WithValue(ctx, rspHeaderKey{}, &rspHeaderVal{})
|
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
md = metadata.New(len(r.Header) + 8)
|
|
|
|
}
|
|
|
|
for k, v := range r.Header {
|
|
|
|
md[k] = strings.Join(v, ", ")
|
|
|
|
}
|
|
|
|
md["RemoteAddr"] = r.RemoteAddr
|
|
|
|
md["Method"] = r.Method
|
|
|
|
md["URL"] = r.URL.String()
|
|
|
|
md["Proto"] = r.Proto
|
2024-03-11 23:30:33 +03:00
|
|
|
md["Content-Length"] = fmt.Sprintf("%d", r.ContentLength)
|
|
|
|
md["Transfer-Encoding"] = strings.Join(r.TransferEncoding, ",")
|
2023-05-08 22:23:34 +03:00
|
|
|
md["Host"] = r.Host
|
|
|
|
md["RequestURI"] = r.RequestURI
|
2023-08-03 10:42:54 +03:00
|
|
|
if r.TLS != nil {
|
2023-08-03 10:43:33 +03:00
|
|
|
md["TLS"] = "true"
|
2024-03-11 23:30:33 +03:00
|
|
|
md["TLS-ALPN"] = r.TLS.NegotiatedProtocol
|
|
|
|
md["TLS-ServerName"] = r.TLS.ServerName
|
2023-08-03 10:42:54 +03:00
|
|
|
}
|
|
|
|
|
2023-05-08 22:23:34 +03:00
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
|
|
|
|
|
|
|
path := r.URL.Path
|
|
|
|
|
|
|
|
if r.Body != nil {
|
|
|
|
defer r.Body.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := make(map[string]interface{})
|
|
|
|
var match bool
|
|
|
|
var hldr *patHandler
|
|
|
|
var handler *httpHandler
|
|
|
|
|
|
|
|
for _, shdlr := range h.handlers {
|
|
|
|
hdlr := shdlr.(*httpHandler)
|
|
|
|
fh, mp, err := hdlr.handlers.Search(r.Method, path)
|
|
|
|
if err == nil {
|
|
|
|
match = true
|
|
|
|
for k, v := range mp {
|
|
|
|
matches[k] = v
|
|
|
|
}
|
|
|
|
hldr = fh.(*patHandler)
|
|
|
|
handler = hdlr
|
|
|
|
break
|
|
|
|
} else if err == rhttp.ErrMethodNotAllowed && !h.registerRPC {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
_, _ = w.Write([]byte("not matching route found"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !match && h.registerRPC {
|
|
|
|
microMethod, mok := md.Get(metadata.HeaderEndpoint)
|
|
|
|
if mok {
|
|
|
|
serviceMethod := strings.Split(microMethod, ".")
|
|
|
|
if len(serviceMethod) == 2 {
|
|
|
|
if shdlr, ok := h.handlers[serviceMethod[0]]; ok {
|
|
|
|
hdlr := shdlr.(*httpHandler)
|
|
|
|
fh, mp, err := hdlr.handlers.Search(http.MethodPost, "/"+microMethod)
|
|
|
|
if err == nil {
|
|
|
|
match = true
|
|
|
|
for k, v := range mp {
|
|
|
|
matches[k] = v
|
|
|
|
}
|
|
|
|
hldr = fh.(*patHandler)
|
|
|
|
handler = hdlr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get fields from url values
|
|
|
|
if len(r.URL.RawQuery) > 0 {
|
|
|
|
umd, cerr := rflutil.URLMap(r.URL.RawQuery)
|
|
|
|
if cerr != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = w.Write([]byte(cerr.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for k, v := range umd {
|
|
|
|
matches[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cf, err := h.newCodec(ct)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
_, _ = w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var argv, replyv reflect.Value
|
|
|
|
|
|
|
|
// Decode the argument value.
|
|
|
|
argIsValue := false // if true, need to indirect before calling.
|
|
|
|
if hldr.mtype.ArgType.Kind() == reflect.Ptr {
|
|
|
|
argv = reflect.New(hldr.mtype.ArgType.Elem())
|
|
|
|
} else {
|
|
|
|
argv = reflect.New(hldr.mtype.ArgType)
|
|
|
|
argIsValue = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if argIsValue {
|
|
|
|
argv = argv.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
// reply value
|
|
|
|
replyv = reflect.New(hldr.mtype.ReplyType.Elem())
|
|
|
|
|
|
|
|
function := hldr.mtype.method.Func
|
|
|
|
var returnValues []reflect.Value
|
|
|
|
|
|
|
|
if r.Body != nil {
|
|
|
|
var buf []byte
|
|
|
|
buf, err = io.ReadAll(r.Body)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = cf.Unmarshal(buf, argv.Interface()); err != nil {
|
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matches = rflutil.FlattenMap(matches)
|
|
|
|
if err = rflutil.Merge(argv.Interface(), matches, rflutil.SliceAppend(true), rflutil.Tags([]string{"protobuf", "json"})); err != nil {
|
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hr := &rpcRequest{
|
|
|
|
codec: cf,
|
|
|
|
service: handler.sopts.Name,
|
|
|
|
contentType: ct,
|
|
|
|
method: fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name),
|
|
|
|
endpoint: fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name),
|
|
|
|
payload: argv.Interface(),
|
|
|
|
header: md,
|
|
|
|
}
|
|
|
|
|
|
|
|
// define the handler func
|
|
|
|
fn := func(fctx context.Context, req server.Request, rsp interface{}) (err error) {
|
|
|
|
returnValues = function.Call([]reflect.Value{hldr.rcvr, hldr.mtype.prepareContext(fctx), argv, reflect.ValueOf(rsp)})
|
|
|
|
|
|
|
|
// The return value for the method is an error.
|
|
|
|
if rerr := returnValues[0].Interface(); rerr != nil {
|
|
|
|
err = rerr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
md = metadata.New(0)
|
|
|
|
}
|
|
|
|
if nmd, ok := metadata.FromOutgoingContext(fctx); ok {
|
|
|
|
for k, v := range nmd {
|
|
|
|
md.Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
metadata.SetOutgoingContext(ctx, md)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrap the handler func
|
2024-04-23 22:21:27 +03:00
|
|
|
h.opts.Hooks.EachNext(func(hook options.Hook) {
|
|
|
|
if h, ok := hook.(server.HookHandler); ok {
|
|
|
|
fn = h(fn)
|
|
|
|
}
|
|
|
|
})
|
2023-05-08 22:23:34 +03:00
|
|
|
|
|
|
|
if ct == "application/x-www-form-urlencoded" {
|
|
|
|
cf, err = h.newCodec(DefaultContentType)
|
|
|
|
if err != nil {
|
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ct = DefaultContentType
|
|
|
|
}
|
|
|
|
|
|
|
|
scode := int(200)
|
|
|
|
appErr := fn(ctx, hr, replyv.Interface())
|
|
|
|
|
|
|
|
w.Header().Set(metadata.HeaderContentType, ct)
|
|
|
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
|
|
|
for k, v := range md {
|
|
|
|
w.Header().Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if md := getRspHeader(ctx); md != nil {
|
|
|
|
for k, v := range md {
|
|
|
|
for _, vv := range v {
|
|
|
|
w.Header().Add(k, vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nct := w.Header().Get(metadata.HeaderContentType); nct != ct {
|
|
|
|
if cf, err = h.newCodec(nct); err != nil {
|
|
|
|
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf []byte
|
|
|
|
if appErr != nil {
|
|
|
|
switch verr := appErr.(type) {
|
|
|
|
case *errors.Error:
|
|
|
|
scode = int(verr.Code)
|
|
|
|
buf, err = cf.Marshal(verr)
|
|
|
|
case *Error:
|
|
|
|
buf, err = cf.Marshal(verr.err)
|
|
|
|
default:
|
|
|
|
buf, err = cf.Marshal(appErr)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buf, err = cf.Marshal(replyv.Interface())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) {
|
2024-10-12 13:24:32 +03:00
|
|
|
handler.sopts.Logger.Error(handler.sopts.Context, "handler error", err)
|
2023-05-08 22:23:34 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if nscode := GetRspCode(ctx); nscode != 0 {
|
|
|
|
scode = nscode
|
|
|
|
}
|
|
|
|
w.WriteHeader(scode)
|
|
|
|
|
|
|
|
if _, cerr := w.Write(buf); cerr != nil {
|
2024-10-12 13:24:32 +03:00
|
|
|
handler.sopts.Logger.Error(ctx, "write failed", cerr)
|
2023-05-08 22:23:34 +03:00
|
|
|
}
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-09 21:14:55 +03:00
|
|
|
func (h *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2021-04-13 23:42:33 +03:00
|
|
|
ct := DefaultContentType
|
2021-10-26 22:36:04 +03:00
|
|
|
if htype := r.Header.Get(metadata.HeaderContentType); htype != "" {
|
2021-04-13 23:42:33 +03:00
|
|
|
ct = htype
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:21:27 +03:00
|
|
|
ts := time.Now()
|
|
|
|
|
2021-04-30 00:40:08 +03:00
|
|
|
ctx := context.WithValue(r.Context(), rspCodeKey{}, &rspCodeVal{})
|
2023-02-11 01:18:16 +03:00
|
|
|
ctx = context.WithValue(ctx, rspHeaderKey{}, &rspHeaderVal{})
|
2024-04-23 22:21:27 +03:00
|
|
|
|
2021-04-30 00:40:08 +03:00
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
2022-01-22 01:10:24 +03:00
|
|
|
md = metadata.New(len(r.Header) + 8)
|
2021-04-30 00:40:08 +03:00
|
|
|
}
|
|
|
|
for k, v := range r.Header {
|
2023-02-13 00:09:54 +03:00
|
|
|
md[k] = strings.Join(v, ", ")
|
2021-04-30 00:40:08 +03:00
|
|
|
}
|
2023-02-13 00:09:54 +03:00
|
|
|
md["RemoteAddr"] = r.RemoteAddr
|
2023-05-19 23:24:53 +03:00
|
|
|
if r.TLS != nil {
|
|
|
|
md["Scheme"] = "https"
|
|
|
|
} else {
|
|
|
|
md["Scheme"] = "http"
|
|
|
|
}
|
2023-02-13 00:09:54 +03:00
|
|
|
md["Method"] = r.Method
|
|
|
|
md["URL"] = r.URL.String()
|
|
|
|
md["Proto"] = r.Proto
|
|
|
|
md["ContentLength"] = fmt.Sprintf("%d", r.ContentLength)
|
2023-05-19 23:24:53 +03:00
|
|
|
if len(r.TransferEncoding) > 0 {
|
|
|
|
md["TransferEncoding"] = strings.Join(r.TransferEncoding, ",")
|
|
|
|
}
|
2023-02-13 00:09:54 +03:00
|
|
|
md["Host"] = r.Host
|
|
|
|
md["RequestURI"] = r.RequestURI
|
2021-04-30 00:40:08 +03:00
|
|
|
ctx = metadata.NewIncomingContext(ctx, md)
|
2021-02-05 18:31:51 +03:00
|
|
|
|
|
|
|
path := r.URL.Path
|
|
|
|
if !strings.HasPrefix(path, "/") {
|
2021-10-26 22:36:04 +03:00
|
|
|
h.errorHandler(ctx, nil, w, r, fmt.Errorf("path must starts with /"), http.StatusBadRequest)
|
2021-03-09 23:54:46 +03:00
|
|
|
return
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
matches := make(map[string]interface{})
|
2021-03-31 00:49:55 +03:00
|
|
|
|
2021-02-05 18:31:51 +03:00
|
|
|
var match bool
|
2022-01-22 01:10:24 +03:00
|
|
|
var hldr *patHandler
|
2021-03-09 14:19:59 +03:00
|
|
|
var handler *httpHandler
|
2021-07-14 13:43:45 +03:00
|
|
|
|
2022-01-22 01:10:24 +03:00
|
|
|
for _, shdlr := range h.handlers {
|
|
|
|
hdlr := shdlr.(*httpHandler)
|
2022-12-27 23:59:02 +03:00
|
|
|
fh, mp, err := hdlr.handlers.Search(r.Method, path)
|
|
|
|
if err == nil {
|
2022-01-22 01:10:24 +03:00
|
|
|
match = true
|
|
|
|
for k, v := range mp {
|
|
|
|
matches[k] = v
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
2022-01-22 01:10:24 +03:00
|
|
|
hldr = fh.(*patHandler)
|
|
|
|
handler = hdlr
|
|
|
|
break
|
2022-12-27 23:59:02 +03:00
|
|
|
} else if err == rhttp.ErrMethodNotAllowed && !h.registerRPC {
|
2023-03-09 08:42:04 +03:00
|
|
|
h.errorHandler(ctx, nil, w, r, fmt.Errorf("not matching route found"), http.StatusMethodNotAllowed)
|
2023-02-05 11:29:51 +03:00
|
|
|
return
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 20:43:27 +03:00
|
|
|
if !match && h.registerRPC {
|
|
|
|
microMethod, mok := md.Get(metadata.HeaderEndpoint)
|
|
|
|
if mok {
|
|
|
|
serviceMethod := strings.Split(microMethod, ".")
|
|
|
|
if len(serviceMethod) == 2 {
|
|
|
|
if shdlr, ok := h.handlers[serviceMethod[0]]; ok {
|
|
|
|
hdlr := shdlr.(*httpHandler)
|
2022-12-27 23:59:02 +03:00
|
|
|
fh, mp, err := hdlr.handlers.Search(http.MethodPost, "/"+microMethod)
|
|
|
|
if err == nil {
|
2022-01-30 20:43:27 +03:00
|
|
|
match = true
|
|
|
|
for k, v := range mp {
|
|
|
|
matches[k] = v
|
|
|
|
}
|
|
|
|
hldr = fh.(*patHandler)
|
|
|
|
handler = hdlr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:21:27 +03:00
|
|
|
var sp tracer.Span
|
2022-01-23 02:00:15 +03:00
|
|
|
if !match && h.hd != nil {
|
|
|
|
if hdlr, ok := h.hd.Handler().(http.Handler); ok {
|
2024-11-10 18:30:19 +03:00
|
|
|
endpointName := fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name)
|
|
|
|
if !slices.Contains(tracer.DefaultSkipEndpoints, endpointName) {
|
|
|
|
ctx, sp = h.opts.Tracer.Start(ctx, "rpc-server",
|
2024-09-20 16:05:48 +03:00
|
|
|
tracer.WithSpanKind(tracer.SpanKindServer),
|
|
|
|
tracer.WithSpanLabels(
|
2024-11-10 18:30:19 +03:00
|
|
|
"endpoint", endpointName,
|
2024-09-20 16:05:48 +03:00
|
|
|
),
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
n := GetRspCode(ctx)
|
|
|
|
if s, _ := sp.Status(); s != tracer.SpanStatusError && n > 399 {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
|
|
|
|
}
|
|
|
|
sp.Finish()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-11-10 18:30:19 +03:00
|
|
|
if !slices.Contains(meter.DefaultSkipEndpoints, endpointName) {
|
|
|
|
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", endpointName).Inc()
|
2024-09-20 16:05:48 +03:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
n := GetRspCode(ctx)
|
|
|
|
if n > 399 {
|
2024-11-10 18:30:19 +03:00
|
|
|
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", endpointName, "status", "success", "code", strconv.Itoa(n)).Inc()
|
2024-09-20 16:05:48 +03:00
|
|
|
} else {
|
2024-11-10 18:30:19 +03:00
|
|
|
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", endpointName, "status", "failure", "code", strconv.Itoa(n)).Inc()
|
2024-09-20 16:05:48 +03:00
|
|
|
}
|
|
|
|
te := time.Since(ts)
|
2024-11-10 18:30:19 +03:00
|
|
|
h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", endpointName).Update(te.Seconds())
|
|
|
|
h.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", endpointName).Update(te.Seconds())
|
|
|
|
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", endpointName).Dec()
|
2024-09-20 16:05:48 +03:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
hdlr.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if !match {
|
|
|
|
// check for http.HandlerFunc handlers
|
|
|
|
if !slices.Contains(tracer.DefaultSkipEndpoints, r.URL.Path) {
|
2024-11-10 18:30:19 +03:00
|
|
|
ctx, sp = h.opts.Tracer.Start(ctx, "rpc-server",
|
2024-04-23 22:21:27 +03:00
|
|
|
tracer.WithSpanKind(tracer.SpanKindServer),
|
|
|
|
tracer.WithSpanLabels(
|
2024-09-20 16:05:48 +03:00
|
|
|
"endpoint", r.URL.Path,
|
2024-04-23 22:21:27 +03:00
|
|
|
),
|
|
|
|
)
|
2024-09-20 16:05:48 +03:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if n := GetRspCode(ctx); n > 399 {
|
2024-04-23 22:21:27 +03:00
|
|
|
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
|
2024-09-20 16:05:48 +03:00
|
|
|
} else {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, http.StatusText(http.StatusNotFound))
|
2024-04-23 22:21:27 +03:00
|
|
|
}
|
2024-09-20 16:05:48 +03:00
|
|
|
sp.Finish()
|
|
|
|
}()
|
2022-01-23 02:00:15 +03:00
|
|
|
}
|
2023-05-19 23:02:42 +03:00
|
|
|
if ph, _, err := h.pathHandlers.Search(r.Method, r.URL.Path); err == nil {
|
2024-04-23 22:21:27 +03:00
|
|
|
ph.(http.HandlerFunc)(w, r.WithContext(ctx))
|
2023-05-19 23:02:42 +03:00
|
|
|
return
|
|
|
|
}
|
2021-03-09 14:19:59 +03:00
|
|
|
h.errorHandler(ctx, nil, w, r, fmt.Errorf("not matching route found"), http.StatusNotFound)
|
2021-02-05 18:31:51 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-04 19:38:21 +03:00
|
|
|
endpointName := fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name)
|
2024-11-10 18:30:19 +03:00
|
|
|
|
2024-05-04 19:38:21 +03:00
|
|
|
topts := []tracer.SpanOption{
|
2024-04-23 22:21:27 +03:00
|
|
|
tracer.WithSpanKind(tracer.SpanKindServer),
|
|
|
|
tracer.WithSpanLabels(
|
2024-05-04 19:38:21 +03:00
|
|
|
"endpoint", endpointName,
|
2024-04-23 22:21:27 +03:00
|
|
|
),
|
2024-05-04 19:38:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if slices.Contains(tracer.DefaultSkipEndpoints, endpointName) {
|
|
|
|
topts = append(topts, tracer.WithSpanRecord(false))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, sp = h.opts.Tracer.Start(ctx, endpointName+" rpc-server", topts...)
|
|
|
|
|
2024-09-20 16:05:48 +03:00
|
|
|
if !slices.Contains(meter.DefaultSkipEndpoints, handler.name) {
|
|
|
|
defer func() {
|
|
|
|
te := time.Since(ts)
|
|
|
|
h.opts.Meter.Summary(semconv.ServerRequestLatencyMicroseconds, "endpoint", handler.name).Update(te.Seconds())
|
|
|
|
h.opts.Meter.Histogram(semconv.ServerRequestDurationSeconds, "endpoint", handler.name).Update(te.Seconds())
|
|
|
|
h.opts.Meter.Counter(semconv.ServerRequestInflight, "endpoint", handler.name).Dec()
|
2024-04-23 22:21:27 +03:00
|
|
|
|
2024-09-20 16:05:48 +03:00
|
|
|
n := GetRspCode(ctx)
|
|
|
|
if n > 399 {
|
|
|
|
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "status", "failure", "code", strconv.Itoa(n)).Inc()
|
|
|
|
} else {
|
|
|
|
h.opts.Meter.Counter(semconv.ServerRequestTotal, "endpoint", handler.name, "status", "success", "code", strconv.Itoa(n)).Inc()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2024-04-23 22:21:27 +03:00
|
|
|
n := GetRspCode(ctx)
|
|
|
|
if n > 399 {
|
|
|
|
if s, _ := sp.Status(); s != tracer.SpanStatusError {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, http.StatusText(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sp.Finish()
|
|
|
|
}()
|
|
|
|
|
2021-04-10 01:24:28 +03:00
|
|
|
// get fields from url values
|
|
|
|
if len(r.URL.RawQuery) > 0 {
|
2021-04-26 00:43:06 +03:00
|
|
|
umd, cerr := rflutil.URLMap(r.URL.RawQuery)
|
|
|
|
if cerr != nil {
|
|
|
|
h.errorHandler(ctx, handler, w, r, cerr, http.StatusBadRequest)
|
2021-03-31 00:49:55 +03:00
|
|
|
return
|
|
|
|
}
|
2021-04-10 01:24:28 +03:00
|
|
|
for k, v := range umd {
|
|
|
|
matches[k] = v
|
|
|
|
}
|
2021-03-31 00:49:55 +03:00
|
|
|
}
|
|
|
|
|
2023-05-19 23:02:42 +03:00
|
|
|
if r.Body != nil {
|
|
|
|
defer r.Body.Close()
|
|
|
|
}
|
|
|
|
|
2022-02-01 11:22:18 +03:00
|
|
|
cf, err := h.newCodec(ct)
|
|
|
|
if err != nil {
|
|
|
|
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-05 18:31:51 +03:00
|
|
|
var argv, replyv reflect.Value
|
|
|
|
|
|
|
|
// Decode the argument value.
|
|
|
|
argIsValue := false // if true, need to indirect before calling.
|
|
|
|
if hldr.mtype.ArgType.Kind() == reflect.Ptr {
|
|
|
|
argv = reflect.New(hldr.mtype.ArgType.Elem())
|
|
|
|
} else {
|
|
|
|
argv = reflect.New(hldr.mtype.ArgType)
|
|
|
|
argIsValue = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if argIsValue {
|
|
|
|
argv = argv.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
// reply value
|
|
|
|
replyv = reflect.New(hldr.mtype.ReplyType.Elem())
|
|
|
|
|
|
|
|
function := hldr.mtype.method.Func
|
|
|
|
var returnValues []reflect.Value
|
|
|
|
|
2023-05-08 22:23:34 +03:00
|
|
|
if r.Body != nil {
|
|
|
|
var buf []byte
|
|
|
|
buf, err = io.ReadAll(r.Body)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-02-06 18:53:25 +03:00
|
|
|
|
2023-05-08 22:23:34 +03:00
|
|
|
if err = cf.Unmarshal(buf, argv.Interface()); err != nil {
|
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
2023-05-29 12:26:56 +03:00
|
|
|
if len(matches) > 0 {
|
|
|
|
matches = rflutil.FlattenMap(matches)
|
|
|
|
if err = rflutil.Merge(argv.Interface(), matches, rflutil.SliceAppend(true), rflutil.Tags([]string{"protobuf", "json"})); err != nil {
|
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
hr := &rpcRequest{
|
|
|
|
codec: cf,
|
2021-03-09 14:19:59 +03:00
|
|
|
service: handler.sopts.Name,
|
2021-02-05 18:31:51 +03:00
|
|
|
contentType: ct,
|
|
|
|
method: fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name),
|
2023-01-17 23:40:24 +03:00
|
|
|
endpoint: fmt.Sprintf("%s.%s", hldr.name, hldr.mtype.method.Name),
|
2021-02-05 18:31:51 +03:00
|
|
|
payload: argv.Interface(),
|
2021-04-30 00:40:08 +03:00
|
|
|
header: md,
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// define the handler func
|
2021-03-01 13:02:35 +03:00
|
|
|
fn := func(fctx context.Context, req server.Request, rsp interface{}) (err error) {
|
2022-12-27 23:59:02 +03:00
|
|
|
returnValues = function.Call([]reflect.Value{hldr.rcvr, hldr.mtype.prepareContext(fctx), argv, reflect.ValueOf(rsp)})
|
2021-02-05 18:31:51 +03:00
|
|
|
|
|
|
|
// The return value for the method is an error.
|
|
|
|
if rerr := returnValues[0].Interface(); rerr != nil {
|
|
|
|
err = rerr.(error)
|
|
|
|
}
|
|
|
|
|
2021-05-01 01:24:09 +03:00
|
|
|
md, ok := metadata.FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
md = metadata.New(0)
|
2021-03-01 13:02:35 +03:00
|
|
|
}
|
2021-05-01 01:24:09 +03:00
|
|
|
if nmd, ok := metadata.FromOutgoingContext(fctx); ok {
|
|
|
|
for k, v := range nmd {
|
|
|
|
md.Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
metadata.SetOutgoingContext(ctx, md)
|
2021-03-01 13:02:35 +03:00
|
|
|
|
2024-04-23 22:21:27 +03:00
|
|
|
if err != nil && sp != nil {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
|
|
|
}
|
|
|
|
|
2021-02-05 18:31:51 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:21:27 +03:00
|
|
|
h.opts.Hooks.EachNext(func(hook options.Hook) {
|
|
|
|
if h, ok := hook.(server.HookHandler); ok {
|
|
|
|
fn = h(fn)
|
|
|
|
}
|
|
|
|
})
|
2021-02-05 18:31:51 +03:00
|
|
|
|
2021-04-17 13:54:32 +03:00
|
|
|
if ct == "application/x-www-form-urlencoded" {
|
|
|
|
cf, err = h.newCodec(DefaultContentType)
|
|
|
|
if err != nil {
|
|
|
|
h.errorHandler(ctx, handler, w, r, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2021-04-18 15:50:41 +03:00
|
|
|
ct = DefaultContentType
|
2021-04-17 13:54:32 +03:00
|
|
|
}
|
|
|
|
|
2021-05-01 01:24:09 +03:00
|
|
|
scode := int(200)
|
2021-05-13 15:46:47 +03:00
|
|
|
appErr := fn(ctx, hr, replyv.Interface())
|
|
|
|
|
2021-10-26 22:36:04 +03:00
|
|
|
w.Header().Set(metadata.HeaderContentType, ct)
|
2021-05-13 15:46:47 +03:00
|
|
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
|
|
|
for k, v := range md {
|
|
|
|
w.Header().Set(k, v)
|
|
|
|
}
|
|
|
|
}
|
2023-02-11 01:18:16 +03:00
|
|
|
if md := getRspHeader(ctx); md != nil {
|
|
|
|
for k, v := range md {
|
|
|
|
for _, vv := range v {
|
|
|
|
w.Header().Add(k, vv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 22:36:04 +03:00
|
|
|
if nct := w.Header().Get(metadata.HeaderContentType); nct != ct {
|
2021-05-14 14:17:33 +03:00
|
|
|
if cf, err = h.newCodec(nct); err != nil {
|
2021-05-13 15:46:47 +03:00
|
|
|
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 22:23:34 +03:00
|
|
|
var buf []byte
|
2021-05-13 15:46:47 +03:00
|
|
|
if appErr != nil {
|
2021-02-25 14:16:55 +03:00
|
|
|
switch verr := appErr.(type) {
|
|
|
|
case *errors.Error:
|
|
|
|
scode = int(verr.Code)
|
2022-01-22 01:10:24 +03:00
|
|
|
buf, err = cf.Marshal(verr)
|
2021-02-25 14:16:55 +03:00
|
|
|
case *Error:
|
2022-01-22 01:10:24 +03:00
|
|
|
buf, err = cf.Marshal(verr.err)
|
2021-02-25 14:16:55 +03:00
|
|
|
default:
|
2022-01-22 01:10:24 +03:00
|
|
|
buf, err = cf.Marshal(appErr)
|
2021-02-25 14:16:55 +03:00
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
} else {
|
2022-01-22 01:10:24 +03:00
|
|
|
buf, err = cf.Marshal(replyv.Interface())
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
2021-05-13 15:46:47 +03:00
|
|
|
|
2024-10-10 14:29:01 +03:00
|
|
|
if err != nil {
|
|
|
|
if handler.sopts.Logger.V(logger.ErrorLevel) {
|
|
|
|
handler.sopts.Logger.Error(handler.sopts.Context, "handler error", err)
|
|
|
|
}
|
|
|
|
scode = http.StatusInternalServerError
|
|
|
|
} else if nscode := GetRspCode(ctx); nscode != 0 {
|
2021-05-01 01:24:09 +03:00
|
|
|
scode = nscode
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|
2024-10-10 14:29:01 +03:00
|
|
|
|
2021-05-01 01:24:09 +03:00
|
|
|
w.WriteHeader(scode)
|
|
|
|
|
2022-01-22 01:10:24 +03:00
|
|
|
if _, cerr := w.Write(buf); cerr != nil {
|
2024-04-23 22:21:27 +03:00
|
|
|
handler.sopts.Logger.Error(ctx, "respoonse write error", cerr)
|
2021-04-26 00:43:06 +03:00
|
|
|
}
|
2021-02-05 18:31:51 +03:00
|
|
|
}
|