Skip to content

Commit

Permalink
Set appropriate Content-Type - Closes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
dewey committed Aug 19, 2018
1 parent 1ad2c38 commit ed977ee
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import (
"net/http"

"github.com/go-chi/chi"
"github.com/go-chi/render"
)

// NewHandler initializes a new feed router
func NewHandler(s service) *chi.Mux {
r := chi.NewRouter()
r.Use(render.SetContentType(render.ContentTypeJSON))

// Public routes
r.Group(func(r chi.Router) {
Expand All @@ -27,13 +25,12 @@ func NewHandler(s service) *chi.Mux {
func getFeedHandler(s service) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
plugin := chi.URLParam(r, "plugin")
format := chi.URLParam(r, "format")

if plugin == "" {
http.Error(w, errors.New("plugin not allowed to be empty").Error(), http.StatusInternalServerError)
return
}

format := chi.URLParam(r, "format")
if format == "" {
format = "rss"
}
Expand All @@ -42,6 +39,15 @@ func getFeedHandler(s service) http.HandlerFunc {
http.Error(w, errors.New("there was an error serving the feed").Error(), http.StatusInternalServerError)
return
}

switch format {
case "atom":
w.Header().Set("Content-Type", "application/atom+xml")
case "json":
w.Header().Set("Content-Type", "application/json")
default:
w.Header().Set("Content-Type", "application/rss+xml")
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(s))
}
Expand Down

0 comments on commit ed977ee

Please sign in to comment.