update: Add JSON query support

This commit is contained in:
ngn 2024-03-21 21:52:14 +03:00
parent 67b6f22fb0
commit 787727b425
4 changed files with 36 additions and 10 deletions

View File

@ -8,6 +8,7 @@ import (
"net/url"
"os"
"strconv"
"strings"
"github.com/bigkevmcd/go-configparser"
"github.com/gofiber/fiber/v2"
@ -27,13 +28,17 @@ type Repo struct {
var Packages []Package
type Package struct {
Name string
Repo string
Desc string
Size string
Deps string
URL string
Ver string
Name string `json:"name"`
Repo string `json:"repo"`
Desc string `json:"desc"`
Size string `json:"size"`
Deps []string `json:"depends"`
URL string `json:"url"`
Ver string `json:"version"`
}
func (p Package) StrDeps() string {
return ListToStr(p.Deps)
}
func LoadPackgae(repo *configparser.ConfigParser, s string) (Package, error) {
@ -64,11 +69,17 @@ func LoadPackgae(repo *configparser.ConfigParser, s string) (Package, error) {
}
pkg.Size = SizeFromBytes(size)
pkg.Deps, err = repo.Get(s, "depends")
deps, err := repo.Get(s, "depends")
if err != nil {
return pkg, err
}
if deps == "" {
pkg.Deps = []string{}
}else {
pkg.Deps = strings.Split(deps, "\n")
}
return pkg, nil
}

View File

@ -13,6 +13,14 @@ import (
"github.com/gofiber/fiber/v2"
)
func ListToStr(l []string) string {
res := ""
for _, e := range l {
res += e+" "
}
return res
}
func RenderError(c *fiber.Ctx, code int) error{
var msg string = "Server Error"
c.Status(code)

View File

@ -57,8 +57,12 @@ func GETIndex(c *fiber.Ctx) error {
repo := c.Query("r")
name := c.Query("n")
exact := c.Query("e")
isjson := c.Query("j")
if repo == "" && name == "" {
if isjson == "1" {
return c.JSON(lib.Packages)
}
return c.Render("index", fiber.Map{
"last": lib.GetTimePassed(lastupdate),
"repos": lib.Repos,
@ -85,13 +89,16 @@ func GETIndex(c *fiber.Ctx) error {
}
}
if isjson == "1" {
return c.JSON(res)
}
return c.Render("index", fiber.Map{
"search": name,
"last": lib.GetTimePassed(lastupdate),
"repos": lib.Repos,
"pkgs": res,
})
}
func main(){

View File

@ -45,7 +45,7 @@
<td>{{.Ver }}</td>
<td>{{.Size}}</td>
<td>{{.Desc}}</td>
<td>{{.Deps}}</td>
<td>{{.StrDeps}}</td>
</tr>
{{end}}
</table>