Skip to content

#3 課題 (takashima.nobutaka) #11

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
44 changes: 43 additions & 1 deletion http_server/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func main() {
e.GET("/square", squareHandler)
// POST Bodyの読み込み
e.POST("/incr", incrementHandler)
// FIXME 本来は group とか使うのかな.. 時間なく諦めた
e.GET("/incr", methodNotAllowedHandler)
e.PATCH("/incr", methodNotAllowedHandler)
e.PUT("/incr", methodNotAllowedHandler)
e.DELETE("/incr", methodNotAllowedHandler)

Comment on lines +27 to +32
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

てか課題2 は何も実装しなくてよかったのか!

echoがmethodの判別してくれるので不要という課題でした!
echoのGroupは、/v1/みたいなprefixを管理する為のものですね。

// POST Bodyの読み込み
e.POST("/dncr", decrementHandler)

// 8080ポートで起動
e.Logger.Fatal(e.Start(":8080"))
Expand All @@ -40,6 +48,11 @@ func unAuthorizedHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusUnauthorized, "UnAuthorized")
}

// 405 を返すハンドラー
func methodNotAllowedHandler(c echo.Context) error {
return echo.NewHTTPError(http.StatusMethodNotAllowed, "MethodNotAllowed")
}

// Headerから数字を取得して、その二乗を返すハンドラー
func squareHandler(c echo.Context) error {
// Headerの読み込み
Expand All @@ -50,6 +63,11 @@ func squareHandler(c echo.Context) error {
// 他のエラーの可能性もあるがサンプルとして纏める
return echo.NewHTTPError(http.StatusBadRequest, "num is not integer")
}
// validation
// FIXME これ使いたい https://github.com/go-playground/validator
if num >= 100 {
return echo.NewHTTPError(http.StatusBadRequest, "num must be less than 100.")
}
// fmt.Sprintfでフォーマットに沿った文字列を生成できる。
return c.String(http.StatusOK, fmt.Sprintf("Square of %d is equal to %d", num, num*num))
}
Expand All @@ -62,11 +80,35 @@ 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))
ir := &incrResponse{
Counter: counter,
}
return c.JSON(http.StatusOK, ir)
}

// Bodyから数字を取得してその数字だけCounterをDecrementするハンドラー
// DBがまだないので簡易的なもの
func decrementHandler(c echo.Context) error {
incrRequest := incrRequest{}
if err := c.Bind(&incrRequest); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
}
Comment on lines +92 to +95
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多分時間の問題だったと思うんですが、incrRequestを使ってるのが違和感あります

Copy link
Author

@ntakashima ntakashima Mar 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

急ぎでやったから(という言い訳
dncrResponse 作ってたけど課題4の実装でデグレってしまった

dr := &dncrResponse{
Counter: counter,
}
return c.JSON(http.StatusOK, dr)
Comment on lines +91 to +99
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

デクリメントがされていない?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

駆け込みでやったから、、(という言い訳

}

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

type incrResponse struct {
Counter int `json:"counter"`
}

type dncrResponse struct {
Counter int `json:"counter"`
}