-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver.go
341 lines (294 loc) · 12.7 KB
/
httpserver.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package httpserver
import (
"bufio"
"bytes"
"context"
"embed"
"fmt"
"html/template"
"io/fs"
"net/http"
"os"
"time"
"github.com/docktermj/cloudshell/xtermservice"
"github.com/flowchartsman/swaggerui"
"github.com/pkg/browser"
"github.com/senzing-garage/demo-entity-search/entitysearchservice"
"github.com/senzing-garage/go-observing/observer"
"github.com/senzing-garage/go-rest-api-service-legacy/restapiservicelegacy"
"github.com/senzing-garage/go-rest-api-service/senzingrestapi"
"google.golang.org/grpc"
)
// ----------------------------------------------------------------------------
// Types
// ----------------------------------------------------------------------------
// HttpServerImpl is the default implementation of the HttpServer interface.
type HttpServerImpl struct {
ApiUrlRoutePrefix string // FIXME: Only works with "api"
EnableAll bool
EnableEntitySearch bool
EnableSenzingRestAPI bool
EnableSwaggerUI bool
EnableXterm bool
EntitySearchRoutePrefix string // FIXME: Only works with "entity-search"
GrpcDialOptions []grpc.DialOption
GrpcTarget string
LogLevelName string
ObserverOrigin string
Observers []observer.Observer
OpenApiSpecificationRest []byte
ReadHeaderTimeout time.Duration
SenzingEngineConfigurationJson string
SenzingModuleName string
SenzingVerboseLogging int64
ServerAddress string
ServerOptions []senzingrestapi.ServerOption
ServerPort int
SwaggerUrlRoutePrefix string // FIXME: Only works with "swagger"
TtyOnly bool
XtermAllowedHostnames []string
XtermArguments []string
XtermCommand string
XtermConnectionErrorLimit int
XtermKeepalivePingTimeout int
XtermMaxBufferSizeBytes int
XtermUrlRoutePrefix string // FIXME: Only works with "xterm"
}
type TemplateVariables struct {
HttpServerImpl
ApiServerStatus string
ApiServerUrl string
EntitySearchStatus string
EntitySearchUrl string
HtmlTitle string
RequestHost string
SwaggerStatus string
SwaggerUrl string
XtermStatus string
XtermUrl string
}
// ----------------------------------------------------------------------------
// Variables
// ----------------------------------------------------------------------------
//go:embed static/*
var static embed.FS
// ----------------------------------------------------------------------------
// Internal methods
// ----------------------------------------------------------------------------
func (httpServer *HttpServerImpl) getServerStatus(up bool) string {
result := "red"
if httpServer.EnableAll {
result = "green"
}
if up {
result = "green"
}
return result
}
func (httpServer *HttpServerImpl) getServerUrl(up bool, url string) string {
result := ""
if httpServer.EnableAll {
result = url
}
if up {
result = url
}
return result
}
func (httpServer *HttpServerImpl) openApiFunc(ctx context.Context, openApiSpecification []byte) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var bytesBuffer bytes.Buffer
bufioWriter := bufio.NewWriter(&bytesBuffer)
openApiSpecificationTemplate, err := template.New("OpenApiTemplate").Parse(string(httpServer.OpenApiSpecificationRest))
if err != nil {
panic(err)
}
templateVariables := TemplateVariables{
RequestHost: r.Host,
}
err = openApiSpecificationTemplate.Execute(bufioWriter, templateVariables)
if err != nil {
panic(err)
}
_, err = w.Write(bytesBuffer.Bytes())
if err != nil {
panic(err)
}
}
}
func (httpServer *HttpServerImpl) populateStaticTemplate(responseWriter http.ResponseWriter, request *http.Request, filepath string, templateVariables TemplateVariables) {
templateBytes, err := static.ReadFile(filepath)
if err != nil {
http.Error(responseWriter, http.StatusText(500), 500)
return
}
templateParsed, err := template.New("HtmlTemplate").Parse(string(templateBytes))
if err != nil {
http.Error(responseWriter, http.StatusText(500), 500)
return
}
err = templateParsed.Execute(responseWriter, templateVariables)
if err != nil {
http.Error(responseWriter, http.StatusText(500), 500)
return
}
}
// ----------------------------------------------------------------------------
// Methods for Go-based API server - in development
// ----------------------------------------------------------------------------
// func (httpServer *HttpServerImpl) getSenzingRestApiGenericMux(ctx context.Context, urlRoutePrefix string) *senzingrestapi.Server {
// service := &senzingrestservice.SenzingRestServiceImpl{
// GrpcDialOptions: httpServer.GrpcDialOptions,
// GrpcTarget: httpServer.GrpcTarget,
// LogLevelName: httpServer.LogLevelName,
// ObserverOrigin: httpServer.ObserverOrigin,
// Observers: httpServer.Observers,
// SenzingEngineConfigurationJson: httpServer.SenzingEngineConfigurationJson,
// SenzingModuleName: httpServer.SenzingModuleName,
// SenzingVerboseLogging: httpServer.SenzingVerboseLogging,
// UrlRoutePrefix: urlRoutePrefix,
// OpenApiSpecificationSpec: httpServer.OpenApiSpecificationRest,
// }
// srv, err := senzingrestapi.NewServer(service, httpServer.ServerOptions...)
// if err != nil {
// log.Fatal(err)
// }
// return srv
// }
// func (httpServer *HttpServerImpl) getSenzingRestApiMux(ctx context.Context) *senzingrestapi.Server {
// return httpServer.getSenzingRestApiGenericMux(ctx, "/api")
// }
// func (httpServer *HttpServerImpl) getSenzingApiProxyMux(ctx context.Context) *senzingrestapi.Server {
// return httpServer.getSenzingApiGenericMux(ctx, "/entity-search/api")
// }
// --- http.ServeMux ----------------------------------------------------------
func (httpServer *HttpServerImpl) getSenzingRestApiMux(ctx context.Context) *http.ServeMux {
service := &restapiservicelegacy.RestApiServiceLegacyImpl{
JarFile: "/app/senzing-poc-server.jar",
ProxyTemplate: "http://localhost:8250%s",
CustomTransport: http.DefaultTransport,
}
return service.Handler(ctx)
}
func (httpServer *HttpServerImpl) getSenzingRestApiProxyMux(ctx context.Context) *http.ServeMux {
service := &restapiservicelegacy.RestApiServiceLegacyImpl{
JarFile: "/app/senzing-poc-server.jar",
ProxyTemplate: "http://localhost:8250%s",
CustomTransport: http.DefaultTransport,
}
return service.Handler(ctx)
}
func (httpServer *HttpServerImpl) getEntitySearchMux(ctx context.Context) *http.ServeMux {
service := &entitysearchservice.HttpServiceImpl{}
return service.Handler(ctx)
}
func (httpServer *HttpServerImpl) getSwaggerUiMux(ctx context.Context) *http.ServeMux {
swaggerMux := swaggerui.Handler([]byte{}) // OpenAPI specification handled by openApiFunc()
swaggerFunc := swaggerMux.ServeHTTP
submux := http.NewServeMux()
submux.HandleFunc("/", swaggerFunc)
submux.HandleFunc("/swagger_spec", httpServer.openApiFunc(ctx, httpServer.OpenApiSpecificationRest))
return submux
}
func (httpServer *HttpServerImpl) getXtermMux(ctx context.Context) *http.ServeMux {
xtermService := &xtermservice.XtermServiceImpl{
AllowedHostnames: httpServer.XtermAllowedHostnames,
Arguments: httpServer.XtermArguments,
Command: httpServer.XtermCommand,
ConnectionErrorLimit: httpServer.XtermConnectionErrorLimit,
KeepalivePingTimeout: httpServer.XtermKeepalivePingTimeout,
MaxBufferSizeBytes: httpServer.XtermMaxBufferSizeBytes,
UrlRoutePrefix: httpServer.XtermUrlRoutePrefix,
}
return xtermService.Handler(ctx)
}
// --- Http Funcs -------------------------------------------------------------
func (httpServer *HttpServerImpl) siteFunc(w http.ResponseWriter, r *http.Request) {
templateVariables := TemplateVariables{
HttpServerImpl: *httpServer,
HtmlTitle: "Senzing Tools",
ApiServerStatus: httpServer.getServerStatus(httpServer.EnableSenzingRestAPI),
ApiServerUrl: httpServer.getServerUrl(httpServer.EnableSenzingRestAPI, fmt.Sprintf("http://%s/api", r.Host)),
EntitySearchStatus: httpServer.getServerStatus(httpServer.EnableEntitySearch),
EntitySearchUrl: httpServer.getServerUrl(httpServer.EnableEntitySearch, fmt.Sprintf("http://%s/entity-search", r.Host)),
SwaggerStatus: httpServer.getServerStatus(httpServer.EnableSwaggerUI),
SwaggerUrl: httpServer.getServerUrl(httpServer.EnableSwaggerUI, fmt.Sprintf("http://%s/swagger", r.Host)),
XtermStatus: httpServer.getServerStatus(httpServer.EnableXterm),
XtermUrl: httpServer.getServerUrl(httpServer.EnableXterm, fmt.Sprintf("http://%s/xterm", r.Host)),
}
w.Header().Set("Content-Type", "text/html")
filePath := fmt.Sprintf("static/templates%s", r.RequestURI)
httpServer.populateStaticTemplate(w, r, filePath, templateVariables)
}
// ----------------------------------------------------------------------------
// Interface methods
// ----------------------------------------------------------------------------
/*
The Serve method simply prints the 'Something' value in the type-struct.
Input
- ctx: A context to control lifecycle.
Output
- Nothing is returned, except for an error. However, something is printed.
See the example output.
*/
func (httpServer *HttpServerImpl) Serve(ctx context.Context) error {
rootMux := http.NewServeMux()
var userMessage string = ""
// Enable Senzing HTTP REST API.
if httpServer.EnableAll || httpServer.EnableSenzingRestAPI {
senzingApiMux := httpServer.getSenzingRestApiMux(ctx)
rootMux.Handle(fmt.Sprintf("/%s/", httpServer.ApiUrlRoutePrefix), http.StripPrefix("/api", senzingApiMux))
userMessage = fmt.Sprintf("%sServing Senzing REST API at http://localhost:%d/%s\n", userMessage, httpServer.ServerPort, httpServer.ApiUrlRoutePrefix)
}
// Enable Senzing HTTP REST API as reverse proxy.
if httpServer.EnableAll || httpServer.EnableSenzingRestAPI || httpServer.EnableEntitySearch {
senzingApiProxyMux := httpServer.getSenzingRestApiProxyMux(ctx)
rootMux.Handle("/entity-search/api/", http.StripPrefix("/entity-search/api", senzingApiProxyMux))
userMessage = fmt.Sprintf("%sServing Senzing REST API Reverse Proxy at http://localhost:%d/%s\n", userMessage, httpServer.ServerPort, "entity-search/api")
}
// Enable Senzing Entity Search.
if httpServer.EnableAll || httpServer.EnableEntitySearch {
entitySearchMux := httpServer.getEntitySearchMux(ctx)
rootMux.Handle(fmt.Sprintf("/%s/", httpServer.EntitySearchRoutePrefix), http.StripPrefix("/entity-search", entitySearchMux))
userMessage = fmt.Sprintf("%sServing Entity Search at http://localhost:%d/%s\n", userMessage, httpServer.ServerPort, httpServer.EntitySearchRoutePrefix)
}
// Enable SwaggerUI.
if httpServer.EnableAll || httpServer.EnableSwaggerUI {
swaggerUiMux := httpServer.getSwaggerUiMux(ctx)
rootMux.Handle(fmt.Sprintf("/%s/", httpServer.SwaggerUrlRoutePrefix), http.StripPrefix("/swagger", swaggerUiMux))
userMessage = fmt.Sprintf("%sServing SwaggerUI at http://localhost:%d/%s\n", userMessage, httpServer.ServerPort, httpServer.SwaggerUrlRoutePrefix)
}
// Enable Xterm.
if httpServer.EnableAll || httpServer.EnableXterm {
err := os.Setenv("SENZING_ENGINE_CONFIGURATION_JSON", httpServer.SenzingEngineConfigurationJson)
if err != nil {
panic(err)
}
xtermMux := httpServer.getXtermMux(ctx)
rootMux.Handle(fmt.Sprintf("/%s/", httpServer.XtermUrlRoutePrefix), http.StripPrefix("/xterm", xtermMux))
userMessage = fmt.Sprintf("%sServing XTerm at http://localhost:%d/%s\n", userMessage, httpServer.ServerPort, httpServer.XtermUrlRoutePrefix)
}
// Add route to template pages.
rootMux.HandleFunc("/site/", httpServer.siteFunc)
userMessage = fmt.Sprintf("%sServing Console at http://localhost:%d\n", userMessage, httpServer.ServerPort)
// Add route to static files.
rootDir, err := fs.Sub(static, "static/root")
if err != nil {
panic(err)
}
rootMux.Handle("/", http.StripPrefix("/", http.FileServer(http.FS(rootDir))))
// Start service.
listenOnAddress := fmt.Sprintf("%s:%v", httpServer.ServerAddress, httpServer.ServerPort)
userMessage = fmt.Sprintf("%sStarting server on interface:port '%s'...\n", userMessage, listenOnAddress)
fmt.Println(userMessage)
server := http.Server{
ReadHeaderTimeout: httpServer.ReadHeaderTimeout,
Addr: listenOnAddress,
Handler: rootMux,
}
// Start a web browser. Unless disabled.
if !httpServer.TtyOnly {
_ = browser.OpenURL(fmt.Sprintf("http://localhost:%d", httpServer.ServerPort))
}
return server.ListenAndServe()
}