feat: package updater
Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
		
							parent
							
								
									cd1ea5495e
								
							
						
					
					
						commit
						e9aafd0cea
					
				@ -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 {
 | 
					type PackageUpdater interface {
 | 
				
			||||||
    Update()
 | 
					    Update()
 | 
				
			||||||
    Run()
 | 
					    Run()
 | 
				
			||||||
@ -70,8 +104,8 @@ func (u *AlpineLinuxPackageUpdate) Stop() {
 | 
				
			|||||||
func NewAlpineLinuxPackageUpdater (
 | 
					func NewAlpineLinuxPackageUpdater (
 | 
				
			||||||
    interval time.Duration,
 | 
					    interval time.Duration,
 | 
				
			||||||
    packages []string,
 | 
					    packages []string,
 | 
				
			||||||
) AlpineLinuxPackageUpdate {
 | 
					) PackageUpdater {
 | 
				
			||||||
        return AlpineLinuxPackageUpdate{
 | 
					        return &AlpineLinuxPackageUpdate{
 | 
				
			||||||
            Interval: interval,
 | 
					            Interval: interval,
 | 
				
			||||||
            Packages: packages,
 | 
					            Packages: packages,
 | 
				
			||||||
            doneChan: make(chan string),
 | 
					            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
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								main.go
									
									
									
									
									
								
							@ -11,6 +11,8 @@ import (
 | 
				
			|||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"gitea.hbanafa.com/hesham/viddl/apkupdater"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
@ -154,6 +156,13 @@ func init() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					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 := http.NewServeMux()
 | 
				
			||||||
    handler.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(PublicFS))))
 | 
					    handler.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(PublicFS))))
 | 
				
			||||||
    handler.Handle("/assets/", http.FileServer(http.FS(AssetsFS)))
 | 
					    handler.Handle("/assets/", http.FileServer(http.FS(AssetsFS)))
 | 
				
			||||||
@ -369,6 +378,8 @@ func main() {
 | 
				
			|||||||
    
 | 
					    
 | 
				
			||||||
    log.Printf("Starting HTTP on %s", DEFAULT_HTTP_PORT)
 | 
					    log.Printf("Starting HTTP on %s", DEFAULT_HTTP_PORT)
 | 
				
			||||||
    log.Fatalln(srv.ListenAndServe())
 | 
					    log.Fatalln(srv.ListenAndServe())
 | 
				
			||||||
 | 
					    log.Println("HTTP server stopped")
 | 
				
			||||||
 | 
					    updater.Stop()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user