Skip to content
Snippets Groups Projects
plugin.go 1.27 KiB
Newer Older
bingyanglin's avatar
bingyanglin committed
package dashboard

import (
	"net/http"
	"golang.org/x/net/context"
	"github.com/iotaledger/goshimmer/plugins/metrics"
	"github.com/iotaledger/hive.go/daemon"
	"github.com/iotaledger/hive.go/events"
capossele's avatar
capossele committed
	"github.com/iotaledger/hive.go/logger"
	"github.com/iotaledger/hive.go/node"
var server *http.Server

var router *http.ServeMux
var PLUGIN = node.NewPlugin("Dashboard", node.Disabled, configure, run)
var log = logger.NewLogger("Dashboard")
bingyanglin's avatar
bingyanglin committed
func configure(plugin *node.Plugin) {
	router = http.NewServeMux()
	server = &http.Server{Addr: ":8081", Handler: router}

	router.HandleFunc("/dashboard", ServeHome)
	router.HandleFunc("/ws", ServeWs)

	// send the sampledTPS to client via websocket, use uint32 to save mem
	metrics.Events.ReceivedTPSUpdated.Attach(events.NewClosure(func(sampledTPS uint64) {
		TPSQ = append(TPSQ, uint32(sampledTPS))
		if len(TPSQ) > MAX_Q_SIZE {
			TPSQ = TPSQ[1:]
		}
	}))

	daemon.Events.Shutdown.Attach(events.NewClosure(func() {
		ctx, cancel := context.WithTimeout(context.Background(), 0*time.Second)
		defer cancel()

		_ = server.Shutdown(ctx)
	}))
bingyanglin's avatar
bingyanglin committed
}

func run(plugin *node.Plugin) {
	daemon.BackgroundWorker("Dashboard Updater", func() {
		go func() {
			if err := server.ListenAndServe(); err != nil {
bingyanglin's avatar
bingyanglin committed
	})
}