From 3bc300ef55dbc6bb8c9ab1999783ba702c1a1b7a Mon Sep 17 00:00:00 2001 From: Moby von Briesen Date: Thu, 8 Jun 2023 17:53:24 -0400 Subject: [PATCH] satellite/console: optionally serve vuetify POC from satellite This change slightly modifies the logic for serving the vuetify frontend. After this change, if the config `console.use-vuetify-project` is set to `true`, the Vuetify UI will be served at the `/vuetifypoc` prefix. The POC is only project dashboard right now; the existing login/registration pages must be used, but once the cookie is set, the POC will work correctly. Change-Id: I7725f23a0d2b04f274bab36d8be3370116687d1b --- satellite/console/consoleweb/server.go | 49 +++++++++++++++---- web/satellite/vuetify-poc/src/router/index.ts | 2 +- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/satellite/console/consoleweb/server.go b/satellite/console/consoleweb/server.go index 02cf41bfd..2dbe2fb0e 100644 --- a/satellite/console/consoleweb/server.go +++ b/satellite/console/consoleweb/server.go @@ -363,6 +363,9 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, oidc slashRouter.HandleFunc("/activation", server.accountActivationHandler) slashRouter.HandleFunc("/cancel-password-recovery", server.cancelPasswordRecoveryHandler) + if server.config.UseVuetifyProject { + router.PathPrefix("/vuetifypoc").Handler(http.HandlerFunc(server.vuetifyAppHandler)) + } router.PathPrefix("/").Handler(http.HandlerFunc(server.appHandler)) } @@ -415,8 +418,8 @@ func (server *Server) Close() error { return server.server.Close() } -// appHandler is web app http handler function. -func (server *Server) appHandler(w http.ResponseWriter, r *http.Request) { +// setAppHeaders sets the necessary headers for requests to the app. +func (server *Server) setAppHeaders(w http.ResponseWriter, r *http.Request) { header := w.Header() if server.config.CSPEnabled { @@ -438,14 +441,13 @@ func (server *Server) appHandler(w http.ResponseWriter, r *http.Request) { header.Set(contentType, "text/html; charset=UTF-8") header.Set("X-Content-Type-Options", "nosniff") header.Set("Referrer-Policy", "same-origin") // Only expose the referring url when navigating around the satellite itself. +} - var path string - if server.config.UseVuetifyProject { - path = filepath.Join(server.config.StaticDir, "dist_vuetify_poc", "index-vuetify.html") - } else { - path = filepath.Join(server.config.StaticDir, "dist", "index.html") - } +// appHandler is web app http handler function. +func (server *Server) appHandler(w http.ResponseWriter, r *http.Request) { + server.setAppHeaders(w, r) + path := filepath.Join(server.config.StaticDir, "dist", "index.html") file, err := os.Open(path) if err != nil { if errors.Is(err, fs.ErrNotExist) { @@ -453,7 +455,6 @@ func (server *Server) appHandler(w http.ResponseWriter, r *http.Request) { } else { server.log.Error("error loading index.html", zap.String("path", path), zap.Error(err)) } - // Loading index is optional. return } @@ -472,6 +473,36 @@ func (server *Server) appHandler(w http.ResponseWriter, r *http.Request) { http.ServeContent(w, r, path, info.ModTime(), file) } +// vuetifyAppHandler is web app http handler function. +func (server *Server) vuetifyAppHandler(w http.ResponseWriter, r *http.Request) { + server.setAppHeaders(w, r) + + path := filepath.Join(server.config.StaticDir, "dist_vuetify_poc", "index-vuetify.html") + file, err := os.Open(path) + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + server.log.Error("index-vuetify.html was not generated. run 'npm run build-vuetify' in the "+server.config.StaticDir+" directory", zap.Error(err)) + } else { + server.log.Error("error loading index-vuetify.html", zap.String("path", path), zap.Error(err)) + } + return + } + + defer func() { + if err := file.Close(); err != nil { + server.log.Error("error closing index-vuetify.html", zap.String("path", path), zap.Error(err)) + } + }() + + info, err := file.Stat() + if err != nil { + server.log.Error("failed to retrieve index-vuetify.html file info", zap.Error(err)) + return + } + + http.ServeContent(w, r, path, info.ModTime(), file) +} + // withAuth performs initial authorization before every request. func (server *Server) withAuth(handler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/web/satellite/vuetify-poc/src/router/index.ts b/web/satellite/vuetify-poc/src/router/index.ts index 0ebb2326a..3e43b698f 100644 --- a/web/satellite/vuetify-poc/src/router/index.ts +++ b/web/satellite/vuetify-poc/src/router/index.ts @@ -5,7 +5,7 @@ import { createRouter, createWebHistory } from 'vue-router'; const routes = [ { - path: '/', + path: '/vuetifypoc', redirect: { path: '/dashboard' }, component: () => import('@poc/layouts/Default.vue'), children: [