-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhandler.go
207 lines (187 loc) · 4.83 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
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
package base
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/gqlerrors"
"github.com/microsvs/base/cmd/discovery"
"github.com/microsvs/base/pkg/env"
"github.com/microsvs/base/pkg/errors"
"github.com/microsvs/base/pkg/log"
"github.com/microsvs/base/pkg/rpc"
"github.com/microsvs/base/pkg/tracing"
"github.com/microsvs/base/pkg/types"
"github.com/microsvs/handler"
opentracing "github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-lib/metrics"
jprom "github.com/uber/jaeger-lib/metrics/prometheus"
"github.com/urfave/negroni"
)
// dns configuration
var BaseDomain string = "api.xhj.com"
var metricsFactory metrics.Factory = jprom.New().Namespace(
func() string {
name, _ := env.Get(env.ServiceName)
return name
}(),
nil,
)
type PHASES string
const (
BEFORE PHASES = "before"
AFTER = "after"
)
type Daemon struct {
service rpc.FGService
extHandlers map[string]http.Handler
schema *graphql.Schema
handler *handler.Handler
middlewares *negroni.Negroni
phasesMap map[PHASES][]http.HandlerFunc
}
// example: "FGError:40011:invalid user"
func customErrorFormat(errs []gqlerrors.FormattedError) interface{} {
if len(errs) <= 0 {
return &types.CustomError{}
}
msg := errs[0].Message
if strings.HasPrefix(msg, errors.FGErrorPrefix) {
fields := strings.SplitN(strings.TrimLeft(msg, errors.FGErrorPrefix), ":", 2)
if len(fields) >= 2 {
code, _ := strconv.Atoi(fields[0])
return &types.CustomError{
ErrCode: code,
ErrMsg: fields[1],
}
}
}
ce := &types.CustomError{
ErrCode: int(errors.FGEInternalError),
ErrMsg: msg,
}
for code, errMsg := range errors.GetErrors() {
if msg == errMsg {
ce.ErrCode = int(code)
break
}
}
return ce
}
func NewGLDaemon(service rpc.FGService, schema *graphql.Schema) (*Daemon, error) {
var d *Daemon
if schema == nil {
return nil, errors.GraphqlObjectIsNull
}
config := handler.NewConfig()
config.Schema = schema
config.HandlerErrorResp = customErrorFormat
d = &Daemon{
service: service,
extHandlers: make(map[string]http.Handler),
schema: schema,
handler: handler.New(config),
middlewares: negroni.New(func() *negroni.Recovery {
recovery := negroni.NewRecovery()
recovery.PrintStack = false
recovery.Formatter = nil
return recovery
}()),
phasesMap: make(map[PHASES][]http.HandlerFunc),
}
// init global tracer
tracer := tracing.Init(
service.String(),
metricsFactory.Namespace(service.String(), nil),
func() string {
agent, _ := env.Get(env.TracerAgent)
return agent
}(),
)
opentracing.SetGlobalTracer(tracer)
return d, nil
}
func (d *Daemon) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var (
ctx context.Context
err error
)
defer func() {
if err != nil {
GLReturnError(err, w)
}
}()
switch getProtocalType(r) {
case rpc.HTTP: // external request
if ctx, err = buildContext(r); err != nil {
// error handler
return
}
ctx = context.WithValue(ctx, rpc.KeyService, d.service.String())
case rpc.RPC: // interval request, between microservices
if ctx, err = rpc.ContextFromHTTPRequest(ctx, r); err != nil {
return
}
}
d.handler.ContextHandler(ctx, w, r)
return
}
func GLReturnError(err error, w http.ResponseWriter) {
tmp := customErrorFormat(
[]gqlerrors.FormattedError{
gqlerrors.FormattedError{
Message: err.Error(),
},
},
)
resp := map[string]interface{}{
"data": nil,
"error": tmp,
}
w.WriteHeader(http.StatusOK)
bts, _ := json.MarshalIndent(resp, "", "\t")
w.Write(bts)
return
}
func Service2Url(service rpc.FGService) string {
host := fmt.Sprintf("dns/%s.%s", service, BaseDomain)
return discovery.KVRead(host, host)
}
// get protocal type from http.Request, http or rpc
func getProtocalType(r *http.Request) rpc.ProtocalType {
tmp := r.Header.Get(fmt.Sprintf("%d", rpc.KeyProtocalType))
if tmp == "" {
return rpc.HTTP
}
return rpc.ProtocalType(tmp)
}
func (d *Daemon) BeforeRouter(middleware ...http.HandlerFunc) {
d.phasesMap[BEFORE] = append([]http.HandlerFunc{}, middleware...)
}
func (d *Daemon) AfterRouter(middleware ...http.HandlerFunc) {
d.phasesMap[AFTER] = append([]http.HandlerFunc{}, middleware...)
}
// generate router handlers
func (d *Daemon) Listen() {
mux := tracing.NewServeMux(opentracing.GlobalTracer())
mux.Handle("/graphql", d)
mux.Handle("/", http.FileServer(assetFS()))
for url, handler := range d.extHandlers {
mux.Handle(url, handler)
}
// before router
for _, fn := range d.phasesMap[BEFORE] {
d.middlewares.UseHandlerFunc(fn)
}
d.middlewares.UseHandler(mux)
// after router
for _, fn := range d.phasesMap[AFTER] {
d.middlewares.UseHandlerFunc(fn)
}
log.InfoRaw("service %s start at %d", d.service.String(), d.service)
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", d.service), d.middlewares)
return
}