forked from rosa/hakurei
Compare commits
8 Commits
93673cb359
...
e24832b8c0
| Author | SHA1 | Date | |
|---|---|---|---|
| e24832b8c0 | |||
| db6a23fa5f | |||
| 0cf14847ce | |||
| 10fe8a0a1e | |||
| 86cd57f5f8 | |||
| ca114393cc | |||
| f864200c9b | |||
| 61acc41a18 |
@@ -10,29 +10,15 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"hakurei.app/internal/info"
|
|
||||||
"hakurei.app/internal/rosa"
|
"hakurei.app/internal/rosa"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InfoPayload struct {
|
func serveCount(index *PackageIndex) func(http.ResponseWriter, *http.Request) {
|
||||||
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/json; charset=utf-8")
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
count := len(index.names)
|
||||||
WritePayload(w, NewInfoPayload(index))
|
w.Write([]byte(strconv.Itoa(count)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,26 +91,22 @@ 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]))]
|
||||||
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) {
|
func apiRoutes(index *PackageIndex) {
|
||||||
http.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), serveInfo(index))
|
http.HandleFunc("GET /api/count", serveCount(index))
|
||||||
http.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), serveGet(index))
|
http.HandleFunc("GET /api/get", serveGet(index))
|
||||||
http.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), serveStatus(index))
|
http.HandleFunc("GET /api/status/", 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/%s/status/%s.log", ApiVersion, m.Name)
|
statusUrl = fmt.Sprintf("/api/status/%s.log", m.Name)
|
||||||
}
|
}
|
||||||
entry := PackageIndexEntry{
|
entry := PackageIndexEntry{
|
||||||
Name: m.Name,
|
Name: m.Name,
|
||||||
|
|||||||
Reference in New Issue
Block a user