diff --git a/main.go b/main.go index 1df03dd..4651454 100644 --- a/main.go +++ b/main.go @@ -15,13 +15,13 @@ import ( // Information for a LineageOS ROM available for download type LineageOSROM struct { - Datetime int `json:"datetime"` - Filename string `json:"filename"` - ID string `json:"id"` - Romtype string `json:"romtype"` - Size int `json:"size"` - URL string `json:"url"` - Version string `json:"version"` + Datetime int `json:"datetime"` // Unix timestamp - i.e. 1685907926 + Filename string `json:"filename"` // .zip filename - i.e. lineage-20.0-20230604-UNOFFICIAL-sunfish.zip + ID string `json:"id"` // A unique identifier such as a SHA256 hash of the .zip - i.e. 603bfc02e403e5fd1bf9ed74383f1d6c9ec7fb228d03c4b37753033d79488e93 + Romtype string `json:"romtype"` // i.e. nightly + Size int `json:"size"` // size of .zip file in bytes + URL string `json:"url"` // public URL where client could download the .zip file + Version string `json:"version"` // LineageOS version - i.e. 20.0 } // The HTTP response JSON should be a JSON array of lineageOSROMS available for download @@ -29,26 +29,27 @@ type HTTPResponseJSON struct { Response []LineageOSROM `json:"response"` } +// Caches data about available ROMs in memory so we don't need to reference the filesystem for each request type ROMCache struct { - ROMs []LineageOSROM - Cached map[string]bool // to quickly lookup if a file is already cached - sync.Mutex + ROMs []LineageOSROM + Cached map[string]bool // to quickly lookup if a file is already cached + sync.Mutex // We have multiple goroutines that may be accessing this data simultaneously, so we much lock / unlock it to prevent race conditions } -var ( +var ( // evil global variable romCache ROMCache ) -// Preload cached list of files and hashes func init() { romCache = ROMCache{} romCache.Cached = make(map[string]bool) + // Check if any new build artifacts and preload the romCache moveBuildArtifacts("out", "public") go updateROMCache("public") } -// HTTP Routing +// HTTP Server func main() { //Public static files @@ -59,7 +60,6 @@ func main() { log.Print("Service listening on :8080") log.Fatal(http.ListenAndServe(":8080", nil)) - } // Reads the ROM files on the filesystem and populates a slice of linageOSROMs