feat: package updater

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2023-09-16 04:56:32 +03:00
parent cd1ea5495e
commit e9aafd0cea
2 changed files with 57 additions and 2 deletions

View File

@ -9,7 +9,41 @@ import (
)
// Create a new updater. Invoke Run().
func InitPkgUpdater() (PackageUpdater, error) {
alpine := isAlpine()
if alpine {
log.Println("Detected Alpine. Setting up Alpine Package Updater.")
return NewAlpineLinuxPackageUpdater(
time.Hour * 24,
[]string{"yt-dlp", "ffmpeg"},
), nil
}
log.Println("OS not known. Using dummy Package Updater")
return NewDummyPackageUpdater(), nil
}
// Returns true if the os running the application
// is using Alpine Linux. If an error occurs it
// returns false
func isAlpine() bool {
cmd := exec.Command("grep", "Alpine", "/etc/os-release")
_, err := cmd.Output()
if err != nil || cmd.ProcessState.ExitCode() != 0 { return false }
return true
}
/*
Provides a package updater to run along side
Application. It updates a set of packages at
runtime. Run() starts a goroutine and returns.
Stop() signals stop to the running goroutine.
*/
type PackageUpdater interface {
Update()
Run()
@ -70,8 +104,8 @@ func (u *AlpineLinuxPackageUpdate) Stop() {
func NewAlpineLinuxPackageUpdater (
interval time.Duration,
packages []string,
) AlpineLinuxPackageUpdate {
return AlpineLinuxPackageUpdate{
) PackageUpdater {
return &AlpineLinuxPackageUpdate{
Interval: interval,
Packages: packages,
doneChan: make(chan string),
@ -79,3 +113,13 @@ func NewAlpineLinuxPackageUpdater (
}
}
type DummyPackageUpdater struct {}
func (u *DummyPackageUpdater) Run() {}
func (u *DummyPackageUpdater) Stop() {}
func (u *DummyPackageUpdater) ValidPackages() bool { return true }
func (u *DummyPackageUpdater) Update() {}
func NewDummyPackageUpdater() PackageUpdater {
return &DummyPackageUpdater{}
}

11
main.go
View File

@ -11,6 +11,8 @@ import (
"os"
"strings"
"time"
"gitea.hbanafa.com/hesham/viddl/apkupdater"
)
const (
@ -154,6 +156,13 @@ func init() {
func main() {
updater, err := apkupdater.InitPkgUpdater()
if err != nil {
log.Println("Could not init Package Updater!\n", err.Error())
}
updater.Run()
handler := http.NewServeMux()
handler.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(PublicFS))))
handler.Handle("/assets/", http.FileServer(http.FS(AssetsFS)))
@ -369,6 +378,8 @@ func main() {
log.Printf("Starting HTTP on %s", DEFAULT_HTTP_PORT)
log.Fatalln(srv.ListenAndServe())
log.Println("HTTP server stopped")
updater.Stop()
}