Skip to content

第3回課題 2020/03/26 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion http_server/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func main() {
e.GET("/square", squareHandler)
// POST Bodyの読み込み
e.POST("/incr", incrementHandler)
// GET クエリパラメータの読み込み
e.GET("/addition", additionHandler)

// 8080ポートで起動
e.Logger.Fatal(e.Start(":8080"))
Expand All @@ -50,6 +52,9 @@ func squareHandler(c echo.Context) error {
// 他のエラーの可能性もあるがサンプルとして纏める
return echo.NewHTTPError(http.StatusBadRequest, "num is not integer")
}
if num >= 100 {
return echo.NewHTTPError(http.StatusBadRequest, "num is 100 or more")
}
// fmt.Sprintfでフォーマットに沿った文字列を生成できる。
return c.String(http.StatusOK, fmt.Sprintf("Square of %d is equal to %d", num, num*num))
}
Expand All @@ -62,11 +67,29 @@ func incrementHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
}
counter += incrRequest.Num
return c.String(http.StatusOK, fmt.Sprintf("Value of Counter is %d \n", counter))

res := &incrResponse{
Counter: counter,
}
return c.JSON(http.StatusOK, res)
}

// クエリパラメータから2つの値を取得して、加算したものを返すハンドラー
func additionHandler(c echo.Context) error {
p1, p1Err := strconv.Atoi(c.QueryParam("p1"))
p2, p2Err := strconv.Atoi(c.QueryParam("p2"))
if p1Err != nil || p2Err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "p1 and/or p2 is not integer")
}
return c.String(http.StatusOK, fmt.Sprintf("Addition result is %d \n", p1+p2))
}

type incrRequest struct {
// jsonタグをつける事でjsonのunmarshalが出来る
// jsonパッケージに渡すので、Publicである必要がある
Num int `json:"num"`
}

type incrResponse struct {
Counter int `json:"counter"`
}
30 changes: 30 additions & 0 deletions http_server/net_http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func main() {
http.HandleFunc("/square", squareHandler)
// POST Bodyの読み込み
http.HandleFunc("/incr", incrementHandler)
// GET クエリパラメータの読み込み
http.HandleFunc("/addition", additionHandler)

// 8080ポートで起動
http.ListenAndServe(":8080", nil)
Expand Down Expand Up @@ -52,13 +54,23 @@ func squareHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, "num is not integer")
return
}
if num >= 100 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "num is 100 or more")
return
}
// fmt.Sprintfでフォーマットに沿った文字列を生成できる。
fmt.Fprint(w, fmt.Sprintf("Square of %d is equal to %d", num, num*num))
}

// Bodyから数字を取得してその数字だけCounterをIncrementするハンドラー
// DBがまだないので簡易的なもの
func incrementHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprint(w, "Method is not POST")
return
}
body := req.Body
// bodyの読み込みに開いたio Readerを最後にCloseする
defer body.Close()
Expand All @@ -74,6 +86,24 @@ func incrementHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, fmt.Sprintf("Value of Counter is %d \n", counter))
}

// クエリパラメータから2つの値を取得して、加算したものを返すハンドラー
func additionHandler(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprint(w, "Method is not GET")
return
}
q := req.URL.Query()
p1, p1Err := strconv.Atoi(q.Get("p1"))
p2, p2Err := strconv.Atoi(q.Get("p2"))
if p1Err != nil || p2Err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "p1 and/or p2 is not integer")
return
}
fmt.Fprint(w, fmt.Sprintf("Addition result is %d \n", p1+p2))
}

type incrRequest struct {
// jsonタグをつける事でjsonのunmarshalが出来る
// jsonパッケージに渡すので、Publicである必要がある
Expand Down