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

View File

@ -13,6 +13,14 @@ import (
"github.com/gofiber/fiber/v2" "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{ func RenderError(c *fiber.Ctx, code int) error{
var msg string = "Server Error" var msg string = "Server Error"
c.Status(code) c.Status(code)

View File

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

View File

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