Skip to content

Commit

Permalink
iter3 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ex0rcist committed Jul 5, 2024
1 parent 454d326 commit 4cac6ad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ module github.com/ex0rcist/metflix
go 1.22.4

require (
github.com/go-chi/chi v1.5.5
github.com/go-chi/chi/v5 v5.1.0
github.com/rs/zerolog v1.33.0
github.com/satori/go.uuid v1.2.0
github.com/stretchr/testify v1.9.0
)

Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand All @@ -17,8 +15,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
25 changes: 19 additions & 6 deletions internal/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ex0rcist/metflix/internal/metrics"
"github.com/ex0rcist/metflix/internal/storage"
"github.com/ex0rcist/metflix/internal/validators"
"github.com/rs/zerolog/log"
)

type Resource struct {
Expand All @@ -20,6 +21,15 @@ func NewResource(s storage.Storage) Resource {
}
}

func writeErrorResponse(w http.ResponseWriter, code int, err error) {
log.Error().Err(err).Msg("")

w.WriteHeader(code) // only header for now

// resp := fmt.Sprintf("%d %v", code, err)
// http.Error(w, resp, code)
}

func (r Resource) Homepage(res http.ResponseWriter, _ *http.Request) {
body := fmt.Sprintln("mainpage here.")

Expand Down Expand Up @@ -108,19 +118,19 @@ func (r Resource) ShowMetric(res http.ResponseWriter, req *http.Request) {

//При попытке передать запрос без имени метрики возвращать http.StatusNotFound.
if err := validators.EnsureNamePresent(metricName); err != nil {
res.WriteHeader(http.StatusNotFound)
writeErrorResponse(res, http.StatusNotFound, err)
return
}

//При попытке передать запрос с некорректным именем метрики возвращать http.StatusBadRequest.
if err := validators.ValidateName(metricName); err != nil {
res.WriteHeader(http.StatusBadRequest)
writeErrorResponse(res, http.StatusBadRequest, err)
return
}

//При попытке передать запрос с некорректным типом метрики или значением возвращать http.StatusBadRequest.
if err := validators.ValidateKind(metricKind); err != nil {
res.WriteHeader(http.StatusBadRequest)
writeErrorResponse(res, http.StatusBadRequest, err)
return
}

Expand All @@ -129,14 +139,17 @@ func (r Resource) ShowMetric(res http.ResponseWriter, req *http.Request) {
recordID := storage.CalculateRecordID(metricName, metricKind)
record, err := r.storage.Get(recordID)
if err != nil {

writeErrorResponse(res, http.StatusNotFound, err)
return
}

body := record.Value.String()

res.WriteHeader(http.StatusOK)
_, wErr := res.Write([]byte(body))
if wErr != nil {

_, err = res.Write([]byte(body))
if err != nil {
writeErrorResponse(res, http.StatusInternalServerError, err)
return
}
}

0 comments on commit 4cac6ad

Please sign in to comment.