configure directories as constants
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2023-06-04 16:15:13 -06:00
parent ce2087e638
commit 1694ce0d04

21
main.go
View File

@ -36,6 +36,11 @@ type ROMCache struct {
sync.Mutex // We have multiple goroutines that may be accessing this data simultaneously, so we much lock / unlock it to prevent race conditions
}
const (
romDirectory = "public" // directory where ROMs are available for download
buildOutDirectory = "out" // directory from build system containing artifacts which we can move to romDirectory
)
var ( // evil global variable
romCache ROMCache
)
@ -45,8 +50,8 @@ func init() {
romCache.Cached = make(map[string]bool)
// Check if any new build artifacts and preload the romCache
moveBuildArtifacts("out", "public")
go updateROMCache("public")
moveBuildArtifacts()
go updateROMCache()
}
// HTTP Server
@ -63,7 +68,7 @@ func main() {
}
// Reads the ROM files on the filesystem and populates a slice of linageOSROMs
func updateROMCache(romDirectory string) {
func updateROMCache() {
log.Printf("updating ROM cache")
@ -130,9 +135,9 @@ func updateROMCache(romDirectory string) {
}
// returns true if new builds were moved
func moveBuildArtifacts(outDirectory, romDirectory string) bool {
func moveBuildArtifacts() bool {
f, err := os.Open(outDirectory)
f, err := os.Open(buildOutDirectory)
if err != nil {
log.Printf("failed to open ROM directory: %v", err)
return false
@ -154,7 +159,7 @@ func moveBuildArtifacts(outDirectory, romDirectory string) bool {
newROMs = true
log.Printf("new build found - moving file %s", v.Name())
romCache.Lock() // lock to prevent multiple concurrent goroutines moving the same file
err := moveBuildFile(fmt.Sprintf("%s/%s", outDirectory, v.Name()), fmt.Sprintf("%s/%s", romDirectory, v.Name()))
err := moveBuildFile(fmt.Sprintf("%s/%s", buildOutDirectory, v.Name()), fmt.Sprintf("%s/%s", romDirectory, v.Name()))
romCache.Unlock()
if err != nil {
log.Printf("failed to move file '%s' from out to rom directory: %v", v.Name(), err)
@ -185,9 +190,9 @@ func isLineageROMZip(v fs.DirEntry) (bool, []string) {
// Writes JSON response for the updater app to know what versions are available to download
func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) {
go func() {
newBuilds := moveBuildArtifacts("out", "public")
newBuilds := moveBuildArtifacts()
if newBuilds {
updateROMCache("public")
updateROMCache()
}
}()