Skip to content

Commit

Permalink
indexer: QoL changes to headers
Browse files Browse the repository at this point in the history
This commit adds some Link headers to indicate to clients next steps to
take and sets errors in the final JSON encode to be sent as trailers.
  • Loading branch information
hdonnay committed Mar 5, 2020
1 parent 741fc2c commit 8953724
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions indexer/httptransport.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package indexer

import (
"context"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -18,9 +17,10 @@ import (
var _ http.Handler = (*HTTP)(nil)

const (
IndexAPIPath = "/api/v1/index_report"
IndexReportAPIPath = "/api/v1/index_report/"
StateAPIPath = "/api/v1/state"
v1Root = "/api/v1/"
IndexAPIPath = v1Root + "index_report"
IndexReportAPIPath = v1Root + "index_report/"
StateAPIPath = v1Root + "state"
)

type HTTP struct {
Expand Down Expand Up @@ -112,7 +112,13 @@ func (h *HTTP) IndexReportHandler(w http.ResponseWriter, r *http.Request) {
}
}

const (
linkIndex = `<%s>; rel="https://projectquay.io/clair/v1/index_report"`
linkReport = `<%s>; rel="https://projectquay.io/clair/v1/vulnerability_report"`
)

func (h *HTTP) IndexHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
w.Header().Set("content-type", "application/json")
if r.Method != http.MethodPost {
resp := &je.Response{
Expand All @@ -138,7 +144,7 @@ func (h *HTTP) IndexHandler(w http.ResponseWriter, r *http.Request) {

// TODO Do we need some sort of background context embedded in the HTTP
// struct?
report, err := h.serv.Index(context.TODO(), &m)
report, err := h.serv.Index(ctx, &m)
if err != nil {
resp := &je.Response{
Code: "index-error",
Expand All @@ -149,16 +155,12 @@ func (h *HTTP) IndexHandler(w http.ResponseWriter, r *http.Request) {
}

next := path.Join(IndexReportAPIPath, m.Hash.String())
w.Header().Add("link", fmt.Sprintf(linkReport, path.Join(v1Root, "vulnerabilty_report", m.Hash.String())))
w.Header().Add("link", fmt.Sprintf(linkIndex, next))
w.Header().Set("location", next)
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(report)
if err != nil {
resp := &je.Response{
Code: "encoding-error",
Message: fmt.Sprintf("failed to encode scan report: %v", err),
}
je.Error(w, resp, http.StatusInternalServerError)
return
if err := json.NewEncoder(w).Encode(report); err != nil {
w.Header().Set("clair-error", err.Error())
}
}

Expand All @@ -176,21 +178,14 @@ func (h *HTTP) StateHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotModified)
return
}
w.Header().Set("content-type", "application/json")

err := json.NewEncoder(w).Encode(struct {
State string `json:"state"`
}{
State: s,
})
err := json.NewEncoder(w).Encode(stateSuccess{s})
if err != nil {
resp := &je.Response{
Code: "encoding-error",
Message: fmt.Sprintf("failed to encode scan report: %v", err),
}
je.Error(w, resp, http.StatusInternalServerError)
w.Header().Set("clair-error", err.Error())
}
return
}

type stateSuccess struct {
State string `json:"state"`
}

// Register will register the api on a given mux.
Expand Down

0 comments on commit 8953724

Please sign in to comment.