tracker/routes/index.go

76 lines
1.2 KiB
Go
Raw Normal View History

package routes
import (
"strings"
"time"
"git.matterlinux.xyz/matter/tracker/lib"
"github.com/gofiber/fiber/v2"
)
func GET_index(c *fiber.Ctx) error {
var (
result []lib.Package
list *[]lib.Package
config *lib.Config
pools_str string
pools []string
last *time.Time
)
result = []lib.Package{}
pools = []string{}
config = c.Locals("config").(*lib.Config)
list = c.Locals("list").(*[]lib.Package)
last = c.Locals("last").(*time.Time)
is_exact := c.Query("exact") == "1"
is_json := c.Query("json") == "1"
if pools_str = c.Query("pools"); pools_str != "" {
pools = strings.Split(pools_str, ",")
}
query := c.Query("q")
for _, pkg := range *list {
found := false
if is_exact && query != "" && pkg.Name != query {
continue
}
if !is_exact && query != "" && !strings.Contains(pkg.Name, query) {
continue
}
if len(pools) != 0 {
for _, p := range pools {
2024-08-25 21:59:15 +00:00
if p == pkg.Pool.ID() {
found = true
break
}
}
} else {
found = true
}
if !found {
continue
}
result = append(result, pkg)
}
if is_json {
return c.JSON(result)
}
return c.Render("index", &fiber.Map{
"query": query,
"list": result,
"last": lib.TimePassed(*last),
"pools": config.Pools,
})
}