From e9aafd0cea6320582e4bb8042775f68ab11ba649 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sat, 16 Sep 2023 04:56:32 +0300 Subject: [PATCH] feat: package updater Signed-off-by: HeshamTB --- apkupdater/apkupdater.go | 48 ++++++++++++++++++++++++++++++++++++++-- main.go | 11 +++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/apkupdater/apkupdater.go b/apkupdater/apkupdater.go index e0ae81d..3b7be44 100644 --- a/apkupdater/apkupdater.go +++ b/apkupdater/apkupdater.go @@ -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{} +} + diff --git a/main.go b/main.go index a636b6a..b951c00 100644 --- a/main.go +++ b/main.go @@ -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() }