mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
global: split Akvorado into 3 services
This commit is contained in:
122
console/root.go
Normal file
122
console/root.go
Normal file
@@ -0,0 +1,122 @@
|
||||
// Package console exposes a web interface.
|
||||
package console
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"log"
|
||||
netHTTP "net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"gopkg.in/tomb.v2"
|
||||
|
||||
"akvorado/common/daemon"
|
||||
"akvorado/common/http"
|
||||
"akvorado/common/reporter"
|
||||
)
|
||||
|
||||
// Component represents the console component.
|
||||
type Component struct {
|
||||
r *reporter.Reporter
|
||||
d *Dependencies
|
||||
t tomb.Tomb
|
||||
config Configuration
|
||||
|
||||
templates map[string]*template.Template
|
||||
templatesLock sync.RWMutex
|
||||
}
|
||||
|
||||
// Dependencies define the dependencies of the console component.
|
||||
type Dependencies struct {
|
||||
Daemon daemon.Component
|
||||
HTTP *http.Component
|
||||
}
|
||||
|
||||
// New creates a new console component.
|
||||
func New(reporter *reporter.Reporter, config Configuration, dependencies Dependencies) (*Component, error) {
|
||||
c := Component{
|
||||
r: reporter,
|
||||
d: &dependencies,
|
||||
config: config,
|
||||
}
|
||||
c.d.Daemon.Track(&c.t, "console")
|
||||
if err := c.loadTemplates(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Grafana proxy
|
||||
if c.config.GrafanaURL != "" {
|
||||
// Provide a proxy for Grafana
|
||||
url, err := url.Parse(config.GrafanaURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse Grafana URL %q: %w", config.GrafanaURL, err)
|
||||
}
|
||||
proxy := httputil.NewSingleHostReverseProxy(url)
|
||||
proxy.Transport = &netHTTP.Transport{
|
||||
Proxy: nil, // Disable proxy
|
||||
}
|
||||
proxy.ErrorLog = log.New(c.r.With().
|
||||
Str("proxy", "grafana").
|
||||
Str("level", zerolog.LevelWarnValue).
|
||||
Logger(), "", 0)
|
||||
proxyHandler := netHTTP.HandlerFunc(
|
||||
func(w netHTTP.ResponseWriter, r *netHTTP.Request) {
|
||||
proxy.ServeHTTP(w, r)
|
||||
})
|
||||
c.d.HTTP.AddHandler("/grafana/", proxyHandler)
|
||||
}
|
||||
|
||||
c.d.HTTP.AddHandler("/docs/", netHTTP.HandlerFunc(c.docsHandlerFunc))
|
||||
c.d.HTTP.AddHandler("/assets/", netHTTP.HandlerFunc(c.assetsHandlerFunc))
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// Start starts the web component.
|
||||
func (c *Component) Start() error {
|
||||
c.r.Info().Msg("starting console component")
|
||||
if err := c.watchTemplates(); err != nil {
|
||||
return err
|
||||
}
|
||||
c.t.Go(func() error {
|
||||
select {
|
||||
case <-c.t.Dying():
|
||||
return nil
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop stops the console component.
|
||||
func (c *Component) Stop() error {
|
||||
c.r.Info().Msg("stopping console component")
|
||||
defer c.r.Info().Msg("console component stopped")
|
||||
c.t.Kill(nil)
|
||||
return c.t.Wait()
|
||||
}
|
||||
|
||||
// embedOrLiveFS returns a subset of the provided embedded filesystem,
|
||||
// except if the component is configured to use the live filesystem.
|
||||
// Then, it returns the provided tree.
|
||||
func (c *Component) embedOrLiveFS(embed fs.FS, p string) fs.FS {
|
||||
var fileSystem fs.FS
|
||||
if c.config.ServeLiveFS {
|
||||
_, src, _, _ := runtime.Caller(0)
|
||||
fileSystem = os.DirFS(filepath.Join(path.Dir(src), p))
|
||||
} else {
|
||||
var err error
|
||||
fileSystem, err = fs.Sub(embed, p)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
return fileSystem
|
||||
}
|
||||
Reference in New Issue
Block a user