tracker/main.go

112 lines
2.4 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
Tick time.Ticker
2024-01-17 17:06:26 +00:00
}
func (t *tracker) Reload() error {
t.List = []lib.Package{}
err := t.Config.Load(&t.List, "config.json")
t.Last = time.Now()
return err
2024-01-17 17:06:26 +00:00
}
func (t *tracker) Loop() {
var err error
if err = t.Reload(); err != nil {
log.Error("Failed to update packages: %s", err.Error())
}
for {
select {
case <-t.Tick.C:
if err = t.Reload(); err != nil {
log.Error("Failed to update packages: %s", err.Error())
}
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
engine *html.Engine
app *fiber.App
)
engine = html.New("./templates", ".html")
app = fiber.New(fiber.Config{
DisableStartupMessage: true,
Views: engine,
})
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)
app.Get("/p/:name", 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")
err := app.Listen(":9877")
if err != nil {
log.Error("Error starting server: %s", err.Error())
}
tracker.Stop()
2024-01-17 17:06:26 +00:00
}