Files
HostKeeper/app/backend/internal/model/host.go
T

97 lines
2.9 KiB
Go

package model
import (
"math/rand"
"strings"
"time"
)
type Host struct {
ID string `json:"id"`
Name string `json:"name"`
IP string `json:"ip"`
OS string `json:"os"`
Provider string `json:"provider"`
Status string `json:"status"` // active | offline
LastSeen string `json:"lastSeen"`
Type string `json:"type"` // api | db | edge | web | desktop | server
}
type HostStore struct {
Hosts []Host
}
func NewHostStore() *HostStore {
return &HostStore{
Hosts: defaultHosts(),
}
}
func (s *HostStore) All() []Host {
return s.Hosts
}
func (s *HostStore) Search(q, filter string) []Host {
var result []Host
for _, h := range s.Hosts {
if filter != "" && filter != "all" && h.Status != filter {
continue
}
if q == "" || strings.Contains(strings.ToLower(h.Name), strings.ToLower(q)) ||
strings.Contains(strings.ToLower(h.IP), strings.ToLower(q)) ||
strings.Contains(strings.ToLower(h.OS), strings.ToLower(q)) {
result = append(result, h)
}
}
if result == nil {
return []Host{}
}
return result
}
func (s *HostStore) Delete(id string) {
for i, h := range s.Hosts {
if h.ID == id {
s.Hosts = append(s.Hosts[:i], s.Hosts[i+1:]...)
return
}
}
}
func (s *HostStore) Add(name, ip, os, provider, hostType string) Host {
h := Host{
ID: randString(8),
Name: name,
IP: ip,
OS: os,
Provider: provider,
Status: "active",
LastSeen: time.Now().Format("Jan 2, 2006"),
Type: hostType,
}
s.Hosts = append(s.Hosts, h)
return h
}
func randString(n int) string {
const letters = "abcdefghijklmnopqrstuvwxyz0123456789"
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func defaultHosts() []Host {
return []Host{
{ID: "h1", Name: "api-prod-01", IP: "10.0.1.15", OS: "Ubuntu 22.04", Provider: "AWS US-East", Status: "active", LastSeen: "Online", Type: "api"},
{ID: "h2", Name: "db-primary-01", IP: "10.0.2.5", OS: "Debian 12", Provider: "AWS US-East", Status: "active", LastSeen: "Online", Type: "db"},
{ID: "h3", Name: "web-edge-02", IP: "192.168.1.20", OS: "Alpine 3.19", Provider: "DigitalOcean", Status: "offline", LastSeen: "2 hours ago", Type: "edge"},
{ID: "h4", Name: "staging-api-01", IP: "10.0.3.10", OS: "Ubuntu 22.04", Provider: "AWS EU-West", Status: "active", LastSeen: "5 min ago", Type: "api"},
{ID: "h5", Name: "dev-db-02", IP: "192.168.1.45", OS: "Fedora 39", Provider: "Hetzner", Status: "offline", LastSeen: "1 day ago", Type: "db"},
{ID: "h6", Name: "monitor-01", IP: "10.0.0.5", OS: "Ubuntu 24.04", Provider: "AWS US-East", Status: "active", LastSeen: "Just now", Type: "server"},
{ID: "h7", Name: "worker-pool-01", IP: "10.0.4.50", OS: "Debian 12", Provider: "GCP US-Central", Status: "active", LastSeen: "1 min ago", Type: "server"},
{ID: "h8", Name: "bastion-host", IP: "54.85.12.7", OS: "Ubuntu 22.04", Provider: "AWS US-East", Status: "offline", LastSeen: "3 days ago", Type: "server"},
}
}