Compare commits
2 Commits
464f4d09e7
...
9a2e992964
Author | SHA1 | Date | |
---|---|---|---|
9a2e992964 | |||
449d48258d |
46
main.go
46
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
|
||||
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
|
||||
@ -153,7 +153,9 @@ 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()))
|
||||
romCache.Unlock()
|
||||
if err != nil {
|
||||
log.Printf("failed to move file '%s' from out to rom directory: %v", v.Name(), err)
|
||||
continue
|
||||
@ -182,11 +184,16 @@ func isLineageROMZip(v fs.DirEntry) (bool, []string) {
|
||||
// http - GET /
|
||||
// Writes JSON response for the updater app to know what versions are available to download
|
||||
func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) {
|
||||
romCache.Lock()
|
||||
lineageOSROMs := romCache.ROMs
|
||||
romCache.Unlock()
|
||||
go func() {
|
||||
newBuilds := moveBuildArtifacts("out", "public")
|
||||
if newBuilds {
|
||||
updateROMCache("public")
|
||||
}
|
||||
}()
|
||||
|
||||
httpResponseJSON := &HTTPResponseJSON{Response: lineageOSROMs}
|
||||
romCache.Lock()
|
||||
httpResponseJSON := &HTTPResponseJSON{Response: romCache.ROMs}
|
||||
romCache.Unlock()
|
||||
|
||||
b, err := json.Marshal(httpResponseJSON)
|
||||
if err != nil {
|
||||
@ -196,13 +203,6 @@ func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
w.Write(b)
|
||||
|
||||
go func() {
|
||||
newBuilds := moveBuildArtifacts("out", "public")
|
||||
if newBuilds {
|
||||
updateROMCache("public")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Returns a sha256 hash of a file located at the path provided
|
||||
|
Loading…
x
Reference in New Issue
Block a user