/
plugin.go
241 lines (213 loc) · 7.51 KB
/
plugin.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package web
import (
"context"
"fmt"
"github.com/anthonyraymond/joal-cli/internal/old/core/broadcast"
"github.com/anthonyraymond/joal-cli/internal/old/core/logs"
"github.com/anthonyraymond/joal-cli/internal/old/plugins/types"
"github.com/go-stomp/stomp/v3"
stompServer "github.com/go-stomp/stomp/v3/server"
"github.com/gorilla/mux"
"github.com/rs/cors"
"go.uber.org/zap"
"net"
"net/http"
"net/url"
"nhooyr.io/websocket"
"os"
"path/filepath"
"sync"
"time"
)
type plugin struct {
configLoader *webConfigLoader
staticFilesDir string
coreBridge types.ICoreBridge
stompServer net.Listener
stompPublisher *stomp.Conn
coreListener *appStateCoreListener
httpServer *http.Server
wsListener net.Listener
unregisterListener func()
}
func (w *plugin) Name() string {
return "Web UI"
}
func ShouldEnablePlugin() bool {
for _, arg := range os.Args {
if arg == "--no-webui" {
return false
}
}
return true
}
func BootStrap(pluginsRootDir string, coreBridge types.ICoreBridge, client *http.Client) (types.IJoalPlugin, error) {
configRoot := filepath.Join(pluginsRootDir, "web")
p := &plugin{
configLoader: newWebConfigLoader(configRoot),
staticFilesDir: staticFilesDirFromRoot(configRoot),
coreBridge: coreBridge,
stompServer: nil,
stompPublisher: nil,
coreListener: nil,
httpServer: nil,
wsListener: nil,
unregisterListener: nil,
}
log := logs.GetLogger().With(zap.String("plugin", p.Name()))
err := bootstrap(configRoot, client, log)
if err != nil {
return nil, fmt.Errorf("failed to bootstrap web plugin: %w", err)
}
return p, nil
}
func (w *plugin) Start() error {
log := logs.GetLogger().With(zap.String("plugin", w.Name()))
conf, err := w.configLoader.ReadConfig()
if err != nil {
return err
}
w.coreListener = &appStateCoreListener{
state: state{}.initialState(),
lock: &sync.Mutex{},
}
// Create a listener for the websocket server
// The listener is a faked one, if a client comes to the http websocket negotiation endpoint
// he will be upgraded then be available to the wsListener#Accept() (just like a real net.Listener)
wsListener, err := newWebSocketListener()
if err != nil {
shutdown(w, nil)
return fmt.Errorf("failed to create web stomp listener: %w", err)
}
w.wsListener = wsListener
// Start the stomp server, so it's ready to accept connection as soon as the HTTP server is up
startStompServer(conf.Stomp, w.wsListener, log)
router := mux.NewRouter()
// Register web ui static files endpoint
router.Handle(conf.Http.withSecretPathPrefix(conf.Http.WebUiUrl), webUiStaticFilesHandler(conf.Http.WebUiUrl, w.staticFilesDir)) // TODO: replace with a SPA handler from gorilla/mux documentation
// Register HTTP API
registerApiRoutes(router.PathPrefix(conf.Http.withSecretPathPrefix(conf.Http.HttpApiUrl)).Subrouter(), func() types.ICoreBridge { return w.coreBridge }, func() *state { return w.coreListener.state })
// Register the websocket negotiation endpoint
router.HandleFunc(conf.Http.withSecretPathPrefix(conf.Http.WsNegotiationEndpointUrl), wsListener.HttpNegotiationHandleFunc(conf.WebSocket))
// TODO: move cors somewhere else (maybe in startHttpServer), and move params in config
handler := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{http.MethodOptions, http.MethodGet, http.MethodPost, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodConnect, http.MethodHead, http.MethodTrace},
Debug: false,
}).Handler(router)
// Start Http server
w.httpServer, err = startHttpServer(handler, conf.Http, log)
if err != nil {
shutdown(w, nil)
return err
}
// Create a client connected to our stomp server to be able to dispatch messages
stompPublisher, err := createStompPublisher(conf.Http, conf.WebSocket, conf.Stomp)
if err != nil {
shutdown(w, nil)
return fmt.Errorf("failed to create the stomp publisher: %w", err)
}
w.stompPublisher = stompPublisher
w.coreListener.stompPublisher = stompPublisher
w.unregisterListener = broadcast.RegisterListener(w.coreListener)
return nil
}
func (w *plugin) AfterCoreLoaded(coreBridge types.ICoreBridge) {
w.coreBridge = coreBridge
}
func (w *plugin) Shutdown(ctx context.Context) {
log := logs.GetLogger().With(zap.String("plugin", w.Name()))
log.Info("Shutting down plugin")
shutdown(w, ctx)
}
// a lock free and nil safe version of Shutdown()
func shutdown(w *plugin, ctx context.Context) {
if ctx == nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
}
if w.wsListener != nil {
_ = w.wsListener.Close()
}
if w.httpServer != nil {
if err := w.httpServer.Shutdown(ctx); err != nil {
_ = w.httpServer.Close()
}
}
if w.stompPublisher != nil {
_ = w.stompPublisher.Disconnect()
}
if w.stompServer != nil {
_ = w.stompServer.Close()
}
if w.unregisterListener != nil {
w.unregisterListener()
}
}
func startHttpServer(httpHandler http.Handler, config *httpConfig, log *zap.Logger) (*http.Server, error) {
// Create a listener for the HTTP server
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port))
if err != nil {
return nil, fmt.Errorf("failed to start listenet on port %d: %w", config.Port, err)
}
server := &http.Server{
Handler: httpHandler,
ReadTimeout: config.ReadTimeout,
ReadHeaderTimeout: config.ReadHeaderTimeout,
WriteTimeout: config.WriteTimeout,
IdleTimeout: config.IdleTimeout,
MaxHeaderBytes: config.MaxHeaderBytes,
}
// Starts the Http server
go func() {
if err := server.Serve(listener); err != nil {
if err != http.ErrServerClosed {
log.Error("http server has been closed", zap.Error(err))
}
}
}()
return server, nil
}
func startStompServer(config *stompConfig, wsListener net.Listener, log *zap.Logger) {
go func() {
err := (&stompServer.Server{
Authenticator: config,
HeartBeat: config.HeartBeat,
Log: wrapZapLogger(log),
}).Serve(wsListener)
if err != nil {
log.Error("stomp server has been closed", zap.Error(err))
}
}()
}
func createStompPublisher(httpConf *httpConfig, wsConfig *webSocketConfig, stompConfig *stompConfig) (*stomp.Conn, error) {
negotiationEndpoint, err := url.Parse(fmt.Sprintf("ws://localhost:%d%s", httpConf.Port, httpConf.withSecretPathPrefix(httpConf.WsNegotiationEndpointUrl)))
if err != nil {
return nil, fmt.Errorf("failed to create stomp negotiation endpoint URL: %w", err)
}
c, _, err := websocket.Dial(context.Background(), negotiationEndpoint.String(), &websocket.DialOptions{
Subprotocols: wsConfig.AcceptedSubProtocols,
CompressionMode: 0,
CompressionThreshold: 0,
})
if err != nil {
return nil, fmt.Errorf("failed to connect to local websocket endpoint: %w", err)
}
c.SetReadLimit(int64(wsConfig.MaxReadLimit))
conn, err := stomp.Connect(
websocket.NetConn(context.Background(), c, websocket.MessageText),
stomp.ConnOpt.Login(stompConfig.Login, stompConfig.Password),
stomp.ConnOpt.Host(negotiationEndpoint.Host), // FIXME: this may need an adaptation
stomp.ConnOpt.HeartBeat(stompConfig.HeartBeat, stompConfig.HeartBeat),
stomp.ConnOpt.UseStomp,
)
if err != nil {
_ = c.Close(websocket.StatusGoingAway, "closing websocket because STOMP connect have failed")
return nil, fmt.Errorf("failed to start stomp publisher: %w", err)
}
return conn, nil
}
func webUiStaticFilesHandler(webUiUrl string, staticFilesPath string) http.Handler {
return http.StripPrefix(webUiUrl, http.FileServer(http.Dir(staticFilesPath)))
}