diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..71d075c --- /dev/null +++ b/.drone.yml @@ -0,0 +1,25 @@ +kind: pipeline +name: default + +workspace: + base: /go + path: src/deadbeef.codes/steven/lineageos-ota-server + +steps: + +- name: build server + image: golang + pull: always + environment: + GOOS: linux + GOARCH: amd64 + CGO_ENABLED: 0 + commands: + - go version + - go get + - go build -a -ldflags '-w' + +- name: package in docker container + image: plugins/docker + settings: + repo: registry.deadbeef.codes/masterlist diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..56fa19e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM scratch +LABEL maintainer="himself@stevenpolley.net" + +COPY lineageos-ota-server . + +EXPOSE 8080 + +CMD [ "./linageos-ota-server" ] diff --git a/main.go b/main.go new file mode 100644 index 0000000..483fc03 --- /dev/null +++ b/main.go @@ -0,0 +1,115 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/fs" + "log" + "net/http" + "os" + "path/filepath" + "strings" +) + +// 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"` +} + +// The HTTP response JSON should be a JSON array of lineageOSROMS available for download +type HttpResponseJSON struct { + Response []LineageOSROM `json:"response"` +} + +// HTTP Routing +func main() { + + //Public static files + http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public")))) + + // ROM list + http.HandleFunc("/lineage", lineageOSROMListHandler) + + 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 +func getLineageOSROMs(romDirectory string) ([]LineageOSROM, error) { + + if _, err := os.Stat(romDirectory); os.IsNotExist(err) { + return nil, fmt.Errorf("romDirectory '%s' does not exist", romDirectory) + } + + // Get all the zip files and populate romFileNames slice + var lineageOSROMs []LineageOSROM + + err := filepath.WalkDir(romDirectory, func(s string, d fs.DirEntry, err error) error { + if err != nil { + return fmt.Errorf("walk error occured during file '%s': %v", d.Name(), err) + } + if filepath.Ext(d.Name()) != ".zip" { + return nil + } + + fInfo, err := d.Info() + if err != nil { + return fmt.Errorf("failed to get file info '%s': %v", d.Name(), err) + } + + // Get information about file and populate rom + + splitName := strings.Split(d.Name(), "-") + if len(splitName) != 5 { + log.Printf("ignoring zip file '%d', name is not formatted correctly ") + } + + lineageOSROM := LineageOSROM{ + Datetime: int(fInfo.ModTime().Unix()), + Filename: d.Name(), + ID: "TBD", + Romtype: "nightly", + Size: int(fInfo.Size()), + URL: fmt.Sprintf("https://lineageos-updater.deadbeef.codes/public/%s", d.Name()), + Version: "TBD", + } + + lineageOSROMs = append(lineageOSROMs, lineageOSROM) + + return nil + }) + if err != nil { + return nil, fmt.Errorf("failed to walk romDirectory '%s': %v", romDirectory, err) + } + + return lineageOSROMs, nil +} + +// 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) { + lineageOSROMs, err := getLineageOSROMs("public") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + log.Printf("failed to get lineageOSROMs: %v", err) + return + } + + httpResponseJSON := &HttpResponseJSON{Response: lineageOSROMs} + + b, err := json.Marshal(httpResponseJSON) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + log.Printf("failed to marshal lineageOSROMs to json: %v", err) + return + } + + w.Write(b) +} diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..a74a571 --- /dev/null +++ b/public/index.html @@ -0,0 +1,8 @@ + + + LineageOS OTA Server + + +

This is the public folder - the build artifact directory should be mounted to this folder so ota updates can be served.

+ + \ No newline at end of file