tracker/main.go

123 lines
2.6 KiB
Go
Raw Normal View History

2024-01-17 17:06:26 +00:00
/*
* tracker | MatterLinux package tracker
2024-03-19 20:11:43 +00:00
* MatterLinux 2023-2024 (https://matterlinux.xyz)
2024-01-17 17:06:26 +00:00
* 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/>.
2024-03-19 20:11:43 +00:00
*/
2024-01-17 17:06:26 +00:00
package main
import (
"time"
"git.matterlinux.xyz/matter/tracker/lib"
"git.matterlinux.xyz/matter/tracker/log"
"git.matterlinux.xyz/matter/tracker/routes"
2024-01-17 17:06:26 +00:00
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
)
type tracker struct {
Channel chan struct{}
Config lib.Config
Last time.Time
List []lib.Package
2024-11-22 13:56:02 +00:00
Tick *time.Ticker
2024-01-17 17:06:26 +00:00
}
func (t *tracker) Reload() error {
t.List = []lib.Package{}
t.Last = time.Now()
2024-11-22 13:56:02 +00:00
for _, p := range t.Config.Pools {
if err := p.Load(&t.List); err != nil {
return err
}
}
return nil
2024-01-17 17:06:26 +00:00
}
func (t *tracker) Loop() {
2024-11-22 13:56:02 +00:00
if err := t.Config.Load(&t.List, "config.json"); err != nil {
log.Error("Failed to load the configuration: %s", err.Error())
return
}
2024-11-22 13:56:02 +00:00
if err := t.Reload(); err != nil {
log.Error("Failed to update packages: %s", err.Error())
2024-11-22 13:56:02 +00:00
return
}
2024-11-22 13:56:02 +00:00
t.Tick = time.NewTicker(time.Minute * time.Duration(t.Config.Interval))
for {
select {
case <-t.Tick.C:
2024-11-22 13:56:02 +00:00
if err := t.Reload(); err != nil {
log.Error("Failed to update packages: %s", err.Error())
}
2024-11-22 13:56:02 +00:00
case <-t.Channel:
t.Tick.Stop()
return
}
}
2024-01-17 17:06:26 +00:00
}
func (t *tracker) Stop() {
close(t.Channel)
}
func main() {
var (
tracker tracker
app *fiber.App
2024-11-22 13:56:02 +00:00
err error
)
app = fiber.New(fiber.Config{
DisableStartupMessage: true,
2024-11-22 13:56:02 +00:00
Views: html.New("./templates", ".html"),
})
app.Static("/", "./public")
app.All("*", func(c *fiber.Ctx) error {
c.Locals("config", &tracker.Config)
c.Locals("list", &tracker.List)
c.Locals("last", &tracker.Last)
return c.Next()
})
app.Get("/", routes.GET_index)
2024-08-13 19:12:27 +00:00
app.Get("/p/:name/:version", routes.GET_package)
app.Get("*", func(c *fiber.Ctx) error {
return lib.RenderError(c, 404)
})
go tracker.Loop()
log.Info("Starting MatterLinux package tracker on port 9877")
2024-11-22 13:56:02 +00:00
if err = app.Listen(":9877"); err != nil {
log.Error("Error starting server: %s", err.Error())
}
tracker.Stop()
2024-01-17 17:06:26 +00:00
}