From dcf55c913ba451afc9664b571f6dc6b8e91e152f Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Sun, 21 Mar 2021 21:05:34 +0000 Subject: [PATCH] 0 --- controllers/health.go | 4 +++- controllers/user.go | 2 -- main.go | 1 + middlewares/json.go | 10 ++++++++++ 4 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 middlewares/json.go diff --git a/controllers/health.go b/controllers/health.go index eb5c129..3e695ef 100644 --- a/controllers/health.go +++ b/controllers/health.go @@ -10,6 +10,7 @@ import ( type HealthController struct{} func (h *HealthController) Healthy(w http.ResponseWriter, r *http.Request) { + w.Header().Del("Content-Type") w.WriteHeader(http.StatusOK) } @@ -18,8 +19,9 @@ func (h *HealthController) Metrics(w http.ResponseWriter, r *http.Request) { output += fmt.Sprintf("mangadex_item_count{type=\"user\"} %v\n", countUsers()) - w.Write([]byte(output)) + w.Header().Del("Content-Type") w.WriteHeader(http.StatusOK) + w.Write([]byte(output)) } func countUsers() int { diff --git a/controllers/user.go b/controllers/user.go index 4bb3dbd..b49a68b 100644 --- a/controllers/user.go +++ b/controllers/user.go @@ -25,7 +25,6 @@ func (u *UserController) GetAll(w http.ResponseWriter, r *http.Request) { return } - w.Header().Add("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusOK) w.Write(usersJSON) } @@ -50,7 +49,6 @@ func (u *UserController) GetOne(w http.ResponseWriter, r *http.Request) { return } - w.Header().Add("Content-Type", "application/json; charset=utf-8") w.WriteHeader(http.StatusOK) w.Write([]byte(*userJSON)) } diff --git a/main.go b/main.go index 4c12e52..5c30841 100644 --- a/main.go +++ b/main.go @@ -38,6 +38,7 @@ func main() { r.Use(middlewares.LogMiddleware) r.Use(middlewares.SentryMiddleware) + r.Use(middlewares.JSONMiddleware) healthRouter := routers.HealthRouter{} healthRouter.RegisterRoutes(r) diff --git a/middlewares/json.go b/middlewares/json.go new file mode 100644 index 0000000..a746873 --- /dev/null +++ b/middlewares/json.go @@ -0,0 +1,10 @@ +package middlewares + +import "net/http" + +func JSONMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json; charset=utf-8") + next.ServeHTTP(w, r) + }) +}