cmd/pkgserver: add /status endpoint

This commit is contained in:
mae
2026-03-10 04:24:49 -05:00
parent 52a4e5b87d
commit 469bd1ee99
5 changed files with 37 additions and 23 deletions

View File

@@ -8,9 +8,9 @@ import (
"net/http" "net/http"
"path" "path"
"strconv" "strconv"
"strings"
"hakurei.app/internal/info" "hakurei.app/internal/info"
"hakurei.app/internal/pkg"
"hakurei.app/internal/rosa" "hakurei.app/internal/rosa"
) )
@@ -36,14 +36,14 @@ func serveInfo(index *PackageIndex) func(http.ResponseWriter, *http.Request) {
} }
} }
func serveStatus(index *PackageIndex) func(w http.ResponseWriter, r *http.Request) { func serveStatus(index *PackageIndex, cache *pkg.Cache) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
download := path.Dir(r.URL.Path) == "/status"
if index == nil { if index == nil {
http.Error(w, "index is nil", http.StatusInternalServerError) http.Error(w, "index is nil", http.StatusInternalServerError)
return return
} }
base := path.Base(r.URL.Path) name := path.Base(r.URL.Path)
name := strings.TrimSuffix(base, ".log")
p, ok := rosa.ResolveName(name) p, ok := rosa.ResolveName(name)
if !ok { if !ok {
http.NotFound(w, r) http.NotFound(w, r)
@@ -56,7 +56,21 @@ func serveStatus(index *PackageIndex) func(w http.ResponseWriter, r *http.Reques
return return
} }
if len(pk.status) > 0 { if len(pk.status) > 0 {
w.Header().Set("Content-Type", "text/plain; charset=utf-8") if download {
w.Header().Set("Content-Type", "application/octet-stream")
} else {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
}
if download {
var version string
if pk.Version != "\u0000" {
version = pk.Version
} else {
version = "unknown"
}
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s-%s-%s.log\"", pk.Name, version, pkg.Encode(pk.ident.Value())))
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
_, err := io.Copy(w, bytes.NewReader(pk.status)) _, err := io.Copy(w, bytes.NewReader(pk.status))
@@ -111,10 +125,11 @@ func serveGet(index *PackageIndex) func(http.ResponseWriter, *http.Request) {
const ApiVersion = "v1" const ApiVersion = "v1"
func apiRoutes(index *PackageIndex) { func apiRoutes(index *PackageIndex, cache *pkg.Cache) {
http.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), serveInfo(index)) http.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), serveInfo(index))
http.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), serveGet(index)) http.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), serveGet(index))
http.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), serveStatus(index)) http.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), serveStatus(index, cache))
http.HandleFunc("GET /status/", serveStatus(index, cache))
} }
func WritePayload(w http.ResponseWriter, payload any) { func WritePayload(w http.ResponseWriter, payload any) {

View File

@@ -2,8 +2,8 @@ package main
import ( import (
"cmp" "cmp"
"fmt"
"slices" "slices"
"unique"
"hakurei.app/internal/pkg" "hakurei.app/internal/pkg"
"hakurei.app/internal/rosa" "hakurei.app/internal/rosa"
@@ -25,12 +25,13 @@ type PackageIndex struct {
} }
type PackageIndexEntry struct { type PackageIndexEntry struct {
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
Website string `json:"website,omitempty"` Website string `json:"website,omitempty"`
Version string `json:"version"` Version string `json:"version"`
Status string `json:"report,omitempty"` HasReport bool `json:"report,omitempty"`
status []byte `json:"-"` ident unique.Handle[pkg.ID] `json:"-"`
status []byte `json:"-"`
} }
func createPackageIndex(cache *pkg.Cache, report *rosa.Report) (_ *PackageIndex, err error) { func createPackageIndex(cache *pkg.Cache, report *rosa.Report) (_ *PackageIndex, err error) {
@@ -45,20 +46,18 @@ func createPackageIndex(cache *pkg.Cache, report *rosa.Report) (_ *PackageIndex,
id := cache.Ident(a) id := cache.Ident(a)
st, n := report.ArtifactOf(id) st, n := report.ArtifactOf(id)
var status []byte var status []byte
var statusUrl string
if n < 1 { if n < 1 {
status = nil status = nil
statusUrl = ""
} else { } else {
status = st status = st
statusUrl = fmt.Sprintf("/api/%s/status/%s.log", ApiVersion, m.Name)
} }
entry := PackageIndexEntry{ entry := PackageIndexEntry{
Name: m.Name, Name: m.Name,
Description: m.Description, Description: m.Description,
Website: m.Website, Website: m.Website,
Version: v, Version: v,
Status: statusUrl, HasReport: len(status) > 0,
ident: id,
status: status, status: status,
} }
work[p] = entry work[p] = entry

View File

@@ -50,7 +50,7 @@ func main() {
return err return err
} }
uiRoutes() uiRoutes()
apiRoutes(index) apiRoutes(index, cache)
err = http.ListenAndServe(fmt.Sprintf(":%d", flagPort), nil) err = http.ListenAndServe(fmt.Sprintf(":%d", flagPort), nil)
if err != nil { if err != nil {
return err return err

View File

@@ -9,7 +9,7 @@ function toHTML(entry) {
let v = entry.version != null ? `<span>${escapeHtml(entry.version)}</span>` : ""; let v = entry.version != null ? `<span>${escapeHtml(entry.version)}</span>` : "";
let d = entry.description != null ? `<p>${escapeHtml(entry.description)}</p>` : ""; let d = entry.description != null ? `<p>${escapeHtml(entry.description)}</p>` : "";
let w = entry.website != null ? `<a href="${encodeURI(entry.website)}">Website</a>` : ""; let w = entry.website != null ? `<a href="${encodeURI(entry.website)}">Website</a>` : "";
let r = entry.report != null ? `<a href="${encodeURI(entry.report)}">Log</a>` : ""; let r = entry.report ? `Log (<a href=\"${encodeURI('/api/v1/status/' + entry.name)}\">View</a> | <a href=\"${encodeURI('/status/' + entry.name)}\">Download</a>)` : "";
let row = (document.createElement('tr')); let row = (document.createElement('tr'));
row.innerHTML = `<td> row.innerHTML = `<td>
<h2>${escapeHtml(entry.name)} ${v}</h2> <h2>${escapeHtml(entry.name)} ${v}</h2>
@@ -50,7 +50,7 @@ class State {
entriesPerPage = 10; entriesPerPage = 10;
currentPage = 1; currentPage = 1;
entryIndex = 0; entryIndex = 0;
maxEntries = 100; maxEntries = 0;
getEntriesPerPage() { getEntriesPerPage() {
return this.entriesPerPage; return this.entriesPerPage;
} }

View File

@@ -3,13 +3,13 @@ class PackageIndexEntry {
description: string | null description: string | null
website: string | null website: string | null
version: string | null version: string | null
report: string | null report: boolean
} }
function toHTML(entry: PackageIndexEntry): HTMLTableRowElement { function toHTML(entry: PackageIndexEntry): HTMLTableRowElement {
let v = entry.version != null ? `<span>${escapeHtml(entry.version)}</span>` : "" let v = entry.version != null ? `<span>${escapeHtml(entry.version)}</span>` : ""
let d = entry.description != null ? `<p>${escapeHtml(entry.description)}</p>` : "" let d = entry.description != null ? `<p>${escapeHtml(entry.description)}</p>` : ""
let w = entry.website != null ? `<a href="${encodeURI(entry.website)}">Website</a>` : "" let w = entry.website != null ? `<a href="${encodeURI(entry.website)}">Website</a>` : ""
let r = entry.report != null ? `<a href="${encodeURI(entry.report)}">Log</a>` : "" let r = entry.report ? `Log (<a href=\"${encodeURI('/api/v1/status/' + entry.name)}\">View</a> | <a href=\"${encodeURI('/status/' + entry.name)}\">Download</a>)` : ""
let row = <HTMLTableRowElement>(document.createElement('tr')) let row = <HTMLTableRowElement>(document.createElement('tr'))
row.innerHTML = `<td> row.innerHTML = `<td>
<h2>${escapeHtml(entry.name)} ${v}</h2> <h2>${escapeHtml(entry.name)} ${v}</h2>