forked from security/hakurei
cmd/pkgserver: api versioning
This commit is contained in:
@@ -10,15 +10,29 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"hakurei.app/internal/info"
|
||||||
"hakurei.app/internal/rosa"
|
"hakurei.app/internal/rosa"
|
||||||
)
|
)
|
||||||
|
|
||||||
func serveCount(index *PackageIndex) func(http.ResponseWriter, *http.Request) {
|
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) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
w.Header().Set("Content-Type", "text/json; charset=utf-8")
|
||||||
count := len(index.names)
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||||
w.Write([]byte(strconv.Itoa(count)))
|
WritePayload(w, NewInfoPayload(index))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,22 +105,26 @@ func serveGet(index *PackageIndex) func(http.ResponseWriter, *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
values := index.sorts[sort][i:min(i+limit, len(index.sorts[sort]))]
|
values := index.sorts[sort][i:min(i+limit, len(index.sorts[sort]))]
|
||||||
payload := NewGetPayload(values)
|
WritePayload(w, 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) {
|
func apiRoutes(index *PackageIndex) {
|
||||||
http.HandleFunc("GET /api/count", serveCount(index))
|
http.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), serveInfo(index))
|
||||||
http.HandleFunc("GET /api/get", serveGet(index))
|
http.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), serveGet(index))
|
||||||
http.HandleFunc("GET /api/status/", serveStatus(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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ func createPackageIndex(cache *pkg.Cache, report *rosa.Report) (_ *PackageIndex,
|
|||||||
statusUrl = ""
|
statusUrl = ""
|
||||||
} else {
|
} else {
|
||||||
status = st
|
status = st
|
||||||
statusUrl = fmt.Sprintf("/api/status/%s.log", m.Name)
|
statusUrl = fmt.Sprintf("/api/%s/status/%s.log", ApiVersion, m.Name)
|
||||||
}
|
}
|
||||||
entry := PackageIndexEntry{
|
entry := PackageIndexEntry{
|
||||||
Name: m.Name,
|
Name: m.Name,
|
||||||
|
|||||||
Reference in New Issue
Block a user