parent
fd63889697
commit
e75535cad7
11 changed files with 254 additions and 3 deletions
|
@ -17,7 +17,7 @@ func (server *Server) setupMiddlewares() {
|
||||||
server.Router.Use(middleware.CleanPath)
|
server.Router.Use(middleware.CleanPath)
|
||||||
server.Router.Use(middleware.RedirectSlashes)
|
server.Router.Use(middleware.RedirectSlashes)
|
||||||
server.Router.Use(middleware.AllowContentEncoding("deflate", "gzip"))
|
server.Router.Use(middleware.AllowContentEncoding("deflate", "gzip"))
|
||||||
server.Router.Use(middleware.Compress(5, "text/html", "text/js", "text/css"))
|
server.Router.Use(middleware.Compress(5, "text/html", "text/css"))
|
||||||
server.Router.Use(cors.Handler(cors.Options{
|
server.Router.Use(cors.Handler(cors.Options{
|
||||||
AllowedOrigins: server.AllowedHosts,
|
AllowedOrigins: server.AllowedHosts,
|
||||||
AllowedMethods: []string{"GET", "OPTIONS"},
|
AllowedMethods: []string{"GET", "OPTIONS"},
|
||||||
|
|
|
@ -9,7 +9,11 @@ import (
|
||||||
func (server *Server) setupRouter() {
|
func (server *Server) setupRouter() {
|
||||||
server.Router.Get("/", launchpad)
|
server.Router.Get("/", launchpad)
|
||||||
server.Router.Route("/api", func(r chi.Router) {
|
server.Router.Route("/api", func(r chi.Router) {
|
||||||
r.Get("/ws", webSocket)
|
r.Route("/system", func(r chi.Router) {
|
||||||
|
r.Get("/static", routeStaticSystem)
|
||||||
|
r.Get("/live", routeLiveSystem)
|
||||||
|
r.Get("/ws", webSocket)
|
||||||
|
})
|
||||||
r.Get("/weather", getWeather)
|
r.Get("/weather", getWeather)
|
||||||
})
|
})
|
||||||
server.Router.Get("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
server.Router.Get("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"godash/files"
|
"godash/files"
|
||||||
"godash/hub"
|
"godash/hub"
|
||||||
"godash/message"
|
"godash/message"
|
||||||
|
"godash/system"
|
||||||
"godash/weather"
|
"godash/weather"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
@ -16,6 +17,7 @@ type launchpadInformation struct {
|
||||||
Host string
|
Host string
|
||||||
Bookmarks []bookmark.Bookmark
|
Bookmarks []bookmark.Bookmark
|
||||||
Weather weather.OpenWeatherApiResponse
|
Weather weather.OpenWeatherApiResponse
|
||||||
|
System system.LiveInformation
|
||||||
}
|
}
|
||||||
|
|
||||||
func launchpad(w http.ResponseWriter, r *http.Request) {
|
func launchpad(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -24,6 +26,7 @@ func launchpad(w http.ResponseWriter, r *http.Request) {
|
||||||
Title: "Godash",
|
Title: "Godash",
|
||||||
Bookmarks: bookmark.Bookmarks,
|
Bookmarks: bookmark.Bookmarks,
|
||||||
Weather: weather.CurrentOpenWeather,
|
Weather: weather.CurrentOpenWeather,
|
||||||
|
System: system.Sys.Live,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +46,38 @@ func getWeather(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @Schemes
|
||||||
|
// @Summary live system information
|
||||||
|
// @Description gets live information of the system
|
||||||
|
// @Tags system
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} system.LiveInformation
|
||||||
|
// @Success 204 {object} message.Response
|
||||||
|
// @Router /system/live [get]
|
||||||
|
func routeLiveSystem(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if system.Config.LiveSystem {
|
||||||
|
jsonResponse(w, system.Sys.Live, http.StatusOK)
|
||||||
|
} else {
|
||||||
|
jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Schemes
|
||||||
|
// @Summary static system information
|
||||||
|
// @Description gets static information of the system
|
||||||
|
// @Tags system
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} system.StaticInformation
|
||||||
|
// @Success 204 {object} message.Response
|
||||||
|
// @Router /system/static [get]
|
||||||
|
func routeStaticSystem(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if system.Config.LiveSystem {
|
||||||
|
jsonResponse(w, system.Sys.Static, http.StatusOK)
|
||||||
|
} else {
|
||||||
|
jsonResponse(w, message.Response{Message: message.NotFound.String()}, http.StatusNoContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func webSocket(w http.ResponseWriter, r *http.Request) {
|
func webSocket(w http.ResponseWriter, r *http.Request) {
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const WsType = { Weather: 0, System: 1 };
|
const WsType = { Weather: 0, System: 1 };
|
||||||
const apiBase = window.location.origin + "/api";
|
const apiBase = window.location.origin + "/api";
|
||||||
let socket = new WebSocket(apiBase.replace("http", "ws") + "/ws");
|
let socket = new WebSocket(apiBase.replace("http", "ws") + "/system/ws");
|
||||||
const weatherIcon = document.getElementById("weatherIcon");
|
const weatherIcon = document.getElementById("weatherIcon");
|
||||||
const weatherTemp = document.getElementById("weatherTemp");
|
const weatherTemp = document.getElementById("weatherTemp");
|
||||||
const weatherDescription = document.getElementById("weatherDescription");
|
const weatherDescription = document.getElementById("weatherDescription");
|
||||||
|
|
29
system/cpu.go
Normal file
29
system/cpu.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/shirou/gopsutil/v3/cpu"
|
||||||
|
"math"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func staticCpu() CPU {
|
||||||
|
var p CPU
|
||||||
|
p.Threads = runtime.NumCPU()
|
||||||
|
p.Architecture = runtime.GOARCH
|
||||||
|
c, err := cpu.Info()
|
||||||
|
if err == nil {
|
||||||
|
p.Name = c[0].ModelName
|
||||||
|
} else {
|
||||||
|
p.Name = "none detected"
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *System) liveCpu() {
|
||||||
|
p, err := cpu.Percent(0, false)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.Live.CPU.Value = s.Static.CPU.Name
|
||||||
|
s.Live.CPU.Percentage = append(s.Live.CPU.Percentage[1:], math.RoundToEven(p[0]))
|
||||||
|
}
|
35
system/disk.go
Normal file
35
system/disk.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/dariubs/percent"
|
||||||
|
"github.com/shirou/gopsutil/v3/disk"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
func staticDisk() Storage {
|
||||||
|
var s Storage
|
||||||
|
d, err := disk.Usage("/")
|
||||||
|
if err != nil {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
total := d.Total
|
||||||
|
if total <= 0 {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
processStorage(&s, total)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *System) liveDisk() {
|
||||||
|
d, err := disk.Usage("/")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
usage := d.Used
|
||||||
|
if usage > 0 {
|
||||||
|
niceUsage := float64(usage) / s.Static.Disk.Unit
|
||||||
|
s.Live.Disk.Value = fmt.Sprintf("%.2f", niceUsage)
|
||||||
|
s.Live.Disk.Percentage = math.RoundToEven(percent.PercentOfFloat(niceUsage, s.Static.Disk.Value))
|
||||||
|
}
|
||||||
|
}
|
36
system/ram.go
Normal file
36
system/ram.go
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/dariubs/percent"
|
||||||
|
"github.com/shirou/gopsutil/v3/mem"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
func staticRam() Storage {
|
||||||
|
var s Storage
|
||||||
|
r, err := mem.VirtualMemory()
|
||||||
|
if err != nil {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
total := r.Total
|
||||||
|
if total <= 0 {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
processStorage(&s, total)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *System) liveRam() {
|
||||||
|
r, err := mem.VirtualMemory()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var niceUsage float64 = 0
|
||||||
|
used := r.Used
|
||||||
|
if used > 0 {
|
||||||
|
niceUsage = float64(used) / s.Static.Ram.Unit
|
||||||
|
s.Live.Ram.Value = fmt.Sprintf("%.2f", niceUsage)
|
||||||
|
s.Live.Ram.Percentage = math.RoundToEven(percent.PercentOfFloat(niceUsage, s.Static.Ram.Value))
|
||||||
|
}
|
||||||
|
}
|
14
system/storage.go
Normal file
14
system/storage.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/jaypipes/ghw/pkg/unitutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func processStorage(s *Storage, total uint64) {
|
||||||
|
unit, unitStr := unitutil.AmountString(int64(total))
|
||||||
|
s.Unit = float64(unit)
|
||||||
|
s.Value = float64(total) / s.Unit
|
||||||
|
s.UnitString = unitStr
|
||||||
|
s.Readable = fmt.Sprintf("%.2f %s", s.Value, s.UnitString)
|
||||||
|
}
|
38
system/system.go
Normal file
38
system/system.go
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"godash/config"
|
||||||
|
"godash/hub"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Config = SystemConfig{}
|
||||||
|
var Sys = System{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
config.ParseViperConfig(&Config, config.AddViperConfig("system"))
|
||||||
|
if Config.LiveSystem {
|
||||||
|
Sys.Initialize()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *System) UpdateLiveInformation() {
|
||||||
|
for {
|
||||||
|
s.liveCpu()
|
||||||
|
s.liveRam()
|
||||||
|
s.liveDisk()
|
||||||
|
s.uptime()
|
||||||
|
hub.LiveInformationCh <- hub.Message{WsType: hub.System, Message: s.Live}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *System) Initialize() {
|
||||||
|
s.Static.CPU = staticCpu()
|
||||||
|
s.Static.Ram = staticRam()
|
||||||
|
s.Static.Disk = staticDisk()
|
||||||
|
s.Live.CPU.Percentage = make([]float64, 120)
|
||||||
|
go s.UpdateLiveInformation()
|
||||||
|
logrus.WithFields(logrus.Fields{"cpu": s.Static.CPU.Name, "arch": s.Static.CPU.Architecture}).Debug("system updated")
|
||||||
|
}
|
46
system/types.go
Normal file
46
system/types.go
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
package system
|
||||||
|
|
||||||
|
type SystemConfig struct {
|
||||||
|
LiveSystem bool `mapstructure:"LIVE_SYSTEM"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type BasicSystemInformation struct {
|
||||||
|
Value string `json:"value" validate:"required"`
|
||||||
|
Percentage float64 `json:"percentage" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type LiveInformation struct {
|
||||||
|
CPU CpuSystemInformation `json:"cpu" validate:"required"`
|
||||||
|
Ram BasicSystemInformation `json:"ram" validate:"required"`
|
||||||
|
Disk BasicSystemInformation `json:"disk" validate:"required"`
|
||||||
|
ServerUptime uint64 `json:"server_uptime" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type StaticInformation struct {
|
||||||
|
CPU CPU `json:"cpu" validate:"required"`
|
||||||
|
Ram Storage `json:"ram" validate:"required"`
|
||||||
|
Disk Storage `json:"disk" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type System struct {
|
||||||
|
Live LiveInformation `json:"live" validate:"required"`
|
||||||
|
Static StaticInformation `json:"static" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Storage struct {
|
||||||
|
Readable string `json:"readable" validate:"required"`
|
||||||
|
Value float64 `json:"value" validate:"required"`
|
||||||
|
UnitString string `json:"unit_string" validate:"required"`
|
||||||
|
Unit float64 `json:"unit" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CPU struct {
|
||||||
|
Name string `json:"name" validate:"required"`
|
||||||
|
Threads int `json:"threads" validate:"required"`
|
||||||
|
Architecture string `json:"architecture" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CpuSystemInformation struct {
|
||||||
|
Value string `json:"value" validate:"required"`
|
||||||
|
Percentage []float64 `json:"percentage" validate:"required"`
|
||||||
|
}
|
14
system/uptime.go
Normal file
14
system/uptime.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/shirou/gopsutil/v3/host"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *System) uptime() {
|
||||||
|
i, err := host.Info()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// returns uptime in milliseconds
|
||||||
|
s.Live.ServerUptime = i.Uptime * 1000
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue