feat: Sprint 1 — host model, mock store, dashboard CRUD, HTMX partials
This commit is contained in:
@@ -1,10 +1,20 @@
|
||||
package handler
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"git.tukangketik.id/swanadiva/hostkeeper/v2/internal/model"
|
||||
)
|
||||
|
||||
var store = model.NewHostStore()
|
||||
|
||||
func Dashboard(c *fiber.Ctx) error {
|
||||
hosts := store.All()
|
||||
return c.Render("index", fiber.Map{
|
||||
"View": "hosts",
|
||||
"View": "hosts",
|
||||
"Title": "Hosts",
|
||||
"Hosts": hosts,
|
||||
"Active": countByStatus(hosts, "active"),
|
||||
"Offline": countByStatus(hosts, "offline"),
|
||||
"NavItems": []fiber.Map{
|
||||
{"ID": "hosts", "Label": "Hosts", "Icon": "server"},
|
||||
{"ID": "terminal", "Label": "Terminal", "Icon": "terminal"},
|
||||
@@ -13,6 +23,46 @@ func Dashboard(c *fiber.Ctx) error {
|
||||
{"ID": "keychain", "Label": "Keychain", "Icon": "key-round"},
|
||||
{"ID": "settings", "Label": "Settings", "Icon": "settings2"},
|
||||
},
|
||||
"Title": "Hosts",
|
||||
})
|
||||
}
|
||||
|
||||
func DashboardList(c *fiber.Ctx) error {
|
||||
q := c.Query("q")
|
||||
filter := c.Query("filter", "all")
|
||||
hosts := store.Search(q, filter)
|
||||
return c.Render("host_list", fiber.Map{
|
||||
"Hosts": hosts,
|
||||
"Active": countByStatus(hosts, "active"),
|
||||
"Offline": countByStatus(hosts, "offline"),
|
||||
})
|
||||
}
|
||||
|
||||
func CreateHost(c *fiber.Ctx) error {
|
||||
name := c.FormValue("name")
|
||||
ip := c.FormValue("ip")
|
||||
os := c.FormValue("os")
|
||||
provider := c.FormValue("provider")
|
||||
hostType := c.FormValue("type")
|
||||
|
||||
if name == "" || ip == "" {
|
||||
return c.Status(400).SendString("name and ip required")
|
||||
}
|
||||
|
||||
store.Add(name, ip, os, provider, hostType)
|
||||
hosts := store.All()
|
||||
return c.Render("host_list", fiber.Map{
|
||||
"Hosts": hosts,
|
||||
"Active": countByStatus(hosts, "active"),
|
||||
"Offline": countByStatus(hosts, "offline"),
|
||||
})
|
||||
}
|
||||
|
||||
func countByStatus(hosts []model.Host, status string) int {
|
||||
n := 0
|
||||
for _, h := range hosts {
|
||||
if h.Status == status {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user