-
Notifications
You must be signed in to change notification settings - Fork 180
/
resolver.go
166 lines (146 loc) · 4.46 KB
/
resolver.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
package http
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"strings"
"google.golang.org/grpc"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/client"
clientcontext "github.com/pydio/cells/v4/common/client/context"
grpc2 "github.com/pydio/cells/v4/common/client/grpc"
"github.com/pydio/cells/v4/common/log"
pb "github.com/pydio/cells/v4/common/proto/registry"
"github.com/pydio/cells/v4/common/proto/rest"
"github.com/pydio/cells/v4/common/registry"
"github.com/pydio/cells/v4/common/runtime"
"github.com/pydio/cells/v4/common/server"
"github.com/pydio/cells/v4/common/server/caddy/maintenance"
servercontext "github.com/pydio/cells/v4/common/server/context"
json "github.com/pydio/cells/v4/common/utils/jsonx"
)
var grpcTransport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
ForceAttemptHTTP2: true,
}
type Resolver interface {
ServeHTTP(w http.ResponseWriter, r *http.Request) (bool, error)
Init(ctx context.Context, s server.HttpMux)
Stop()
}
func NewResolver() Resolver {
return &resolver{}
}
type resolver struct {
c grpc.ClientConnInterface
r registry.Registry
s server.HttpMux
b Balancer
rc client.ResolverCallback
monitor grpc2.HealthMonitor
userReady bool
}
func (m *resolver) Init(ctx context.Context, s server.HttpMux) {
conn := clientcontext.GetClientConn(ctx)
reg := servercontext.GetRegistry(ctx)
rc, _ := client.NewResolverCallback(reg)
bal := NewBalancer()
rc.Add(bal.Build)
m.c = conn
m.rc = rc
m.r = reg
m.s = s
m.b = bal
if runtime.LastInitType() != "install" {
monitor := grpc2.NewHealthChecker(ctx)
go monitor.Monitor(common.ServiceOAuth)
m.monitor = monitor
}
}
func (m *resolver) Stop() {
if m.rc != nil {
m.rc.Stop()
}
if m.monitor != nil {
m.monitor.Stop()
}
}
func (m *resolver) ServeHTTP(w http.ResponseWriter, r *http.Request) (bool, error) {
// Special case for application/grpc
if strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
proxy, e := m.b.PickService(common.ServiceGatewayGrpc)
if e != nil {
http.NotFound(w, r)
return false, fmt.Errorf("cannot find grpc gateway")
}
// We assume that internally, the GRPCs service is serving self-signed
proxy.Transport = grpcTransport
// Wrap context and server request
ctx := clientcontext.WithClientConn(r.Context(), m.c)
ctx = servercontext.WithRegistry(ctx, m.r)
proxy.ServeHTTP(w, r.WithContext(ctx))
return true, nil
}
if m.monitor != nil && (!m.monitor.Up() || !m.userServiceReady()) {
var bb []byte
if strings.Contains(r.Header.Get("Accept"), "text/html") {
if !m.monitor.Up() {
log.Logger(r.Context()).Warn("Returning server is starting because grpc.oauth monitor is not Up")
} else {
log.Logger(r.Context()).Warn("Returning server is starting because grpc.user service is not ready")
}
bb, _ = maintenance.Assets.ReadFile("starting.html")
w.Header().Set("Content-Type", "text/html")
} else {
er := &rest.Error{
Code: "503",
Title: "Server is starting",
Detail: "Server is starting, please retry later",
}
bb, _ = json.Marshal(er)
w.Header().Set("Content-Type", "application/json")
}
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bb)))
w.Header().Set("Retry-After", "10")
w.WriteHeader(503)
_, er := w.Write(bb)
return true, er
}
if r.RequestURI == "/maintenance.html" && r.Header.Get("X-Maintenance-Redirect") != "" {
bb, _ := maintenance.Assets.ReadFile("maintenance.html")
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bb)))
w.WriteHeader(503)
_, er := w.Write(bb)
return true, er
}
// try to find it in the current mux
_, pattern := m.s.Handler(r)
if len(pattern) > 0 && (pattern != "/" || r.URL.Path == "/") {
ctx := clientcontext.WithClientConn(r.Context(), m.c)
ctx = servercontext.WithRegistry(ctx, m.r)
m.s.ServeHTTP(w, r.WithContext(ctx))
return true, nil
}
proxy, e := m.b.PickEndpoint(r.URL.Path)
if e == nil {
proxy.ServeHTTP(w, r)
return true, nil
}
return false, nil
}
func (m *resolver) userServiceReady() bool {
if m.userReady {
return true
}
/// Detect service grpc.user is ready
if ss, e := m.r.List(
registry.WithName(common.ServiceGrpcNamespace_+common.ServiceUser),
registry.WithType(pb.ItemType_SERVICE),
registry.WithMeta(registry.MetaStatusKey, string(registry.StatusReady))); e == nil && len(ss) > 0 {
m.userReady = true
return true
}
return false
}