128 lines
2.7 KiB
Go
128 lines
2.7 KiB
Go
/*
|
|
|
|
* tracker | MatterLinux Package Tracker
|
|
* MatterLinux 2023-2024 (https://matterlinux.xyz)
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.matterlinux.xyz/matter/tracker/lib"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/template/html/v2"
|
|
)
|
|
|
|
var lastupdate time.Time
|
|
var updatetick = time.NewTicker(time.Hour)
|
|
var stopchan = make(chan struct{})
|
|
|
|
func UpdateLoop() {
|
|
UpdatePackages()
|
|
|
|
for {
|
|
select {
|
|
case <- updatetick.C:
|
|
UpdatePackages()
|
|
case <- stopchan:
|
|
updatetick.Stop()
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func UpdatePackages() {
|
|
lib.LoadAllPkgs()
|
|
lastupdate = time.Now()
|
|
}
|
|
|
|
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,
|
|
"pkgs": lib.Packages,
|
|
})
|
|
}
|
|
|
|
name = lib.CleanString(name)
|
|
var res []lib.Package
|
|
|
|
for _, p := range lib.Packages {
|
|
if(repo != "all" && p.Repo != repo){
|
|
continue
|
|
}
|
|
|
|
if(exact == ""){
|
|
if(strings.Contains(p.Name, name)){
|
|
res = append(res, p)
|
|
}
|
|
}else {
|
|
if (p.Name == name) {
|
|
res = append(res, p)
|
|
}
|
|
}
|
|
}
|
|
|
|
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(){
|
|
log.SetFlags(log.Ltime | log.Lshortfile)
|
|
|
|
engine := html.New("./templates", ".html")
|
|
app := fiber.New(fiber.Config{
|
|
DisableStartupMessage: true,
|
|
Views: engine,
|
|
})
|
|
|
|
app.Static("/", "./public")
|
|
app.Get("/", GETIndex)
|
|
|
|
app.Get("*", func(c *fiber.Ctx) error {
|
|
return lib.RenderError(c, 404)
|
|
})
|
|
|
|
go UpdateLoop()
|
|
log.Println("Starting MatterLinux Package Tracker on port 9877")
|
|
err := app.Listen(":9877")
|
|
if err != nil {
|
|
log.Printf("Error starting server: %s", err)
|
|
}
|
|
close(stopchan)
|
|
}
|