Skip to content

Commit

Permalink
restify cache server's API
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin committed Jun 23, 2024
1 parent 2643e0d commit 911852a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cache_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewCacheClient(config CacheClientConfig) (*CacheClient, error) {

// Get retrieves an objects if exists in the cache or an error otherwise
func (c *CacheClient) Get(_ context.Context, id string) (Object, error) {
url := fmt.Sprintf("%s/get?id=%s", c.server, id)
url := fmt.Sprintf("%s/%s", c.server, id)

// TODO: use http.Request
resp, err := http.Get(url) //nolint:gosec,noctx
Expand Down Expand Up @@ -65,7 +65,7 @@ func (c *CacheClient) Get(_ context.Context, id string) (Object, error) {

// Store stores the object and returns the metadata
func (c *CacheClient) Store(_ context.Context, id string, content io.Reader) (Object, error) {
url := fmt.Sprintf("%s/store?id=%s", c.server, id)
url := fmt.Sprintf("%s/%s", c.server, id)
resp, err := http.Post( //nolint:gosec,noctx
url,
"application/octet-stream",
Expand Down
36 changes: 17 additions & 19 deletions cache_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"net/http"
"net/url"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -45,9 +44,10 @@ func NewCacheServer(config CacheServerConfig) http.Handler {
}

handler := http.NewServeMux()
handler.HandleFunc("/store", cacheSrv.Store)
handler.HandleFunc("/get", cacheSrv.Get)
handler.HandleFunc("/download", cacheSrv.Download)
// FIXME: this should be PUT (used POST as http client doesn't have PUT method)
handler.HandleFunc("POST /{id}", cacheSrv.Store)
handler.HandleFunc("GET /{id}", cacheSrv.Get)
handler.HandleFunc("GET /{id}/content", cacheSrv.Download)

return handler
}
Expand All @@ -66,7 +66,7 @@ func (s *CacheServer) Get(w http.ResponseWriter, r *http.Request) {
}
}()

id := r.URL.Query().Get("id")
id := r.PathValue("id")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
resp.Error = ErrInvalidRequest.Error()
Expand All @@ -86,17 +86,16 @@ func (s *CacheServer) Get(w http.ResponseWriter, r *http.Request) {
}

// overwrite URL with own
downloadURL, err := url.JoinPath(s.baseURL, "download")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
resp.Error = err.Error()
return
baseURL := s.baseURL
if baseURL == "" {
baseURL = fmt.Sprintf("http://%s%s", r.Host, r.RequestURI)
}
downloadURL := fmt.Sprintf("%s/%s/download", baseURL, id)

resp.Object = Object{
ID: id,
Checksum: object.Checksum,
URL: fmt.Sprintf("%s?id=%s", downloadURL, object.ID),
URL: downloadURL,
}

w.WriteHeader(http.StatusOK)
Expand All @@ -117,7 +116,7 @@ func (s *CacheServer) Store(w http.ResponseWriter, r *http.Request) {
}
}()

id := r.URL.Query().Get("id")
id := r.PathValue("id")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
resp.Error = ErrInvalidRequest.Error()
Expand All @@ -132,17 +131,16 @@ func (s *CacheServer) Store(w http.ResponseWriter, r *http.Request) {
}

// overwrite URL with own
downloadURL, err := url.JoinPath(s.baseURL, "download")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
resp.Error = err.Error()
return
baseURL := s.baseURL
if baseURL == "" {
baseURL = fmt.Sprintf("http://%s%s", r.Host, r.RequestURI)
}
downloadURL := fmt.Sprintf("%s/%s/download", baseURL, id)

resp.Object = Object{
ID: id,
Checksum: object.Checksum,
URL: fmt.Sprintf("%s?id=%s", downloadURL, object.ID),
URL: downloadURL,
}

w.WriteHeader(http.StatusOK)
Expand All @@ -151,7 +149,7 @@ func (s *CacheServer) Store(w http.ResponseWriter, r *http.Request) {

// Download returns an object's content given its id
func (s *CacheServer) Download(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
id := r.PathValue("id")
if id == "" {
w.WriteHeader(http.StatusBadRequest)
return
Expand Down
6 changes: 3 additions & 3 deletions cache_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestCacheServerGet(t *testing.T) {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()

url := fmt.Sprintf("%s/get?id=%s", srv.URL, tc.id)
url := fmt.Sprintf("%s/%s", srv.URL, tc.id)
resp, err := http.Get(url)
if err != nil {
t.Fatalf("accessing server %v", err)
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestCacheServerStore(t *testing.T) {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()

url := fmt.Sprintf("%s/store?id=%s", srv.URL, tc.id)
url := fmt.Sprintf("%s/%s", srv.URL, tc.id)
resp, err := http.Post(
url,
"application/octet-stream",
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestCacheServerDownload(t *testing.T) {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()

url := fmt.Sprintf("%s/download?id=%s", srv.URL, tc.id)
url := fmt.Sprintf("%s/%s/content", srv.URL, tc.id)
resp, err := http.Get(url)
if err != nil {
t.Fatalf("accessing server %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewClient() *cobra.Command {
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("downloading artifact %w", err)
return fmt.Errorf("request failed with status %s", resp.Status)
}

outFile, err := os.OpenFile(output, os.O_WRONLY|os.O_CREATE, 0o755) //nolint:gosec
Expand Down

0 comments on commit 911852a

Please sign in to comment.