diff --git a/api/api.go b/api/api.go index b82fddf..11e16b8 100644 --- a/api/api.go +++ b/api/api.go @@ -34,6 +34,7 @@ func (h *Handler) setupRoutes() { h.router.Get("/", h.handleWelcome) h.router.Get("/*", h.handleGetObject) h.router.Put("/*", h.handlePutObject) + h.router.Head("/*", h.handleHeadObject) } func (h *Handler) handleWelcome(w http.ResponseWriter, r *http.Request) { @@ -89,6 +90,23 @@ func (h *Handler) handlePutObject(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } +func (h *Handler) handleHeadObject(w http.ResponseWriter, r *http.Request) { + urlParams := chi.URLParam(r, "*") + bucket := strings.Split(urlParams, "/")[0] + key := strings.Join(strings.Split(urlParams, "/")[1:], "/") + + manifest, err := h.svc.HeadObject(bucket, key) + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + + w.Header().Set("ETag", manifest.ETag) + w.Header().Set("Content-Length", "0") + w.Header().Set("Last-Modified", time.Unix(manifest.CreatedAt, 0).UTC().Format(time.RFC1123)) + w.WriteHeader(http.StatusOK) +} + func (h *Handler) Start(address string) error { fmt.Printf("Starting API server on %s\n", address) h.setupRoutes() diff --git a/service/service.go b/service/service.go index 1f1500d..c96930b 100644 --- a/service/service.go +++ b/service/service.go @@ -64,3 +64,11 @@ func (s *ObjectService) GetObject(bucket, key string) (io.ReadCloser, *models.Ob }() return pr, manifest, nil } + +func (s *ObjectService) HeadObject(bucket, key string) (models.ObjectManifest, error) { + manifest, err := s.metadataHandler.GetManifest(bucket, key) + if err != nil { + return models.ObjectManifest{}, err + } + return *manifest, nil +}