-
Notifications
You must be signed in to change notification settings - Fork 25
#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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
// POST Bodyの読み込み | ||
e.POST("/dncr", decrementHandler) | ||
|
||
// 8080ポートで起動 | ||
e.Logger.Fatal(e.Start(":8080")) | ||
|
@@ -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の読み込み | ||
|
@@ -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)) | ||
} | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 多分時間の問題だったと思うんですが、incrRequestを使ってるのが違和感あります There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 急ぎでやったから(という言い訳 |
||
dr := &dncrResponse{ | ||
Counter: counter, | ||
} | ||
return c.JSON(http.StatusOK, dr) | ||
Comment on lines
+91
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. デクリメントがされていない? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
echoがmethodの判別してくれるので不要という課題でした!
echoのGroupは、
/v1/
みたいなprefixを管理する為のものですね。