diff --git a/server/dashboards.go b/server/dashboards.go index 62e6aac1c..fac4258b6 100644 --- a/server/dashboards.go +++ b/server/dashboards.go @@ -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) } diff --git a/server/helpers.go b/server/helpers.go new file mode 100644 index 000000000..895273498 --- /dev/null +++ b/server/helpers.go @@ -0,0 +1,7 @@ +package server + +import "net/http" + +func location(w http.ResponseWriter, self string) { + w.Header().Add("Location", self) +} diff --git a/server/kapacitors.go b/server/kapacitors.go index 2b18653c9..438d3067e 100644 --- a/server/kapacitors.go +++ b/server/kapacitors.go @@ -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) } diff --git a/server/layout.go b/server/layout.go index 8f0a0ae41..e5ef31e43 100644 --- a/server/layout.go +++ b/server/layout.go @@ -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) } diff --git a/server/roles.go b/server/roles.go index 69b6ad418..7a60a054a 100644 --- a/server/roles.go +++ b/server/roles.go @@ -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) } diff --git a/server/sources.go b/server/sources.go index 1502d9df0..24a510b0c 100644 --- a/server/sources.go +++ b/server/sources.go @@ -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) } diff --git a/server/users.go b/server/users.go index 990ecab2a..7b2ba14a7 100644 --- a/server/users.go +++ b/server/users.go @@ -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) -}