added corporations endpoints
parent
f2e2d13b9a
commit
5850a1e53d
|
|
@ -0,0 +1,108 @@
|
||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"api/database"
|
||||||
|
"api/models"
|
||||||
|
|
||||||
|
"github.com/shirou/gopsutil/v3/cpu"
|
||||||
|
"github.com/shirou/gopsutil/v3/disk"
|
||||||
|
"github.com/shirou/gopsutil/v3/host"
|
||||||
|
"github.com/shirou/gopsutil/v3/mem"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddServer - Adds a server to the database
|
||||||
|
func AddServer(c *fiber.Ctx) error {
|
||||||
|
|
||||||
|
var data map[string]string
|
||||||
|
|
||||||
|
if err := c.BodyParser(&data); err != nil {
|
||||||
|
return fiber.ErrBadRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
if data["name"] == "" || data["ip"] == "" {
|
||||||
|
return fiber.ErrBadRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
server := models.Server{
|
||||||
|
Name: data["name"],
|
||||||
|
IP: data["ip"],
|
||||||
|
}
|
||||||
|
|
||||||
|
database.DB.Create(&server)
|
||||||
|
|
||||||
|
if server.ID == 0 {
|
||||||
|
return fiber.ErrNotAcceptable
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(server)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllServers - Returns all servers
|
||||||
|
func GetAllServers(c *fiber.Ctx) error {
|
||||||
|
|
||||||
|
u := c.Locals("user").(models.User)
|
||||||
|
|
||||||
|
if u.UserType != "A" {
|
||||||
|
return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized: User is not admin")
|
||||||
|
}
|
||||||
|
|
||||||
|
var servers []models.Server
|
||||||
|
|
||||||
|
database.DB.Find(&servers)
|
||||||
|
|
||||||
|
if len(servers) == 0 {
|
||||||
|
return fiber.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(servers)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetServerInfo(c *fiber.Ctx) error {
|
||||||
|
|
||||||
|
var diskPercent float64 = 0.0
|
||||||
|
var memoryPercent float64 = 0.0
|
||||||
|
var cpuPercent float64 = 0.0
|
||||||
|
|
||||||
|
// Get info from the host
|
||||||
|
hostStat, _ := host.Info()
|
||||||
|
|
||||||
|
// Get info from the host disks
|
||||||
|
if hostStat.OS == "windows" {
|
||||||
|
var total, free uint64 = 0.0, 0.0
|
||||||
|
partitions, _ := disk.Partitions(false)
|
||||||
|
for _, partition := range partitions {
|
||||||
|
v3, _ := disk.Usage(partition.Device)
|
||||||
|
total = total + v3.Total
|
||||||
|
free = free + v3.Free
|
||||||
|
}
|
||||||
|
diskPercent = (float64(total-free) / float64(total)) * 100
|
||||||
|
} else {
|
||||||
|
diskstat, _ := disk.Usage("/")
|
||||||
|
diskPercent = diskstat.UsedPercent
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get info from the host memory
|
||||||
|
v, _ := mem.VirtualMemory()
|
||||||
|
memoryPercent = v.UsedPercent
|
||||||
|
|
||||||
|
// Get info from the host CPUs
|
||||||
|
cpus := 0
|
||||||
|
v4, _ := cpu.Percent(0, true)
|
||||||
|
for _, cpu := range v4 {
|
||||||
|
cpus++
|
||||||
|
cpuPercent = cpuPercent + cpu
|
||||||
|
}
|
||||||
|
cpuPercent = cpuPercent / float64(cpus)
|
||||||
|
|
||||||
|
c.Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
|
||||||
|
type result struct {
|
||||||
|
Memory float64 `json:"memory"`
|
||||||
|
CPU float64 `json:"cpu"`
|
||||||
|
Disk float64 `json:"disk"`
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(result{Memory: memoryPercent, CPU: cpuPercent, Disk: diskPercent})
|
||||||
|
}
|
||||||
|
|
@ -4,16 +4,11 @@ import (
|
||||||
"api/database"
|
"api/database"
|
||||||
"api/models"
|
"api/models"
|
||||||
|
|
||||||
"github.com/shirou/gopsutil/v3/cpu"
|
|
||||||
"github.com/shirou/gopsutil/v3/disk"
|
|
||||||
"github.com/shirou/gopsutil/v3/host"
|
|
||||||
"github.com/shirou/gopsutil/v3/mem"
|
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddServer - Adds a server to the database
|
// AddServer - Adds a server to the database
|
||||||
func AddServer(c *fiber.Ctx) error {
|
func AddCorporation(c *fiber.Ctx) error {
|
||||||
|
|
||||||
var data map[string]string
|
var data map[string]string
|
||||||
|
|
||||||
|
|
@ -21,88 +16,39 @@ func AddServer(c *fiber.Ctx) error {
|
||||||
return fiber.ErrBadRequest
|
return fiber.ErrBadRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
if data["name"] == "" || data["ip"] == "" {
|
if data["name"] == "" || data["description"] == "" || data["id"] == "" {
|
||||||
return fiber.ErrBadRequest
|
return fiber.ErrBadRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
server := models.Server{
|
corp := models.Corporation{
|
||||||
Name: data["name"],
|
Name: data["name"],
|
||||||
IP: data["ip"],
|
Description: data["description"],
|
||||||
|
CorporationId: data["id"],
|
||||||
}
|
}
|
||||||
|
|
||||||
database.DB.Create(&server)
|
database.DB.Create(&corp)
|
||||||
|
|
||||||
if server.ID == 0 {
|
if corp.ID == 0 {
|
||||||
return fiber.ErrNotAcceptable
|
return fiber.ErrNotAcceptable
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(server)
|
return c.JSON(corp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllServers - Returns all servers
|
// GetAllServers - Returns all servers
|
||||||
func GetAllServers(c *fiber.Ctx) error {
|
func GetCorporation(c *fiber.Ctx) error {
|
||||||
|
|
||||||
u := c.Locals("user").(models.User)
|
u := c.Locals("user").(models.User)
|
||||||
|
|
||||||
|
id := c.Params("id")
|
||||||
|
|
||||||
if u.UserType != "A" {
|
if u.UserType != "A" {
|
||||||
return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized: User is not admin")
|
return fiber.NewError(fiber.StatusUnauthorized, "Unauthorized: User is not admin")
|
||||||
}
|
}
|
||||||
|
|
||||||
var servers []models.Server
|
var corp models.Corporation
|
||||||
|
|
||||||
database.DB.Find(&servers)
|
database.DB.Find(&corp, "id = ?", id)
|
||||||
|
|
||||||
if len(servers) == 0 {
|
return c.JSON(corp)
|
||||||
return fiber.ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(servers)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetServerInfo(c *fiber.Ctx) error {
|
|
||||||
|
|
||||||
var diskPercent float64 = 0.0
|
|
||||||
var memoryPercent float64 = 0.0
|
|
||||||
var cpuPercent float64 = 0.0
|
|
||||||
|
|
||||||
// Get info from the host
|
|
||||||
hostStat, _ := host.Info()
|
|
||||||
|
|
||||||
// Get info from the host disks
|
|
||||||
if hostStat.OS == "windows" {
|
|
||||||
var total, free uint64 = 0.0, 0.0
|
|
||||||
partitions, _ := disk.Partitions(false)
|
|
||||||
for _, partition := range partitions {
|
|
||||||
v3, _ := disk.Usage(partition.Device)
|
|
||||||
total = total + v3.Total
|
|
||||||
free = free + v3.Free
|
|
||||||
}
|
|
||||||
diskPercent = (float64(total-free) / float64(total)) * 100
|
|
||||||
} else {
|
|
||||||
diskstat, _ := disk.Usage("/")
|
|
||||||
diskPercent = diskstat.UsedPercent
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get info from the host memory
|
|
||||||
v, _ := mem.VirtualMemory()
|
|
||||||
memoryPercent = v.UsedPercent
|
|
||||||
|
|
||||||
// Get info from the host CPUs
|
|
||||||
cpus := 0
|
|
||||||
v4, _ := cpu.Percent(0, true)
|
|
||||||
for _, cpu := range v4 {
|
|
||||||
cpus++
|
|
||||||
cpuPercent = cpuPercent + cpu
|
|
||||||
}
|
|
||||||
cpuPercent = cpuPercent / float64(cpus)
|
|
||||||
|
|
||||||
c.Set("Content-Type", "application/json; charset=utf-8")
|
|
||||||
|
|
||||||
type result struct {
|
|
||||||
Memory float64 `json:"memory"`
|
|
||||||
CPU float64 `json:"cpu"`
|
|
||||||
Disk float64 `json:"disk"`
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(result{Memory: memoryPercent, CPU: cpuPercent, Disk: diskPercent})
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Channel struct {
|
||||||
|
gorm.Model
|
||||||
|
Name string `gorm:"size:40;not null" json:"name"`
|
||||||
|
Description string `gorm:"not null" json:"description"`
|
||||||
|
CorporationId string
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Corporation struct {
|
||||||
|
gorm.Model
|
||||||
|
CorporationId string
|
||||||
|
Name string `gorm:"size:40;not null" json:"name"`
|
||||||
|
Description string `gorm:"not null" json:"description"`
|
||||||
|
}
|
||||||
|
|
@ -30,6 +30,8 @@ func Setup(app *fiber.App) {
|
||||||
app.Post("/on_pub_stop", controllers.OnPubStop)
|
app.Post("/on_pub_stop", controllers.OnPubStop)
|
||||||
app.Post("/on_sub_start", controllers.OnSubStart)
|
app.Post("/on_sub_start", controllers.OnSubStart)
|
||||||
|
|
||||||
|
app.Get("/corporations/:id", controllers.GetCorporation)
|
||||||
|
|
||||||
// Protected routes. Needs login before.
|
// Protected routes. Needs login before.
|
||||||
protected := app.Group("/")
|
protected := app.Group("/")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue