Merge branch '2022.3.x-feature-web-console' into 2022.3.x
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
webconsole/dist
|
||||
webconsole/node_modules
|
||||
.idea
|
||||
.dev
|
||||
build
|
||||
|
||||
@@ -170,6 +170,26 @@
|
||||
# Default: <no value>
|
||||
# HTTP_SSL_TERMINATED=<no value>
|
||||
|
||||
###############################################################################
|
||||
# Enable web console. When running in dev environment, web console is enabled by default.
|
||||
# Type: bool
|
||||
# Default: <no value>
|
||||
# HTTP_SERVER_WEB_CONSOLE_ENABLED=<no value>
|
||||
|
||||
###############################################################################
|
||||
# Username for the web console endpoint.
|
||||
# Type: string
|
||||
# Default: admin
|
||||
# HTTP_SERVER_WEB_CONSOLE_USERNAME=admin
|
||||
|
||||
###############################################################################
|
||||
# Password for the web console endpoint. When running in dev environment, password is not required.
|
||||
#
|
||||
# Corteza intentionally sets default password to random chars to prevent security incidents.
|
||||
# Type: string
|
||||
# Default: <no value>
|
||||
# HTTP_SERVER_WEB_CONSOLE_PASSWORD=<no value>
|
||||
|
||||
###############################################################################
|
||||
###############################################################################
|
||||
# RBAC options
|
||||
|
||||
@@ -25,10 +25,33 @@ jobs:
|
||||
restore-keys: ${{ runner.os }}-go-
|
||||
- run: make test.all
|
||||
|
||||
release-linux:
|
||||
|
||||
build-web-console:
|
||||
runs-on: ubuntu-latest
|
||||
needs:
|
||||
- test
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/setup-node@v2
|
||||
with:
|
||||
node-version: '16'
|
||||
- uses: actions/cache@v2
|
||||
if: ${{ !env.ACT }}
|
||||
with:
|
||||
path: ~/.npm
|
||||
key: ${{ runner.OS }}-node-${{ hashFiles('**/yarn.lock') }}
|
||||
restore-keys: ${{ runner.OS }}-node-
|
||||
- name: Install dependencies
|
||||
working-directory: ./webconsole
|
||||
run: yarn install
|
||||
- name: Build Package
|
||||
working-directory: ./webconsole
|
||||
run: yarn build
|
||||
|
||||
release-linux:
|
||||
runs-on: ubuntu-latest
|
||||
needs:
|
||||
- build-web-console
|
||||
env:
|
||||
BUILD_OS: linux
|
||||
BUILD_ARCH: amd64
|
||||
@@ -47,7 +70,7 @@ jobs:
|
||||
release-darwin:
|
||||
runs-on: macos-latest
|
||||
needs:
|
||||
- test
|
||||
- build-web-console
|
||||
env:
|
||||
BUILD_OS: darwin
|
||||
BUILD_ARCH: amd64
|
||||
@@ -73,7 +96,7 @@ jobs:
|
||||
release-docker:
|
||||
runs-on: ubuntu-latest
|
||||
needs:
|
||||
- test
|
||||
- build-web-console
|
||||
- release-linux
|
||||
env:
|
||||
BUILD_OS: linux
|
||||
|
||||
@@ -1,3 +1,12 @@
|
||||
# bundle web-console
|
||||
FROM node:16.14-alpine as webconsole-build-stage
|
||||
|
||||
WORKDIR /webconsole
|
||||
COPY ./webconsole ./
|
||||
# Snapshot is built in development mode and with source map
|
||||
RUN yarn install && yarn build --mode dev --sourcemap
|
||||
|
||||
|
||||
# build server
|
||||
FROM golang:1.17-buster as server-build-stage
|
||||
|
||||
@@ -9,6 +18,7 @@ WORKDIR /corteza
|
||||
|
||||
COPY . ./
|
||||
|
||||
COPY --from=webconsole-build-stage /webconsole/dist ./webconsole/dist
|
||||
RUN make release-clean release
|
||||
|
||||
|
||||
|
||||
+14
-11
@@ -42,20 +42,23 @@ func (app *CortezaApp) InitCLI() {
|
||||
// loaded at this point!
|
||||
app.Opt = options.Init()
|
||||
|
||||
app.Log.Warn("loading plugins", zap.String("paths", app.Opt.Plugins.Paths))
|
||||
if app.Opt.Plugins.Enabled {
|
||||
var paths []string
|
||||
paths, err = plugin.Resolve(app.Opt.Plugins.Paths)
|
||||
{
|
||||
log := app.Log.Named("plugins")
|
||||
if app.Opt.Plugins.Enabled && len(app.Opt.Plugins.Paths) > 0 {
|
||||
log.Warn("loading", zap.String("paths", app.Opt.Plugins.Paths))
|
||||
|
||||
app.Log.Warn("loading plugins", zap.Strings("paths", paths))
|
||||
var paths []string
|
||||
paths, err = plugin.Resolve(app.Opt.Plugins.Paths)
|
||||
log.Warn("loading", zap.Strings("resolved-paths", paths))
|
||||
|
||||
app.plugins, err = plugin.Load(paths...)
|
||||
if err != nil {
|
||||
return err
|
||||
app.plugins, err = plugin.Load(paths...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// Empty set of plugins
|
||||
app.plugins = plugin.Set{}
|
||||
}
|
||||
} else {
|
||||
// Empty set of plugins
|
||||
app.plugins = plugin.Set{}
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
@@ -119,5 +119,23 @@ HTTPServer: schema.#optionsGroup & {
|
||||
"""
|
||||
env: "HTTP_SSL_TERMINATED"
|
||||
}
|
||||
|
||||
web_console_enabled: {
|
||||
type: "bool"
|
||||
defaultGoExpr: "false"
|
||||
description: "Enable web console. When running in dev environment, web console is enabled by default."
|
||||
}
|
||||
web_console_username: {
|
||||
defaultValue: "admin"
|
||||
description: "Username for the web console endpoint."
|
||||
}
|
||||
web_console_password: {
|
||||
defaultGoExpr: "string(rand.Bytes(32))"
|
||||
description: """
|
||||
Password for the web console endpoint. When running in dev environment, password is not required.
|
||||
|
||||
Corteza intentionally sets default password to random chars to prevent security incidents.
|
||||
"""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,11 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/corredor"
|
||||
"github.com/cortezaproject/corteza-server/pkg/eventbus"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"runtime"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
@@ -22,8 +21,13 @@ func debugRoutes(r chi.Routes) http.HandlerFunc {
|
||||
if route.SubRoutes != nil && len(route.SubRoutes.Routes()) > 0 {
|
||||
printRoutes(route.SubRoutes, pfix+route.Pattern[:len(route.Pattern)-2])
|
||||
} else {
|
||||
for method, fn := range route.Handlers {
|
||||
fmt.Fprintf(w, "%-8s %-80s -> %s\n", method, pfix+route.Pattern, runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name())
|
||||
if route.Handlers["*"] != nil {
|
||||
fmt.Fprintf(w, "%-8s %-80s\n", "*", pfix+route.Pattern)
|
||||
continue
|
||||
}
|
||||
|
||||
for method := range route.Handlers {
|
||||
fmt.Fprintf(w, "%-8s %-80s\n", method, pfix+route.Pattern)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,17 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/api"
|
||||
"github.com/cortezaproject/corteza-server/pkg/auth"
|
||||
"github.com/cortezaproject/corteza-server/pkg/errors"
|
||||
"github.com/cortezaproject/corteza-server/pkg/healthcheck"
|
||||
"github.com/cortezaproject/corteza-server/pkg/logger"
|
||||
"github.com/cortezaproject/corteza-server/pkg/options"
|
||||
"github.com/cortezaproject/corteza-server/pkg/version"
|
||||
"github.com/cortezaproject/corteza-server/webconsole"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"go.uber.org/zap"
|
||||
@@ -20,7 +23,9 @@ import (
|
||||
// routes used when server is in waiting mode
|
||||
func waitingRoutes(log *zap.Logger, httpOpt options.HttpServerOpt) (r chi.Router) {
|
||||
r = chi.NewRouter()
|
||||
mountServiceHandlers(r, log, httpOpt)
|
||||
r.Use(handleCORS)
|
||||
|
||||
mountServiceHandlers(r, log, httpOpt, waiting)
|
||||
|
||||
r.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
// For non GET requests, return 503 (service unavailable)
|
||||
@@ -61,12 +66,14 @@ func shutdownRoutes() (r chi.Router) {
|
||||
w.Header().Set("Refresh", "15; url=/")
|
||||
_, _ = fmt.Fprint(w, "corteza server shutting down")
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// routes used when in active mode
|
||||
func activeRoutes(log *zap.Logger, mountable []func(r chi.Router), envOpt options.EnvironmentOpt, httpOpt options.HttpServerOpt) (r chi.Router) {
|
||||
r = chi.NewRouter()
|
||||
r.Use(handleCORS)
|
||||
|
||||
r.Route("/"+strings.TrimPrefix(httpOpt.BaseUrl, "/"), func(r chi.Router) {
|
||||
// Reports error to Sentry if enabled
|
||||
@@ -110,14 +117,40 @@ func activeRoutes(log *zap.Logger, mountable []func(r chi.Router), envOpt option
|
||||
|
||||
if httpOpt.BaseUrl != "/" {
|
||||
r.Handle("/", http.RedirectHandler(httpOpt.BaseUrl, http.StatusTemporaryRedirect))
|
||||
|
||||
}
|
||||
|
||||
mountServiceHandlers(r, log, httpOpt)
|
||||
mountServiceHandlers(r, log, httpOpt, active)
|
||||
return
|
||||
}
|
||||
|
||||
func mountServiceHandlers(r chi.Router, log *zap.Logger, opt options.HttpServerOpt) {
|
||||
func mountServiceHandlers(r chi.Router, log *zap.Logger, opt options.HttpServerOpt, state uint32) {
|
||||
if opt.WebConsoleEnabled {
|
||||
path := "/console"
|
||||
log.Info("web console enabled (HTTP_SERVER_WEB_CONSOLE_ENABLED=true): " + path)
|
||||
r.Route(path, func(r chi.Router) {
|
||||
if len(opt.WebConsolePassword) > 0 {
|
||||
credentials := map[string]string{
|
||||
opt.WebConsoleUsername: opt.WebConsolePassword,
|
||||
}
|
||||
r.Use(middleware.BasicAuth("web-console", credentials))
|
||||
} else {
|
||||
// warn only in waiting state to avoid repeated log messages
|
||||
if state == waiting {
|
||||
// warn the user regardless of what environment Corteza is running in.
|
||||
log.Warn("SECURITY RISK: web console is enabled and unprotected, set " +
|
||||
"HTTP_SERVER_WEB_CONSOLE_USERNAME, HTTP_SERVER_WEB_CONSOLE_PASSWORD " +
|
||||
"if not running in development environment!")
|
||||
}
|
||||
}
|
||||
|
||||
webconsole.Mount(r)
|
||||
mountDebugLogViewer(r, log)
|
||||
|
||||
// redirect from /console to /console/ui/
|
||||
r.Mount("/", http.RedirectHandler(path+"/ui", http.StatusTemporaryRedirect))
|
||||
})
|
||||
}
|
||||
|
||||
if opt.EnableDebugRoute {
|
||||
mountDebugHandler(r, log)
|
||||
}
|
||||
@@ -129,8 +162,11 @@ func mountServiceHandlers(r chi.Router, log *zap.Logger, opt options.HttpServerO
|
||||
if opt.EnableHealthcheckRoute {
|
||||
mountHealthCheckHandler(r, log, opt.BaseUrl)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// @todo move all these routes under /console and
|
||||
// output JSON instead of plain raw text
|
||||
func mountDebugHandler(r chi.Router, log *zap.Logger) {
|
||||
log.Debug("route debugger enabled: /__routes")
|
||||
r.Get("/__routes", debugRoutes(r))
|
||||
@@ -183,3 +219,36 @@ func mountHealthCheckHandler(r chi.Router, log *zap.Logger, basePath string) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func mountDebugLogViewer(r chi.Router, log *zap.Logger) {
|
||||
var (
|
||||
path = "/server-log-feed"
|
||||
)
|
||||
|
||||
r.Get(path+".json", func(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
after int = 0
|
||||
limit int = 100
|
||||
err error
|
||||
q = r.URL.Query()
|
||||
)
|
||||
|
||||
if aux := q.Get("after"); len(aux) > 0 {
|
||||
after, err = strconv.Atoi(aux)
|
||||
if err != nil {
|
||||
errors.ProperlyServeHTTP(w, r, errors.InvalidData("invalid value format for after: %v", err), false)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if aux := q.Get("limit"); len(aux) > 0 {
|
||||
limit, err = strconv.Atoi(aux)
|
||||
if err != nil {
|
||||
errors.ProperlyServeHTTP(w, r, errors.InvalidData("invalid value format for limit: %v", err), false)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
_, _ = logger.WriteLogBuffer(w, after, limit)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func New(log *zap.Logger, envOpt options.EnvironmentOpt, httpOpt options.HttpSer
|
||||
waitForOpt: waitForOpt,
|
||||
}
|
||||
|
||||
s.demux = Demux(waiting, waitingRoutes(s.log, s.httpOpt))
|
||||
s.demux = Demux(waiting, waitingRoutes(s.log.Named("waiting"), s.httpOpt))
|
||||
s.demux.Router(shutdown, shutdownRoutes())
|
||||
|
||||
return s
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type (
|
||||
// special file-server meant to be used for serving single page applications
|
||||
// or anything that needs a bit of extra attention like fallback handling
|
||||
// 404 handler, error handler and URL prefixing
|
||||
fileServer struct {
|
||||
// fileServer files
|
||||
files http.FileSystem
|
||||
|
||||
urlPrefix string
|
||||
|
||||
fallbacks []string
|
||||
|
||||
// Final not-found handler
|
||||
notFound http.HandlerFunc
|
||||
|
||||
// how errors are handled
|
||||
errHandler func(w http.ResponseWriter, error string, code int)
|
||||
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
configurator func(*fileServer) error
|
||||
)
|
||||
|
||||
// MountSPA helper function, preconfigures FileServer for SPA serving
|
||||
// and mounts it to chi Router
|
||||
func MountSPA(r chi.Router, path string, root fs.FS, cc ...configurator) error {
|
||||
path = "/" + strings.Trim(strings.TrimRight(path, "*"), "/") + "/"
|
||||
|
||||
cc = append(
|
||||
[]configurator{UrlPrefix(path), Fallbacks("index.html")},
|
||||
// appnd all configurators at the end and allow override of prefix & fallbacks
|
||||
cc...
|
||||
)
|
||||
|
||||
handler, err := FileServer(root, cc...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.Handle(
|
||||
strings.TrimRight(path, "/"),
|
||||
http.RedirectHandler("."+path, http.StatusTemporaryRedirect),
|
||||
)
|
||||
|
||||
r.Handle(path+"*", handler)
|
||||
return nil
|
||||
}
|
||||
|
||||
func FileServer(files fs.FS, cc ...configurator) (h *fileServer, err error) {
|
||||
h = &fileServer{
|
||||
files: http.FS(files),
|
||||
notFound: http.NotFound,
|
||||
errHandler: http.Error,
|
||||
logger: zap.NewNop(),
|
||||
}
|
||||
|
||||
for _, configure := range cc {
|
||||
if err = configure(h); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func UrlPrefix(prefix string) configurator {
|
||||
return func(s *fileServer) error {
|
||||
s.urlPrefix = prefix
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func Fallbacks(ff ...string) configurator {
|
||||
return func(s *fileServer) error {
|
||||
s.fallbacks = ff
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func NotFound(h http.HandlerFunc) configurator {
|
||||
return func(s *fileServer) error {
|
||||
s.notFound = h
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func Logger(l *zap.Logger) configurator {
|
||||
return func(s *fileServer) error {
|
||||
s.logger = l
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Serves the single-page-application
|
||||
//
|
||||
// This is file-server with some special logic for handling missing
|
||||
// files (404s) and directories.
|
||||
// In both cases we serve index file directly
|
||||
func (h *fileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// catch requests for non-existing files and redirect to index.html
|
||||
if h.files == nil {
|
||||
h.errHandler(w, "configured without files", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
trimmed := path.Clean(strings.TrimPrefix(r.URL.Path, h.urlPrefix))
|
||||
h.logger.Debug(r.URL.Path, zap.String("trimmed", trimmed), zap.String("urlPrefix", h.urlPrefix))
|
||||
r.URL.Path = trimmed
|
||||
|
||||
var (
|
||||
err error
|
||||
fh http.File
|
||||
st fs.FileInfo
|
||||
)
|
||||
|
||||
for _, candidate := range append([]string{r.URL.Path}, h.fallbacks...) {
|
||||
if len(candidate) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if fh, err = h.files.Open(candidate); err != nil {
|
||||
continue
|
||||
} else if st, err = fh.Stat(); err != nil {
|
||||
continue
|
||||
} else if st.IsDir() {
|
||||
// index
|
||||
continue
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
if fh == nil || st == nil {
|
||||
h.notFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeContent(w, r, st.Name(), st.ModTime(), fh)
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed test_data/file_server
|
||||
testAssets embed.FS
|
||||
)
|
||||
|
||||
func TestSPA(t *testing.T) {
|
||||
testAssetsSub, err := fs.Sub(testAssets, "test_data/file_server")
|
||||
if err != nil {
|
||||
t.Errorf("failed to dive into subdirectory: %w", err)
|
||||
}
|
||||
|
||||
testDirectAssets := os.DirFS("test_data/file_server")
|
||||
|
||||
var (
|
||||
cases = []struct {
|
||||
name string
|
||||
fs fs.FS
|
||||
cc []configurator
|
||||
url string
|
||||
rsp string
|
||||
}{
|
||||
{
|
||||
name: "no special config",
|
||||
fs: testAssetsSub,
|
||||
url: "index.html",
|
||||
rsp: "index html\n",
|
||||
},
|
||||
|
||||
{
|
||||
name: "empty path",
|
||||
fs: testAssetsSub,
|
||||
cc: []configurator{Fallbacks("index.html")},
|
||||
url: "",
|
||||
rsp: "index html\n",
|
||||
},
|
||||
{
|
||||
name: "slash",
|
||||
fs: testAssetsSub,
|
||||
cc: []configurator{Fallbacks("index.html")},
|
||||
url: "/",
|
||||
rsp: "index html\n",
|
||||
},
|
||||
|
||||
{
|
||||
name: "prefix, no slash",
|
||||
fs: testAssetsSub,
|
||||
cc: []configurator{UrlPrefix("/my-spa"), Fallbacks("index.html")},
|
||||
url: "/my-spa",
|
||||
rsp: "index html\n",
|
||||
},
|
||||
|
||||
{
|
||||
name: "sub dir with url prefix",
|
||||
fs: testAssetsSub,
|
||||
cc: []configurator{UrlPrefix("/my-spa"), Fallbacks("index.html")},
|
||||
url: "/my-spa/index.html",
|
||||
rsp: "index html\n",
|
||||
},
|
||||
{
|
||||
name: "sub dir with url prefix, none existing file",
|
||||
fs: testAssetsSub,
|
||||
cc: []configurator{UrlPrefix("/my-spa"), Fallbacks("index.html")},
|
||||
url: "/not-here",
|
||||
rsp: "index html\n",
|
||||
},
|
||||
{
|
||||
name: "sub dir with url prefix, dir",
|
||||
fs: testAssetsSub,
|
||||
cc: []configurator{UrlPrefix("/my-spa"), Fallbacks("index.html")},
|
||||
url: "/my-spa/sub",
|
||||
rsp: "index html\n",
|
||||
},
|
||||
{
|
||||
name: "sub dir with url prefix, sub index",
|
||||
fs: testAssetsSub,
|
||||
cc: []configurator{UrlPrefix("/my-spa"), Fallbacks("index.html")},
|
||||
url: "/my-spa/sub/index.html",
|
||||
rsp: "sub index html\n",
|
||||
},
|
||||
{
|
||||
name: "sub dir with url prefix, sub file",
|
||||
fs: testAssetsSub,
|
||||
cc: []configurator{UrlPrefix("/my-spa"), Fallbacks("index.html")},
|
||||
url: "/my-spa/sub/test.html",
|
||||
rsp: "sub test html\n",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// another set of test cases for direct fs
|
||||
total := len(cases)
|
||||
for c := 0; c < total; c++ {
|
||||
direct := cases[c]
|
||||
cases[c].name += "; embedded"
|
||||
direct.name += "; direct"
|
||||
direct.fs = testDirectAssets
|
||||
|
||||
cases = append(cases, direct)
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
var (
|
||||
handler http.Handler
|
||||
req = require.New(t)
|
||||
w = httptest.NewRecorder()
|
||||
r, err = http.NewRequest(http.MethodGet, c.url, nil)
|
||||
)
|
||||
|
||||
handler, err = FileServer(c.fs, c.cc...)
|
||||
|
||||
req.NoError(err)
|
||||
handler.ServeHTTP(w, r)
|
||||
|
||||
req.Equal(c.rsp, w.Body.String())
|
||||
req.Equal(http.StatusOK, w.Result().StatusCode)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
index html
|
||||
@@ -0,0 +1 @@
|
||||
sub index html
|
||||
@@ -0,0 +1 @@
|
||||
sub test html
|
||||
@@ -0,0 +1 @@
|
||||
test html
|
||||
+89
-30
@@ -1,6 +1,7 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/options"
|
||||
@@ -10,7 +11,6 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
opt = options.Log()
|
||||
defaultLogger = zap.NewNop()
|
||||
)
|
||||
|
||||
@@ -26,36 +26,76 @@ func SetDefault(logger *zap.Logger) {
|
||||
defaultLogger = logger
|
||||
}
|
||||
|
||||
// Init (re)initializes logger according to the settings
|
||||
// Init (re)initializes global logger according to the settings
|
||||
//
|
||||
// It also peaks into http-server options to determinate if log events
|
||||
// should be buffered for use from web console
|
||||
func Init() {
|
||||
var (
|
||||
// @todo this should probably be refactored by adding a new option to LogOpt
|
||||
// that controls if we create a buffered output as well; and when not explicitly
|
||||
// set, we take state of web-console as a base
|
||||
hSrvOpt = options.HttpServer()
|
||||
logger = Must(Make(options.Log()))
|
||||
)
|
||||
|
||||
if hSrvOpt.WebConsoleEnabled {
|
||||
// web console is the only thing right now
|
||||
// that needs logger to buffer events for later access
|
||||
logger = withDebugBuffer(logger)
|
||||
}
|
||||
|
||||
defaultLogger = logger
|
||||
}
|
||||
|
||||
// Make creates a logger (debug or production) according to options
|
||||
func Make(opt *options.LogOpt) (logger *zap.Logger, err error) {
|
||||
if opt.Debug {
|
||||
// Do we want to enable debug logger
|
||||
// with a bit more dev-friendly output
|
||||
defaultLogger = MakeDebugLogger()
|
||||
defaultLogger.Debug("full debug mode enabled")
|
||||
return
|
||||
logger, err = Debug(opt)
|
||||
} else {
|
||||
logger, err = Production(opt)
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
conf = applyOptions(zap.NewProductionConfig(), opt)
|
||||
)
|
||||
|
||||
defaultLogger, err = conf.Build()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defaultLogger = applySpecials(defaultLogger, opt)
|
||||
logger = withFilter(logger, opt.Filter)
|
||||
logger = withStacktraceLevel(logger, opt.StacktraceLevel)
|
||||
|
||||
return logger, nil
|
||||
}
|
||||
|
||||
func MakeDebugLogger() *zap.Logger {
|
||||
dbgOpt := *opt
|
||||
dbgOpt.Debug = true
|
||||
dbgOpt.Level = "debug"
|
||||
return Must(Debug(options.Log()))
|
||||
}
|
||||
|
||||
// Must is a utility function that panics if given log maker returns an error
|
||||
func Must(logger *zap.Logger, err error) *zap.Logger {
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to configure logger: %w", err))
|
||||
}
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
// Debug prepares debug logger using options
|
||||
func Debug(opt *options.LogOpt) (*zap.Logger, error) {
|
||||
var (
|
||||
// make a copy of debug options so that we do not
|
||||
dbgOpt = &options.LogOpt{
|
||||
Debug: true,
|
||||
Level: "debug",
|
||||
Filter: opt.Filter,
|
||||
IncludeCaller: opt.IncludeCaller,
|
||||
StacktraceLevel: opt.StacktraceLevel,
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
conf = applyOptions(zap.NewDevelopmentConfig(), &dbgOpt)
|
||||
conf = applyOptionsToConfig(zap.NewDevelopmentConfig(), dbgOpt)
|
||||
)
|
||||
|
||||
// Print log level in colors
|
||||
@@ -66,16 +106,19 @@ func MakeDebugLogger() *zap.Logger {
|
||||
enc.AppendString(t.Format("15:04:05.000"))
|
||||
}
|
||||
|
||||
logger, err := conf.Build()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return conf.Build()
|
||||
}
|
||||
|
||||
return applySpecials(logger, &dbgOpt)
|
||||
func Production(opt *options.LogOpt) (*zap.Logger, error) {
|
||||
var (
|
||||
conf = applyOptionsToConfig(zap.NewProductionConfig(), opt)
|
||||
)
|
||||
|
||||
return conf.Build()
|
||||
}
|
||||
|
||||
// Applies options from environment variables
|
||||
func applyOptions(conf zap.Config, opt *options.LogOpt) zap.Config {
|
||||
func applyOptionsToConfig(conf zap.Config, opt *options.LogOpt) zap.Config {
|
||||
// LOG_LEVEL
|
||||
conf.Level = zap.NewAtomicLevelAt(mustParseLevel(opt.Level))
|
||||
|
||||
@@ -87,15 +130,31 @@ func applyOptions(conf zap.Config, opt *options.LogOpt) zap.Config {
|
||||
return conf
|
||||
}
|
||||
|
||||
// Applies "special" options - filtering and conditional stack-level
|
||||
func applySpecials(l *zap.Logger, opt *options.LogOpt) *zap.Logger {
|
||||
if len(opt.Filter) > 0 {
|
||||
// LOG_FILTER
|
||||
l = zap.New(zapfilter.NewFilteringCore(l.Core(), zapfilter.MustParseRules(opt.Filter)))
|
||||
// Applies filtering options
|
||||
//
|
||||
// This is controlled with LOG_FILTER environmental var
|
||||
func withFilter(l *zap.Logger, filter string) *zap.Logger {
|
||||
if len(filter) > 0 {
|
||||
l = zap.New(zapfilter.NewFilteringCore(l.Core(), zapfilter.MustParseRules(filter)))
|
||||
}
|
||||
|
||||
// LOG_STACKTRACE_LEVEL
|
||||
return l.WithOptions(zap.AddStacktrace(mustParseLevel(opt.StacktraceLevel)))
|
||||
return l
|
||||
}
|
||||
|
||||
// Applies stacktrace level options
|
||||
//
|
||||
// This is controlled with LOG_STACKTRACE_LEVEL environmental var
|
||||
func withStacktraceLevel(l *zap.Logger, level string) *zap.Logger {
|
||||
return l.WithOptions(zap.AddStacktrace(mustParseLevel(level)))
|
||||
}
|
||||
|
||||
// Adds Tee logger that copies all log messages to debug buffer
|
||||
func withDebugBuffer(in *zap.Logger) *zap.Logger {
|
||||
return zap.New(zapcore.NewTee(
|
||||
in.Core(),
|
||||
|
||||
DebugBufferedLogger(debugLogRR),
|
||||
))
|
||||
}
|
||||
|
||||
func mustParseLevel(l string) (o zapcore.Level) {
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
type (
|
||||
rrBufEntry struct {
|
||||
num int
|
||||
rec []byte
|
||||
}
|
||||
|
||||
// simple round-robin struct that holds our buffer with log entries
|
||||
rr struct {
|
||||
mux sync.RWMutex
|
||||
num int
|
||||
buf []*rrBufEntry
|
||||
}
|
||||
|
||||
debugBufferingLogger struct {
|
||||
zapcore.LevelEnabler
|
||||
out *rr
|
||||
enc zapcore.Encoder
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
// allowing 10k entries (no limiting the entry size)
|
||||
debugLogCap = 10240
|
||||
)
|
||||
|
||||
var (
|
||||
// initialize debug logger round robin db
|
||||
debugLogRR = &rr{
|
||||
buf: make([]*rrBufEntry, 0),
|
||||
}
|
||||
)
|
||||
|
||||
// WriteLogBuffer provides access to default debug log buffer
|
||||
func WriteLogBuffer(w io.Writer, after, limit int) (_ int, err error) {
|
||||
return writeLogBuffer(w, debugLogRR, after, limit)
|
||||
}
|
||||
|
||||
// writes stream of entries from log buffer into provided writer array of JSON objects.
|
||||
func writeLogBuffer(w io.Writer, logBuf *rr, after, limit int) (_ int, err error) {
|
||||
var (
|
||||
// was at least one entry outputted?
|
||||
has bool
|
||||
)
|
||||
|
||||
debugLogRR.mux.RLock()
|
||||
defer debugLogRR.mux.RUnlock()
|
||||
if _, err = w.Write([]byte{'['}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, e := range logBuf.buf {
|
||||
if after >= e.num {
|
||||
continue
|
||||
}
|
||||
|
||||
// count back to zero
|
||||
limit--
|
||||
|
||||
if has {
|
||||
if _, err = w.Write([]byte{','}); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if _, err = w.Write(e.rec); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if limit == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
has = true
|
||||
}
|
||||
|
||||
if _, err = w.Write([]byte{']'}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (r *rr) append(ent []byte) {
|
||||
r.mux.Lock()
|
||||
defer r.mux.Unlock()
|
||||
|
||||
r.num++
|
||||
|
||||
// modify serialized json
|
||||
var bufEnt = &rrBufEntry{r.num, append(ent[:len(ent)-2], []byte(fmt.Sprintf(`,"index":%d}`, r.num))...)}
|
||||
|
||||
if len(r.buf) >= debugLogCap {
|
||||
r.buf = append(r.buf[1:], bufEnt)
|
||||
} else {
|
||||
r.buf = append(r.buf, bufEnt)
|
||||
}
|
||||
}
|
||||
|
||||
// DebugBufferedLogger provides buffered logger compatible with zap.
|
||||
func DebugBufferedLogger(out *rr) *debugBufferingLogger {
|
||||
var encConf = zap.NewProductionEncoderConfig()
|
||||
encConf.EncodeTime = zapcore.RFC3339NanoTimeEncoder
|
||||
|
||||
return &debugBufferingLogger{
|
||||
LevelEnabler: zapcore.DebugLevel,
|
||||
out: out,
|
||||
enc: zapcore.NewJSONEncoder(encConf),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *debugBufferingLogger) With(fields []zapcore.Field) zapcore.Core {
|
||||
clone := c.clone()
|
||||
for i := range fields {
|
||||
fields[i].AddTo(clone.enc)
|
||||
}
|
||||
return clone
|
||||
}
|
||||
|
||||
func (c *debugBufferingLogger) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
|
||||
if c.Enabled(ent.Level) {
|
||||
return ce.AddCore(ent, c)
|
||||
}
|
||||
return ce
|
||||
}
|
||||
|
||||
func (c *debugBufferingLogger) Write(ent zapcore.Entry, fields []zapcore.Field) error {
|
||||
encbuf, err := c.enc.EncodeEntry(ent, fields)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.out.append(encbuf.Bytes())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *debugBufferingLogger) Sync() error { return nil }
|
||||
func (c *debugBufferingLogger) clone() *debugBufferingLogger {
|
||||
return &debugBufferingLogger{
|
||||
LevelEnabler: c.LevelEnabler,
|
||||
enc: c.enc.Clone(),
|
||||
out: c.out,
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,15 @@
|
||||
package options
|
||||
|
||||
func (o *HttpServerOpt) Defaults() {
|
||||
if Environment().IsDevelopment() {
|
||||
// enable web console and remove username, password defaults
|
||||
// if this is explicitly via ENV, it will override these defaults
|
||||
o.WebConsoleEnabled = true
|
||||
o.WebConsoleUsername = ""
|
||||
o.WebConsolePassword = ""
|
||||
}
|
||||
}
|
||||
|
||||
func (o *HttpServerOpt) Cleanup() {
|
||||
o.BaseUrl = CleanBase(o.BaseUrl)
|
||||
o.ApiBaseUrl = CleanBase(o.ApiBaseUrl)
|
||||
|
||||
Generated
+6
@@ -43,6 +43,9 @@ type (
|
||||
WebappBaseDir string `env:"HTTP_WEBAPP_BASE_DIR"`
|
||||
WebappList string `env:"HTTP_WEBAPP_LIST"`
|
||||
SslTerminated bool `env:"HTTP_SSL_TERMINATED"`
|
||||
WebConsoleEnabled bool `env:"HTTP_SERVER_WEB_CONSOLE_ENABLED"`
|
||||
WebConsoleUsername string `env:"HTTP_SERVER_WEB_CONSOLE_USERNAME"`
|
||||
WebConsolePassword string `env:"HTTP_SERVER_WEB_CONSOLE_PASSWORD"`
|
||||
}
|
||||
|
||||
RbacOpt struct {
|
||||
@@ -325,6 +328,9 @@ func HttpServer() (o *HttpServerOpt) {
|
||||
WebappBaseDir: "./webapp/public",
|
||||
WebappList: "admin,compose,workflow,reporter",
|
||||
SslTerminated: isSecure(),
|
||||
WebConsoleEnabled: false,
|
||||
WebConsoleUsername: "admin",
|
||||
WebConsolePassword: string(rand.Bytes(32)),
|
||||
}
|
||||
|
||||
// Custom defaults
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
/* eslint-env node */
|
||||
require("@rushstack/eslint-patch/modern-module-resolution");
|
||||
|
||||
module.exports = {
|
||||
"root": true,
|
||||
"extends": [
|
||||
"plugin:vue/vue3-essential",
|
||||
"eslint:recommended",
|
||||
"@vue/eslint-config-typescript/recommended",
|
||||
],
|
||||
"env": {
|
||||
"vue/setup-compiler-macros": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
.DS_Store
|
||||
dist
|
||||
dist-ssr
|
||||
!dist/.placeholder
|
||||
coverage
|
||||
*.local
|
||||
public/config.js
|
||||
|
||||
/cypress/videos/
|
||||
/cypress/screenshots/
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
@@ -0,0 +1,46 @@
|
||||
# Corteza Server Web Console
|
||||
|
||||
## Type Support for `.vue` Imports in TS
|
||||
|
||||
TypeScript cannot handle type information for `.vue` imports by default, so we replace the `tsc` CLI with `vue-tsc` for type checking. In editors, we need [TypeScript Vue Plugin (Volar)](https://marketplace.visualstudio.com/items?itemName=johnsoncodehk.vscode-typescript-vue-plugin) to make the TypeScript language service aware of `.vue` types.
|
||||
|
||||
If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a [Take Over Mode](https://github.com/johnsoncodehk/volar/discussions/471#discussioncomment-1361669) that is more performant. You can enable it by the following steps:
|
||||
|
||||
1. Disable the built-in TypeScript Extension
|
||||
1) Run `Extensions: Show Built-in Extensions` from VSCode's command palette
|
||||
2) Find `TypeScript and JavaScript Language Features`, right click and select `Disable (Workspace)`
|
||||
2. Reload the VSCode window by running `Developer: Reload Window` from the command palette.
|
||||
|
||||
## Customize configuration
|
||||
|
||||
See [Vite Configuration Reference](https://vitejs.dev/config/).
|
||||
|
||||
## Project Setup
|
||||
|
||||
```sh
|
||||
npm install
|
||||
```
|
||||
|
||||
### Compile and Hot-Reload for Development
|
||||
|
||||
```sh
|
||||
npm run dev
|
||||
```
|
||||
|
||||
### Type-Check, Compile and Minify for Production
|
||||
|
||||
```sh
|
||||
npm run build
|
||||
```
|
||||
|
||||
### Run Unit Tests with [Vitest](https://vitest.dev/)
|
||||
|
||||
```sh
|
||||
npm run test:unit
|
||||
```
|
||||
|
||||
### Lint with [ESLint](https://eslint.org/)
|
||||
|
||||
```sh
|
||||
npm run lint
|
||||
```
|
||||
@@ -0,0 +1,11 @@
|
||||
# Corteza Server Web Console
|
||||
|
||||
When enabled (`HTTP_WEB_CONSOLE_ENABLED=true`), it allows insight and management of corteza internals.
|
||||
|
||||
## Web Console development
|
||||
|
||||
When developing web-console backend (API) base URL must be set to the actual server. That can be achieved by setting a URL as value local store item with `console-api-base-url` as key:
|
||||
|
||||
```javascript
|
||||
localStorage.setItem('console-api-base-url', '//localhost:3000/console')
|
||||
```
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
Placeholder file to avoid broken build when dist is empty (go error: contains no embeddable files)
|
||||
File is copied from the public to dist when web console app is built.
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
/// <reference types="vite/client" />
|
||||
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" href="/favicon32x32.png" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Corteza Server Web Console</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/src/main.ts"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "corteza-server-web-console",
|
||||
"license": "Apache-2.0",
|
||||
"version": "0.0.0",
|
||||
"contributors": [
|
||||
"Denis Arh <denis.arh@crust.tech>"
|
||||
],
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vue-tsc --noEmit && vite build",
|
||||
"preview": "vite preview --port 5050",
|
||||
"test:unit": "vitest --environment jsdom",
|
||||
"typecheck": "vue-tsc --noEmit && vue-tsc --noEmit -p tsconfig.vitest.json --composite false",
|
||||
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.26.0",
|
||||
"pinia": "^2.0.11",
|
||||
"sass": "^1.49.7",
|
||||
"vue": "^3.2.29",
|
||||
"vue-router": "^4.0.12"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rushstack/eslint-patch": "^1.1.0",
|
||||
"@types/jsdom": "^16.2.14",
|
||||
"@types/node": "^16.11.22",
|
||||
"@vitejs/plugin-vue": "^2.1.0",
|
||||
"@vue/eslint-config-prettier": "^7.0.0",
|
||||
"@vue/eslint-config-typescript": "^10.0.0",
|
||||
"@vue/test-utils": "^2.0.0-rc.18",
|
||||
"@vue/tsconfig": "^0.1.3",
|
||||
"eslint": "^8.5.0",
|
||||
"eslint-plugin-vue": "^8.2.0",
|
||||
"jsdom": "^19.0.0",
|
||||
"prettier": "^2.5.1",
|
||||
"typescript": "~4.5.5",
|
||||
"vite": "^2.7.13",
|
||||
"vitest": "^0.2.5",
|
||||
"vue-tsc": "^0.31.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
Placeholder file to avoid broken build when dist is empty (go error: contains no embeddable files)
|
||||
File is copied from the public to dist when web console app is built.
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.2 KiB |
@@ -0,0 +1,27 @@
|
||||
package webconsole
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/pkg/http"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
var (
|
||||
// we need combination of go:embed dist/* and .placeholder
|
||||
// file inside. If only dist (w/o) wildcard is used,
|
||||
// dot-file (.placeholder) will be ignored
|
||||
|
||||
//go:embed dist/*
|
||||
assets embed.FS
|
||||
)
|
||||
|
||||
func Mount(r chi.Router) error {
|
||||
assets, err := fs.Sub(assets, "dist")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return http.MountSPA(r, "/ui", assets, http.UrlPrefix("/console/ui"))
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<template>
|
||||
<header>
|
||||
<div class="wrapper">
|
||||
<nav>
|
||||
<RouterLink to="/">Home</RouterLink>
|
||||
<RouterLink to="/log-viewer">Logs</RouterLink>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
<div
|
||||
v-if="capturedError"
|
||||
class="error"
|
||||
>
|
||||
<h2>Error</h2>
|
||||
<p v-html="capturedError"></p>
|
||||
</div>
|
||||
<main
|
||||
v-else
|
||||
>
|
||||
<router-view />
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { RouterLink, RouterView } from 'vue-router'
|
||||
import { onBeforeMount, ref } from 'vue'
|
||||
|
||||
const capturedError = ref<string | undefined>(undefined)
|
||||
|
||||
onBeforeMount(() => {
|
||||
//
|
||||
})
|
||||
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
@import '@/assets/base.css';
|
||||
|
||||
nav {
|
||||
background: black;
|
||||
padding: 15px;
|
||||
font-size: 140%;
|
||||
a {
|
||||
margin-right: 20px;
|
||||
color: white;
|
||||
text-decoration: none;
|
||||
}
|
||||
}
|
||||
|
||||
.error {
|
||||
background: #ffa9a9;
|
||||
padding: 40px;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
</style>
|
||||
@@ -0,0 +1,5 @@
|
||||
html, body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: monospace;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import axios from 'axios'
|
||||
import type { AxiosInstance, AxiosRequestConfig } from 'axios'
|
||||
|
||||
export interface LogEntry {
|
||||
ts: Date
|
||||
level: string
|
||||
logger: string
|
||||
msg: string
|
||||
index: number
|
||||
extra?: Record<string, unknown>
|
||||
}
|
||||
|
||||
interface LogEntryResponse extends Omit<LogEntry, 'ts'> {
|
||||
ts: string
|
||||
[_:string]: unknown
|
||||
}
|
||||
|
||||
interface LogFetchParams {
|
||||
after?: number
|
||||
limit?: number
|
||||
}
|
||||
|
||||
|
||||
let baseURL = '/console'
|
||||
|
||||
const storedBaseURL = window.localStorage.getItem('console-api-base-url')
|
||||
if (storedBaseURL !== null) {
|
||||
baseURL = storedBaseURL
|
||||
console.warn('using base URL from local store (key: console-api-base-url)', { baseURL })
|
||||
}
|
||||
|
||||
export async function fetchLoggedEvents (params: LogFetchParams = {}): Promise<Array<LogEntry>> {
|
||||
const config: AxiosRequestConfig = {
|
||||
params
|
||||
}
|
||||
|
||||
return api()
|
||||
.get<Array<LogEntryResponse>>('/server-log-feed.json', config)
|
||||
.then(({ data }) => {
|
||||
return data.map(({ ts, index, level, logger, msg, ...extra }) => ({
|
||||
ts: new Date(Date.parse(ts)),
|
||||
index,
|
||||
level,
|
||||
logger,
|
||||
msg,
|
||||
extra: Object.getOwnPropertyNames(extra).length > 0 ? extra : undefined
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
function api(): AxiosInstance {
|
||||
return axios.create({ baseURL })
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { createApp } from 'vue'
|
||||
import { createPinia } from 'pinia'
|
||||
|
||||
import App from './WebConsole.vue'
|
||||
import router from './views'
|
||||
|
||||
const app = createApp(App)
|
||||
|
||||
app.use(createPinia())
|
||||
app.use(router)
|
||||
|
||||
app.mount('#app')
|
||||
@@ -0,0 +1,16 @@
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
export const useCounterStore = defineStore({
|
||||
id: 'counter',
|
||||
state: () => ({
|
||||
counter: 0
|
||||
}),
|
||||
getters: {
|
||||
doubleCount: (state) => state.counter * 2
|
||||
},
|
||||
actions: {
|
||||
increment() {
|
||||
this.counter++
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,10 @@
|
||||
<template>
|
||||
<section>
|
||||
<h1>Welcome to Corteza Server Web Console.</h1>
|
||||
</section>
|
||||
</template>
|
||||
<style lang="scss">
|
||||
section {
|
||||
padding: 20px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,129 @@
|
||||
<template>
|
||||
<section>
|
||||
<h1>Log viewer</h1>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<td>one</td>
|
||||
<td>level</td>
|
||||
<td>logger</td>
|
||||
<td>message</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tfoot v-if="lastRefresh">
|
||||
<tr>
|
||||
<td
|
||||
colspan="4"
|
||||
class="last-refresh"
|
||||
>
|
||||
{{ lastRefresh.toISOString().substring(11) }}
|
||||
</td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
<tbody>
|
||||
<tr
|
||||
v-for="e in events"
|
||||
:key="e.index"
|
||||
:class="[`level-${e.level}`]"
|
||||
>
|
||||
<td class="ts">{{ e.ts.toISOString().substring(11) }}</td>
|
||||
<td class="level">{{ e.level }}</td>
|
||||
<td class="logger">{{ e.logger }}</td>
|
||||
<td class="msg">
|
||||
{{ e.msg }}
|
||||
<pre v-if="e.extra">{{ e.extra }}</pre>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, onUnmounted } from 'vue'
|
||||
import { fetchLoggedEvents } from '@/libs/logs'
|
||||
import type { LogEntry } from '@/libs/logs'
|
||||
|
||||
let intervalHandler: number | undefined
|
||||
const events = ref<Array<LogEntry>>([])
|
||||
const lastRefresh = ref<Date | undefined>()
|
||||
|
||||
|
||||
onMounted(() => {
|
||||
events.value = []
|
||||
fetch().then((interval: boolean) => {
|
||||
if (!interval) {
|
||||
return
|
||||
}
|
||||
|
||||
setInterval(async () => {
|
||||
let after: number | undefined
|
||||
|
||||
if (events.value.length > 0) {
|
||||
after = events.value[events.value.length - 1].index
|
||||
}
|
||||
|
||||
await fetch(after)
|
||||
}, 2000)
|
||||
}).catch((err ) => {
|
||||
alert(err)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
async function fetch (after?: number): Promise<boolean> {
|
||||
return fetchLoggedEvents({ after, limit: -1 })
|
||||
.then(ee => {
|
||||
events.value.push(...ee)
|
||||
lastRefresh.value = new Date()
|
||||
return true
|
||||
})
|
||||
.catch((err) => {
|
||||
alert(err)
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
if (intervalHandler) {
|
||||
clearInterval(intervalHandler)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
</script>
|
||||
<style lang="scss" scoped>
|
||||
table {
|
||||
width: 100vw;
|
||||
font-family: monospace;
|
||||
|
||||
tbody {
|
||||
tr {
|
||||
&.level-warn {
|
||||
background: gold;
|
||||
}
|
||||
|
||||
&.level-debug .msg {
|
||||
color: gray;
|
||||
}
|
||||
|
||||
td {
|
||||
padding: 3px;
|
||||
margin: 0;
|
||||
border-top: 1px dotted silver;
|
||||
vertical-align: top;
|
||||
|
||||
&.msg pre {
|
||||
color: gray;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tfoot {
|
||||
.last-refresh {
|
||||
padding: 20px;
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,23 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import HomeView from './HomeView.vue'
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
routes: [
|
||||
{
|
||||
path: '/',
|
||||
name: 'home',
|
||||
component: HomeView
|
||||
},
|
||||
{
|
||||
path: '/log-viewer',
|
||||
name: 'log-viewer',
|
||||
// route level code-splitting
|
||||
// this generates a separate chunk (About.[hash].js) for this route
|
||||
// which is lazy-loaded when the route is visited.
|
||||
component: () => import('./LogViewer.vue')
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
export default router
|
||||
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"extends": "@vue/tsconfig/tsconfig.web.json",
|
||||
"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],
|
||||
"exclude": ["src/**/__tests__/*"],
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.vite-config.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.vitest.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "@vue/tsconfig/tsconfig.node.json",
|
||||
"include": ["vite.config.*"],
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"types": ["node", "vitest"]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"extends": "@vue/tsconfig/tsconfig.node.json",
|
||||
"include": ["src/**/__tests__/*"],
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
},
|
||||
"types": ["node", "jsdom"]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { fileURLToPath, URL } from "url";
|
||||
|
||||
import { defineConfig } from "vite";
|
||||
import vue from "@vitejs/plugin-vue";
|
||||
|
||||
// https://vitejs.dev/config/
|
||||
export default defineConfig({
|
||||
base: '/console/ui/',
|
||||
plugins: [vue()],
|
||||
resolve: {
|
||||
alias: {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
"@": fileURLToPath(new URL("./src", import.meta.url)),
|
||||
},
|
||||
},
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user