-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (151 loc) · 4.07 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"
"time"
pb "grpc_gateway_media_example/pb"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"
)
type ServerInterface interface {
Serve(net.Listener) error
}
func launchServer(server ServerInterface, port int) {
conn, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
fmt.Printf("failed to listen: %v\n", err)
os.Exit(1)
}
err = server.Serve(conn)
if err != nil {
fmt.Printf("Serve on port %d: %v\n", port, err)
os.Exit(1)
}
}
func getFormFile(r *http.Request, name string) ([]byte, error) {
file, _, err := r.FormFile(name)
if err != nil {
return nil, fmt.Errorf("not found")
}
defer file.Close()
buf := bytes.Buffer{}
io.Copy(&buf, file)
if err != nil {
return nil, fmt.Errorf("error while reading form file")
}
return buf.Bytes(), nil
}
func expandMacros(jsonData map[string]interface{}, r *http.Request, res *map[string]interface{}) error {
for name, val := range jsonData {
if inner, isObject := val.(map[string]interface{}); isObject {
(*res)[name] = map[string]interface{}{}
resInner, _ := (*res)[name].(map[string]interface{})
err := expandMacros(inner, r, &resInner)
if err != nil {
return err
}
} else if valStr, ok := val.(string); ok && strings.HasPrefix(valStr, "$") {
data, err := getFormFile(r, valStr)
if err != nil {
return fmt.Errorf("can not get file %s", val.(string))
}
(*res)[name] = base64.StdEncoding.EncodeToString(data)
} else {
(*res)[name] = val
}
}
return nil
}
func createRequestFromMultiPart(r *http.Request) (*http.Request, error) {
json_data, err := getFormFile(r, "data")
if err != nil {
fmt.Printf("%v", err)
return nil, err
}
rawJson := json.RawMessage(json_data)
var decoded map[string]interface{}
expanded := map[string]interface{}{}
json.Unmarshal(rawJson, &decoded)
err = expandMacros(decoded, r, &expanded)
str, _ := json.Marshal(expanded)
if err != nil {
return nil, err
}
reader := bytes.NewReader(str)
newR, err := http.NewRequest(http.MethodPost, r.URL.String(), reader)
if err != nil {
return nil, err
}
return newR, nil
}
func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
grpcServer.ServeHTTP(w, r)
} else {
if strings.Contains(r.Header.Get("Content-Type"), "multipart/form-data") {
newR, err := createRequestFromMultiPart(r)
if err != nil {
w.WriteHeader(400)
return
}
otherHandler.ServeHTTP(w, newR)
} else {
otherHandler.ServeHTTP(w, r)
}
}
})
}
type EchoService struct {
pb.UnimplementedEchoServiceServer
}
func (service *EchoService) Echo(ctx context.Context, in *pb.EchoMessage) (*pb.EchoMessage, error) {
var res *pb.EchoMessage
handler := func() {
time.Sleep(time.Second)
res = in
}
Process(handler, "echo")
return res, nil
}
func Process(callback func(), label string) {
fmt.Printf("got request of type %s\n", label)
start := time.Now()
callback()
fmt.Printf("it took %f seconds to process %s\n", time.Since(start).Seconds(), label)
}
func main() {
grpcServer := grpc.NewServer()
mux := http.NewServeMux()
pb.RegisterEchoServiceServer(grpcServer, &EchoService{})
reflection.Register(grpcServer)
ctx := context.Background()
gwmux := runtime.NewServeMux()
err := pb.RegisterEchoServiceHandlerFromEndpoint(ctx, gwmux, "localhost:9090", []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())})
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
mux.Handle("/v1/echo", gwmux)
// launch grpc server
go launchServer(grpcServer, 9090)
// launch http server redirecting to grpc
srv := &http.Server{
Addr: "0.0.0.0:8080",
Handler: grpcHandlerFunc(grpcServer, mux),
}
go launchServer(srv, 8080)
for {
time.Sleep(time.Second)
}
}