-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
214 lines (200 loc) · 5.27 KB
/
server.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package swaggerapi
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"sort"
"strings"
"sync"
"github.com/ml444/swaggerapi/generator"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
)
// Server is api meta server
type Server struct {
lock sync.Mutex
services map[string]*descriptorpb.FileDescriptorSet
methods map[string][]string
opts []generator.Option
}
// NewServer create server instance
func NewServer(opts ...generator.Option) *Server {
return &Server{
services: make(map[string]*descriptorpb.FileDescriptorSet),
methods: make(map[string][]string),
opts: opts,
}
}
type ServicesReply struct {
Services []string `json:"services,omitempty"`
Methods []string `json:"methods,omitempty"`
}
func (s *Server) load() error {
if len(s.services) > 0 {
return nil
}
var err error
protoregistry.GlobalFiles.RangeFiles(func(fd protoreflect.FileDescriptor) bool {
if fd.Services() == nil {
return true
}
for i := 0; i < fd.Services().Len(); i++ {
svc := fd.Services().Get(i)
fdp, e := fileDescriptorProto(fd.Path())
if e != nil {
err = e
return false
}
fdps, e := allDependency(fdp)
if e != nil {
if errors.Is(e, protoregistry.NotFound) {
// Skip this service if one of its dependencies is not found.
continue
}
err = e
return false
}
s.services[string(svc.FullName())] = &descriptorpb.FileDescriptorSet{File: fdps}
if svc.Methods() == nil {
continue
}
for j := 0; j < svc.Methods().Len(); j++ {
method := svc.Methods().Get(j)
s.methods[string(svc.FullName())] = append(s.methods[string(svc.FullName())], string(method.Name()))
}
}
return true
})
return err
}
// ListServices return all services
func (s *Server) ListServices(w http.ResponseWriter, _ *http.Request) {
s.lock.Lock()
defer s.lock.Unlock()
if err := s.load(); err != nil {
response(w, http.StatusInternalServerError, err)
return
}
reply := &ServicesReply{
Services: make([]string, 0, len(s.services)),
Methods: make([]string, 0, len(s.methods)),
}
for name := range s.services {
reply.Services = append(reply.Services, name)
}
for name, methods := range s.methods {
for _, method := range methods {
reply.Methods = append(reply.Methods, fmt.Sprintf("/%s/%s", name, method))
}
}
sort.Strings(reply.Services)
sort.Strings(reply.Methods)
buf, err := json.Marshal(reply)
if err != nil {
response(w, http.StatusInternalServerError, err)
return
}
response(w, http.StatusOK, buf)
return
}
// GetServiceDesc return service meta by name
func (s *Server) GetServiceDesc(w http.ResponseWriter, r *http.Request) {
s.lock.Lock()
defer s.lock.Unlock()
if err := s.load(); err != nil {
if _, e := w.Write([]byte(err.Error())); e != nil {
println(e.Error())
}
return
}
name := ""
raws := strings.Split(r.URL.Path, "/")
if rawsLen := len(raws); rawsLen > 0 {
name = raws[rawsLen-1]
}
fds, ok := s.services[name]
if !ok {
response(w, http.StatusNotFound, fmt.Errorf("service %s not found", name))
return
}
files := fds.File
var target string
if len(files) == 0 {
response(w, http.StatusNotFound, fmt.Errorf("proto file is empty"))
return
}
if files[len(files)-1].Name == nil {
response(w, http.StatusNotFound, fmt.Errorf("proto file's name is null"))
return
}
target = *files[len(files)-1].Name
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
req := new(pluginpb.CodeGeneratorRequest)
req.FileToGenerate = []string{target}
var para = ""
req.Parameter = ¶
req.ProtoFile = files
g := generator.NewGenerator(s.opts...)
resp, err := g.Gen(req)
if err != nil {
response(w, http.StatusInternalServerError, err)
return
}
if len(resp.File) == 0 || resp.File[0].Content == nil {
response(w, http.StatusOK, "{}")
return
}
response(w, http.StatusOK, *resp.File[0].Content)
return
}
func response(w http.ResponseWriter, status int, v interface{}) {
if status < 0 {
status = http.StatusInternalServerError
} else if status == 0 {
status = http.StatusOK
}
w.WriteHeader(status)
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
switch v.(type) {
case error:
if _, e := w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, v))); e != nil {
println(e.Error())
}
case string:
if _, e := w.Write([]byte(v.(string))); e != nil {
println(e.Error())
}
case []byte:
if _, e := w.Write(v.([]byte)); e != nil {
println(e.Error())
}
}
}
func allDependency(fd *descriptorpb.FileDescriptorProto) ([]*descriptorpb.FileDescriptorProto, error) {
var files []*descriptorpb.FileDescriptorProto
for _, dep := range fd.Dependency {
fdDep, err := fileDescriptorProto(dep)
if err != nil {
continue
}
temp, err := allDependency(fdDep)
if err != nil {
return nil, err
}
files = append(files, temp...)
}
files = append(files, fd)
return files, nil
}
func fileDescriptorProto(path string) (*descriptorpb.FileDescriptorProto, error) {
fd, err := protoregistry.GlobalFiles.FindFileByPath(path)
if err != nil {
return nil, fmt.Errorf("find proto by path failed, path: %s, err: %s", path, err)
}
fdpb := protodesc.ToFileDescriptorProto(fd)
return fdpb, nil
}