Skip to content

Commit

Permalink
Merge pull request #78 from bmf-san/fix/buf-using-mistake
Browse files Browse the repository at this point in the history
レスポンス周りのbytes.Bufferの使い方を見直した
  • Loading branch information
bmf-san authored Sep 16, 2023
2 parents b6bd7a1 + 21c5085 commit 0f28cf0
Show file tree
Hide file tree
Showing 19 changed files with 394 additions and 304 deletions.
46 changes: 31 additions & 15 deletions app/controller/category_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"bytes"
"encoding/json"
"io"
"net/http"
Expand Down Expand Up @@ -31,71 +32,86 @@ func NewCategoryController(logger *slog.Logger, client *api.Client, presenter *p
// Index displays a listing of the resource.
func (cc *CategoryController) Index() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
code := http.StatusOK
page, _, err := cc.Client.GetPageAndLimit(r)
if err != nil {
cc.Logger.Error(err.Error())
if err := cc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}

resp, err := cc.Client.GetCategories(page, 100)
if err != nil {
cc.Logger.Error(err.Error())
if err := cc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
cc.Logger.Error(err.Error())
if err := cc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}

var categories model.Categories

if err := json.Unmarshal(body, &categories); err != nil {
cc.Logger.Error(err.Error())
if err := cc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}

var pagination model.Pagination
if err := pagination.Convert(resp.Header); err != nil {
cc.Logger.Error(err.Error())
if err := cc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}

if err = cc.Presenter.ExecuteCategoryIndex(w, r, &presenter.CategoryIndex{
buf, err = cc.Presenter.ExecuteCategoryIndex(buf, r, &presenter.CategoryIndex{
Categories: &categories,
Pagination: &presenter.Pagination{
Pager: &pagination,
QueryParams: "",
},
}); err != nil {
})
if err != nil {
cc.Logger.Error(err.Error())
if err := cc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err = cc.Presenter.ExecuteError(buf, code)
if err != nil {
cc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}

bufWriteTo(buf, w, code)
})
}
16 changes: 16 additions & 0 deletions app/controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package controller

import (
"bytes"
"net/http"
)

func bufWriteTo(buf *bytes.Buffer, w http.ResponseWriter, code int) {
w.WriteHeader(code)
if buf == nil {
w.Write([]byte("Response Error"))
}
if _, err := buf.WriteTo(w); err != nil {
w.Write([]byte(err.Error()))
}
}
25 changes: 17 additions & 8 deletions app/controller/feed_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"bytes"
"encoding/json"
"encoding/xml"
"io"
Expand Down Expand Up @@ -33,36 +34,44 @@ func NewFeedController(logger *slog.Logger, client *api.Client, presenter *prese
// Index displays a listing of the resource.
func (fc *FeedController) Index() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
code := http.StatusOK
// NOTE: Since api does not support getting all items, so taking a rough method.
resp, err := fc.Client.GetPosts(1, 99999)
if err != nil {
fc.Logger.Error(err.Error())
if err := fc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := fc.Presenter.ExecuteError(buf, code)
if err != nil {
fc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
fc.Logger.Error(err.Error())
if err := fc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := fc.Presenter.ExecuteError(buf, code)
if err != nil {
fc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}

var posts model.Posts

if err := json.Unmarshal(body, &posts); err != nil {
fc.Logger.Error(err.Error())
if err := fc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := fc.Presenter.ExecuteError(buf, code)
if err != nil {
fc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}

Expand Down Expand Up @@ -103,9 +112,9 @@ func (fc *FeedController) Index() http.Handler {
Entries: entries,
}

buf, _ := xml.MarshalIndent(feed, "", " ")
bytes, _ := xml.MarshalIndent(feed, "", " ")

w.Header().Set("Content-Type", "application/xml")
w.Write([]byte(xml.Header + string(buf)))
w.Write([]byte(xml.Header + string(bytes)))
})
}
40 changes: 27 additions & 13 deletions app/controller/home_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"bytes"
"encoding/json"
"io"
"net/http"
Expand Down Expand Up @@ -31,57 +32,70 @@ func NewHomeController(logger *slog.Logger, client *api.Client, presenter *prese
// Index displays a listing of the resource.
func (hc *HomeController) Index() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
code := http.StatusOK
page, limit, err := hc.Client.GetPageAndLimit(r)
if err != nil {
hc.Logger.Error(err.Error())
if err := hc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := hc.Presenter.ExecuteError(buf, code)
if err != nil {
hc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}

resp, err := hc.Client.GetPosts(page, limit)
if err != nil {
hc.Logger.Error(err.Error())
if err := hc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := hc.Presenter.ExecuteError(buf, code)
if err != nil {
hc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
hc.Logger.Error(err.Error())
if err := hc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := hc.Presenter.ExecuteError(buf, code)
if err != nil {
hc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}

var posts model.Posts

if err := json.Unmarshal(body, &posts); err != nil {
hc.Logger.Error(err.Error())
if err := hc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err := hc.Presenter.ExecuteError(buf, code)
if err != nil {
hc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
bufWriteTo(buf, w, code)
return
}

if err = hc.Presenter.ExecuteHomeIndex(w, r, &presenter.PostIndex{
buf, err = hc.Presenter.ExecuteHomeIndex(buf, r, &presenter.PostIndex{
Posts: &posts,
}); err != nil {
})
if err != nil {
hc.Logger.Error(err.Error())
if err := hc.Presenter.ExecuteError(w, http.StatusInternalServerError); err != nil {
code = http.StatusInternalServerError
buf, err = hc.Presenter.ExecuteError(buf, code)
if err != nil {
hc.Logger.Error(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}

bufWriteTo(buf, w, code)
})
}
Loading

0 comments on commit 0f28cf0

Please sign in to comment.