Refactor setting writer Location header

Signed-off-by: Jared Scheib <jared.scheib@gmail.com>
pull/2099/head
Michael de Sa 2017-10-11 10:14:57 -07:00 committed by Jared Scheib
parent 40fac47b28
commit 04a9af37f3
7 changed files with 16 additions and 13 deletions

View File

@ -104,7 +104,7 @@ func (s *Service) NewDashboard(w http.ResponseWriter, r *http.Request) {
}
res := newDashboardResponse(dashboard)
w.Header().Add("Location", res.Links.Self)
location(w, res.Links.Self)
encodeJSON(w, http.StatusCreated, res, s.Logger)
}

7
server/helpers.go Normal file
View File

@ -0,0 +1,7 @@
package server
import "net/http"
func location(w http.ResponseWriter, self string) {
w.Header().Add("Location", self)
}

View File

@ -94,7 +94,7 @@ func (s *Service) NewKapacitor(w http.ResponseWriter, r *http.Request) {
}
res := newKapacitor(srv)
w.Header().Add("Location", res.Links.Self)
location(w, res.Links.Self)
encodeJSON(w, http.StatusCreated, res, s.Logger)
}
@ -324,7 +324,7 @@ func (s *Service) KapacitorRulesPost(w http.ResponseWriter, r *http.Request) {
return
}
res := newAlertResponse(task, srv.SrcID, srv.ID)
w.Header().Add("Location", res.Links.Self)
location(w, res.Links.Self)
encodeJSON(w, http.StatusCreated, res, s.Logger)
}

View File

@ -70,7 +70,7 @@ func (s *Service) NewLayout(w http.ResponseWriter, r *http.Request) {
}
res := newLayoutResponse(layout)
w.Header().Add("Location", res.Link.Href)
location(w, res.Link.Href)
encodeJSON(w, http.StatusCreated, res, s.Logger)
}

View File

@ -46,7 +46,7 @@ func (s *Service) NewRole(w http.ResponseWriter, r *http.Request) {
}
rr := newRoleResponse(srcID, res)
w.Header().Add("Location", rr.Links.Self)
location(w, rr.Links.Self)
encodeJSON(w, http.StatusCreated, rr, s.Logger)
}
@ -88,7 +88,7 @@ func (s *Service) UpdateRole(w http.ResponseWriter, r *http.Request) {
return
}
rr := newRoleResponse(srcID, role)
w.Header().Add("Location", rr.Links.Self)
location(w, rr.Links.Self)
encodeJSON(w, http.StatusOK, rr, s.Logger)
}

View File

@ -93,7 +93,7 @@ func (s *Service) NewSource(w http.ResponseWriter, r *http.Request) {
}
res := newSourceResponse(src)
w.Header().Add("Location", res.Links.Self)
location(w, res.Links.Self)
encodeJSON(w, http.StatusCreated, res, s.Logger)
}
@ -405,7 +405,7 @@ func (s *Service) NewSourceUser(w http.ResponseWriter, r *http.Request) {
if _, hasRoles := s.hasRoles(ctx, ts); hasRoles {
su.WithRoles(srcID, res.Roles)
}
w.Header().Add("Location", su.Links.Self)
location(w, su.Links.Self)
encodeJSON(w, http.StatusCreated, su, s.Logger)
}
@ -524,7 +524,7 @@ func (s *Service) UpdateSourceUser(w http.ResponseWriter, r *http.Request) {
if _, hasRoles := s.hasRoles(ctx, ts); hasRoles {
res.WithRoles(srcID, u.Roles)
}
w.Header().Add("Location", res.Links.Self)
location(w, res.Links.Self)
encodeJSON(w, http.StatusOK, res, s.Logger)
}

View File

@ -197,7 +197,3 @@ func (s *Service) Users(w http.ResponseWriter, r *http.Request) {
res := newUsersResponse(users)
encodeJSON(w, http.StatusOK, res, s.Logger)
}
func location(w http.ResponseWriter, self string) {
w.Header().Add("Location", self)
}