initial commit

This commit is contained in:
Steven Polley 2023-06-03 23:07:55 -06:00
parent 4623a6ada1
commit c3fac18a46
4 changed files with 156 additions and 0 deletions

25
.drone.yml Normal file
View File

@ -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

8
Dockerfile Normal file
View File

@ -0,0 +1,8 @@
FROM scratch
LABEL maintainer="himself@stevenpolley.net"
COPY lineageos-ota-server .
EXPOSE 8080
CMD [ "./linageos-ota-server" ]

115
main.go Normal file
View File

@ -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)
}

8
public/index.html Normal file
View File

@ -0,0 +1,8 @@
<html>
<head>
<title>LineageOS OTA Server</title>
</head>
<body>
<p>This is the public folder - the build artifact directory should be mounted to this folder so ota updates can be served.</p>
</body>
</html>