-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
169 lines (159 loc) · 4.73 KB
/
proxy.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
/*
* Copyright 2023 Wang Min Xiang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package proxies
import (
"fmt"
"github.com/aacfactory/errors"
"github.com/aacfactory/fns/clusters"
"github.com/aacfactory/fns/context"
"github.com/aacfactory/fns/runtime"
"github.com/aacfactory/fns/services"
"github.com/aacfactory/fns/transports"
"github.com/aacfactory/logs"
)
type ProxyOptions struct {
Log logs.Logger
Config Config
Runtime *runtime.Runtime
Manager clusters.ClusterEndpointsManager
Dialer transports.Dialer
}
type Proxy interface {
Construct(options ProxyOptions) (err error)
Port() int
Run(ctx context.Context) (err error)
Shutdown(ctx context.Context)
}
func New(options ...Option) (p Proxy, err error) {
opt := Options{
transport: nil,
middlewares: make([]transports.Middleware, 0, 1),
handlers: make([]transports.MuxHandler, 0, 1),
}
for _, option := range options {
optErr := option(&opt)
if optErr != nil {
err = errors.Warning("fns: new proxy failed").WithCause(optErr)
return
}
}
if opt.transport == nil {
err = errors.Warning("fns: new proxy failed").WithCause(fmt.Errorf("transport is required"))
return
}
p = &proxy{
log: nil,
transport: opt.transport,
middlewares: opt.middlewares,
handlers: opt.handlers,
}
return
}
type proxy struct {
log logs.Logger
transport transports.Transport
middlewares transports.Middlewares
handlers []transports.MuxHandler
}
func (p *proxy) Construct(options ProxyOptions) (err error) {
p.log = options.Log
// config
config := options.Config
// builtins
builtins := make([]services.Service, 0, 1)
// middlewares
middlewares := make([]transports.Middleware, 0, 1)
middlewares = append(middlewares, runtime.Middleware(options.Runtime))
var corsMiddleware transports.Middleware
for _, middleware := range p.middlewares {
builtin, isBuiltin := middleware.(services.Middleware)
if isBuiltin {
builtins = append(builtins, builtin.Services()...)
}
if middleware.Name() == "cors" {
corsMiddleware = middleware
continue
}
middlewares = append(middlewares, middleware)
}
if corsMiddleware != nil {
middlewares = append([]transports.Middleware{corsMiddleware}, middlewares...)
}
middleware, middlewareErr := transports.WaveMiddlewares(p.log, config.Config, middlewares)
if middlewareErr != nil {
panic(fmt.Errorf("%+v", errors.Warning("fns: new application failed, new transport middleware failed").WithCause(middlewareErr)))
return
}
p.middlewares = middleware
// handlers
mux := transports.NewMux()
for _, handler := range p.handlers {
handlerConfig, handlerConfigErr := config.HandlerConfig(handler.Name())
if handlerConfigErr != nil {
err = errors.Warning("fns: construct proxy failed, new transport handler failed").WithCause(handlerConfigErr).WithMeta("handler", handler.Name())
return
}
handlerErr := handler.Construct(transports.MuxHandlerOptions{
Log: p.log.With("handler", handler.Name()),
Config: handlerConfig,
})
if handlerErr != nil {
err = errors.Warning("fns: construct proxy failed, new transport handler failed").WithCause(handlerErr).WithMeta("handler", handler.Name())
return
}
mux.Add(handler)
builtin, isBuiltin := handler.(services.MuxHandler)
if isBuiltin {
builtins = append(builtins, builtin.Services()...)
}
}
mux.Add(NewProxyHandler(options.Manager, options.Dialer))
// transport
transportErr := p.transport.Construct(transports.Options{
Log: p.log.With("transport", p.transport.Name()),
Config: config.Config,
Handler: middleware.Handler(mux),
})
if transportErr != nil {
err = errors.Warning("fns: construct proxy failed, new transport failed").WithCause(transportErr)
return
}
for _, builtin := range builtins {
err = options.Manager.Add(builtin)
if err != nil {
err = errors.Warning("fns: construct proxy failed, deploy service failed").WithCause(err)
return
}
}
return
}
func (p *proxy) Port() int {
return p.transport.Port()
}
func (p *proxy) Run(_ context.Context) (err error) {
err = p.transport.ListenAndServe()
if err != nil {
err = errors.Warning("fns: proxy run failed").WithCause(err)
return
}
return
}
func (p *proxy) Shutdown(ctx context.Context) {
p.middlewares.Close()
p.transport.Shutdown(ctx)
return
}