tracker/main.go
2024-03-19 22:57:54 +03:00

119 lines
2.5 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 (
"strings"
"time"
"git.matterlinux.xyz/matter/tracker/lib"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"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")
if repo == "" && name == "" {
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)
}
}
}
return c.Render("index", fiber.Map{
"search": name,
"last": lib.GetTimePassed(lastupdate),
"repos": lib.Repos,
"pkgs": res,
})
}
func main(){
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.Info("Starting MatterLinux Package Tracker on port 9877")
err := app.Listen(":9877")
if err != nil {
log.Errorf("Error starting server: %s", err)
}
close(stopchan)
}