Skip to content

Commit dd5eb76

Browse files
committed
Cache console index page.
1 parent 6af017a commit dd5eb76

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
nakama
22
.cookie
33

4-
console/ui/dist
5-
64
# Created by https://www.toptal.com/developers/gitignore/api/go,angular,node,intellij+all,visualstudiocode,visualstudio,sublimetext,windows,linux,macos
75
# Edit at https://www.toptal.com/developers/gitignore?templates=go,angular,node,intellij+all,visualstudiocode,visualstudio,sublimetext,windows,linux,macos
86

server/console.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ func StartConsoleServer(logger *zap.Logger, startupLogger *zap.Logger, db *sql.D
325325
// Allow GRPC Gateway to handle the request.
326326
handlerWithMaxBody.ServeHTTP(w, r)
327327
})
328-
registerDashboardHandlers(logger, grpcGatewayRouter)
328+
if err := registerDashboardHandlers(logger, grpcGatewayRouter); err != nil {
329+
startupLogger.Fatal("Console dashboard registration failed", zap.Error(err))
330+
}
329331

330332
// Enable CORS on all requests.
331333
CORSHeaders := handlers.AllowedHeaders([]string{"Authorization", "Content-Type", "User-Agent"})
@@ -414,27 +416,24 @@ SELECT collection FROM t WHERE collection IS NOT NULL`
414416
return s
415417
}
416418

417-
func registerDashboardHandlers(logger *zap.Logger, router *mux.Router) {
418-
indexFn := func(w http.ResponseWriter, r *http.Request) {
419-
indexFile, err := console.UIFS.Open("index.html")
420-
if err != nil {
421-
logger.Error("Failed to open index file.", zap.Error(err))
422-
w.WriteHeader(http.StatusNotFound)
423-
return
424-
}
425-
426-
// inject variables into the index.html file
427-
indexBytes, err := io.ReadAll(indexFile)
428-
if err != nil {
429-
logger.Error("Failed to read index file.", zap.Error(err))
430-
w.WriteHeader(http.StatusNotFound)
431-
return
432-
}
433-
indexFile.Close()
434-
indexHTMLStr := string(indexBytes)
435-
indexHTMLStr = strings.ReplaceAll(indexHTMLStr, "{{nt}}", strconv.FormatBool(console.UIFS.Nt))
436-
indexBytes = []byte(indexHTMLStr)
419+
func registerDashboardHandlers(logger *zap.Logger, router *mux.Router) error {
420+
indexFile, err := console.UIFS.Open("index.html")
421+
if err != nil {
422+
logger.Error("Failed to open index file.", zap.Error(err))
423+
return err
424+
}
425+
// inject variables into the index.html file
426+
indexBytes, err := io.ReadAll(indexFile)
427+
if err != nil {
428+
logger.Error("Failed to read index file.", zap.Error(err))
429+
return err
430+
}
431+
_ = indexFile.Close()
432+
indexHTMLStr := string(indexBytes)
433+
indexHTMLStr = strings.ReplaceAll(indexHTMLStr, "{{nt}}", strconv.FormatBool(console.UIFS.Nt))
434+
indexBytes = []byte(indexHTMLStr)
437435

436+
indexFn := func(w http.ResponseWriter, r *http.Request) {
438437
w.Header().Add("Cache-Control", "no-cache")
439438
w.Header().Set("X-Frame-Options", "deny")
440439
_, _ = w.Write(indexBytes)
@@ -482,6 +481,8 @@ func registerDashboardHandlers(logger *zap.Logger, router *mux.Router) {
482481
indexFn(w, r)
483482
}
484483
})
484+
485+
return nil
485486
}
486487

487488
func (s *ConsoleServer) Stop() {

0 commit comments

Comments
 (0)