diff --git a/lib/repo.go b/lib/repo.go index 303547b..ab77d73 100644 --- a/lib/repo.go +++ b/lib/repo.go @@ -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 } diff --git a/lib/util.go b/lib/util.go index 2938646..7be9e4d 100644 --- a/lib/util.go +++ b/lib/util.go @@ -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) diff --git a/main.go b/main.go index 430cf6d..55fe711 100644 --- a/main.go +++ b/main.go @@ -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(){ diff --git a/templates/index.html b/templates/index.html index 82739ef..221b7f7 100644 --- a/templates/index.html +++ b/templates/index.html @@ -45,7 +45,7 @@