-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
107 lines (95 loc) · 3.85 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package mux
import (
"context"
"net/http"
"github.com/goriller/ginny/middleware"
"github.com/goriller/ginny/server/mux/rewriter"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
// "github.com/grpc-ecosystem/grpc-gateway/v2/utilities"
"google.golang.org/genproto/googleapis/api/httpbody"
"google.golang.org/protobuf/proto"
)
func defaultErrorHandler(ctx context.Context,
mux *runtime.ServeMux, marshaler runtime.Marshaler,
w http.ResponseWriter, r *http.Request, err error,
) {
rewriter.WriteHTTPErrorResponse(w, r, err)
}
// handlerWithMiddleWares handler with middle wares.
func handlerWithMiddleWares(h http.Handler, middleWares ...middleware.MuxMiddleware) http.Handler {
lenMiddleWare := len(middleWares)
for i := lenMiddleWare - 1; i >= 0; i-- {
middleWare := middleWares[i]
h = middleWare(h)
}
return h
}
// // HandlerGRPCService
// func HandlerGRPCService(mux *runtime.ServeMux, server interface{}, v grpc.MethodDesc) func(w http.ResponseWriter,
// r *http.Request, _ map[string]string) {
// return func(w http.ResponseWriter, req *http.Request, _ map[string]string) {
// ctx, cancel := context.WithCancel(req.Context())
// defer cancel()
// var stream runtime.ServerTransportStream
// ctx = grpc.NewContextWithServerTransportStream(ctx, &stream)
// inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
// resp, md, err := handlerGRPCRequest(ctx, inboundMarshaler, server, req, v.MethodName)
// if err != nil {
// runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
// return
// }
// md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer())
// ctx = runtime.NewServerMetadataContext(ctx, md)
// if err != nil {
// runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
// return
// }
// runtime.ForwardResponseMessage(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
// }
// }
// // handlerGRPCRequest
// func handlerGRPCRequest(ctx context.Context, marshaler runtime.Marshaler,
// server interface{}, req *http.Request, methodName string,
// ) (proto.Message, runtime.ServerMetadata, error) {
// var metadata runtime.ServerMetadata
// newReader, berr := utilities.IOReaderFactory(req.Body)
// if berr != nil {
// return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", berr)
// }
// method := reflect.ValueOf(server).MethodByName(methodName)
// if !method.IsValid() {
// return nil, metadata, status.Errorf(codes.Unimplemented, "method %s is is valid", methodName)
// }
// methodType := method.Type()
// if methodType.NumIn() != 2 || methodType.NumOut() != 2 {
// return nil, metadata, status.Errorf(codes.Unimplemented, "method %s may not a unary service", methodName)
// }
// callParams := make([]reflect.Value, 2)
// callParams[0] = reflect.ValueOf(ctx)
// in := reflect.New(methodType.In(1).Elem())
// protoReq := in.Interface()
// if err := marshaler.NewDecoder(newReader()).Decode(protoReq); err != nil && !errors.Is(err, io.EOF) {
// return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
// }
// callParams[1] = in
// resps := method.Call(callParams)
// if err := resps[1].Interface(); err != nil {
// return nil, metadata, err.(error)
// }
// msg := resps[0].Interface()
// return msg.(proto.Message), metadata, nil
// }
// forwardResponseOptionFunc
func forwardResponseOptionFunc(ctx context.Context, w http.ResponseWriter, message proto.Message) error {
if body, ok := message.(*httpbody.HttpBody); ok {
if body.ContentType == typeLocation {
location := string(body.Data)
w.Header().Set(typeLocation, location)
body.ContentType = "text/html; charset=utf-8"
w.Header().Set("Content-Type", body.ContentType)
w.WriteHeader(http.StatusFound)
body.Data = []byte("<a href=\"" + htmlReplacer.Replace(location) + "\">Found</a>.\n")
}
}
return nil
}