8 Commits

Author SHA1 Message Date
mae
e24832b8c0 cmd/pkgserver: add get endpoint 2026-03-09 18:18:51 -05:00
mae
db6a23fa5f cmd/pkgserver: add count endpoint and restructure 2026-03-09 15:41:21 -05:00
mae
0cf14847ce cmd/pkgserver: add status endpoint 2026-03-09 04:09:18 -05:00
mae
10fe8a0a1e cmd/pkgserver: add createPackageIndex 2026-03-09 01:27:46 -05:00
mae
86cd57f5f8 cmd/pkgserver: add command handler 2026-03-08 22:28:08 -05:00
mae
ca114393cc cmd/pkgserver: replace favicon 2026-03-05 01:12:17 -06:00
mae
f864200c9b cmd/pkgserver: pagination 2026-03-05 00:32:25 -06:00
mae
61acc41a18 cmd/pkgserver: basic web ui 2026-03-04 22:50:58 -06:00
2 changed files with 20 additions and 38 deletions

View File

@@ -10,29 +10,15 @@ import (
"strconv"
"strings"
"hakurei.app/internal/info"
"hakurei.app/internal/rosa"
)
type InfoPayload struct {
Count int `json:"count"`
HakureiVersion string `json:"hakurei_version"`
}
func NewInfoPayload(index *PackageIndex) InfoPayload {
count := len(index.sorts[0])
return InfoPayload{
Count: count,
HakureiVersion: info.Version(),
}
}
func serveInfo(index *PackageIndex) func(http.ResponseWriter, *http.Request) {
func serveCount(index *PackageIndex) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
WritePayload(w, NewInfoPayload(index))
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
count := len(index.names)
w.Write([]byte(strconv.Itoa(count)))
}
}
@@ -105,26 +91,22 @@ func serveGet(index *PackageIndex) func(http.ResponseWriter, *http.Request) {
return
}
values := index.sorts[sort][i:min(i+limit, len(index.sorts[sort]))]
WritePayload(w, NewGetPayload(values))
payload := NewGetPayload(values)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.WriteHeader(http.StatusOK)
b, err := json.Marshal(payload)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
_, err = bytes.NewBuffer(b).WriteTo(w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
const ApiVersion = "v1"
func apiRoutes(index *PackageIndex) {
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/status/", ApiVersion), serveStatus(index))
}
func WritePayload(w http.ResponseWriter, payload any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(payload)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
http.HandleFunc("GET /api/count", serveCount(index))
http.HandleFunc("GET /api/get", serveGet(index))
http.HandleFunc("GET /api/status/", serveStatus(index))
}

View File

@@ -51,7 +51,7 @@ func createPackageIndex(cache *pkg.Cache, report *rosa.Report) (_ *PackageIndex,
statusUrl = ""
} else {
status = st
statusUrl = fmt.Sprintf("/api/%s/status/%s.log", ApiVersion, m.Name)
statusUrl = fmt.Sprintf("/api/status/%s.log", m.Name)
}
entry := PackageIndexEntry{
Name: m.Name,