first commit
This commit is contained in:
119
main.go
Normal file
119
main.go
Normal file
@ -0,0 +1,119 @@
|
||||
/*
|
||||
|
||||
* tracker | MatterLinux Repo Tracker
|
||||
* Copyright (C) 2024 Matterlinux
|
||||
|
||||
* 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"
|
||||
)
|
||||
|
||||
type PostData struct {
|
||||
Repo string `form:"repo"`
|
||||
Name string `form:"name"`
|
||||
}
|
||||
|
||||
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 {
|
||||
return c.Render("index", fiber.Map{
|
||||
"last": lib.GetTimePassed(lastupdate),
|
||||
"repos": lib.Repos,
|
||||
"pkgs": lib.Packages,
|
||||
})
|
||||
}
|
||||
|
||||
func POSTIndex(c *fiber.Ctx) error {
|
||||
var pdata PostData
|
||||
err := c.BodyParser(&pdata)
|
||||
if err != nil || pdata.Name == ""{
|
||||
return c.Redirect("/")
|
||||
}
|
||||
|
||||
var res []lib.Package
|
||||
pdata.Name = lib.CleanString(pdata.Name)
|
||||
|
||||
for _, p := range lib.Packages {
|
||||
if(pdata.Repo != "all" && p.Repo != pdata.Repo){
|
||||
continue
|
||||
}
|
||||
|
||||
if(strings.Contains(p.Name, pdata.Name)){
|
||||
res = append(res, p)
|
||||
}
|
||||
}
|
||||
|
||||
return c.Render("index", fiber.Map{
|
||||
"search": pdata.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.Post("/", POSTIndex)
|
||||
|
||||
app.Get("*", func(c *fiber.Ctx) error {
|
||||
return lib.RenderError(c, 404)
|
||||
})
|
||||
|
||||
go UpdateLoop()
|
||||
log.Info("Starting MatterLinux Tracker on port 9877")
|
||||
err := app.Listen(":9877")
|
||||
if err != nil {
|
||||
log.Errorf("Error starting server: %s", err)
|
||||
}
|
||||
close(stopchan)
|
||||
}
|
Reference in New Issue
Block a user