Skip to content

Daijin no kadai api #9

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 8 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
42 changes: 41 additions & 1 deletion http_server/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func main() {
e.GET("/square", squareHandler)
// POST Bodyの読み込み
e.POST("/incr", incrementHandler)
e.POST("/fizzbuzz", fizzbuzzHandler)

// 8080ポートで起動
e.Logger.Fatal(e.Start(":8080"))
Expand All @@ -50,6 +51,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 over 100")
}
// fmt.Sprintfでフォーマットに沿った文字列を生成できる。
return c.String(http.StatusOK, fmt.Sprintf("Square of %d is equal to %d", num, num*num))
}
Expand All @@ -62,7 +66,43 @@ 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))

jsonMap := map[string]int{
"counter": counter,
}

return c.JSON(http.StatusOK, jsonMap)
}

func fizzbuzzHandler(c echo.Context) error {
fizzbuzzRequest := fizzbuzzRequest{}
if err := c.Bind(&fizzbuzzRequest); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
}

var fizzbuzz string
num := fizzbuzzRequest.Num

switch {
case num%15 == 0:
fizzbuzz = "FIZZ BUZZ!"
case num%3 == 0:
fizzbuzz = "FIZZ!"
case num%5 == 0:
fizzbuzz = "BUZZ!"
default:
fizzbuzz = strconv.Itoa(num)
}

jsonMap := map[string]string{
"fizzbuzz": fizzbuzz,
}

return c.JSON(http.StatusOK, jsonMap)
}

type fizzbuzzRequest struct {
Num int `json:"num"`
}

type incrRequest struct {
Expand Down
51 changes: 50 additions & 1 deletion http_server/net_http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func main() {
// POST Bodyの読み込み
http.HandleFunc("/incr", incrementHandler)

http.HandleFunc("/fizzbuzz", fizzbuzzHandler)

// 8080ポートで起動
http.ListenAndServe(":8080", nil)
}
Expand All @@ -48,17 +50,28 @@ func squareHandler(w http.ResponseWriter, req *http.Request) {
num, err := strconv.Atoi(numStr)
if err != nil {
// 他のエラーの可能性もあるがサンプルとして纏める
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprint(w, "num is not integer")
return
}

if num >= 100 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "too many numbers lol")
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 != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprint(w, fmt.Sprintf("Only POST method is permitted\n"))
return
Comment on lines +72 to +73
Copy link
Owner

Choose a reason for hiding this comment

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

これだと200ステータスで帰ってしまうので、squareHandler同様にステータス書き込みが必要です。

Copy link
Author

Choose a reason for hiding this comment

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

あぁ、確かにそうですね
追記しました!

}
body := req.Body
// bodyの読み込みに開いたio Readerを最後にCloseする
defer body.Close()
Expand All @@ -74,6 +87,42 @@ func incrementHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, fmt.Sprintf("Value of Counter is %d \n", counter))
}

func fizzbuzzHandler(w http.ResponseWriter, req *http.Request) {
Copy link
Owner

Choose a reason for hiding this comment

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

fizzBuzzでcamelCaseにするか、固有名詞としてfizzbuzzにするか、どちらが良いんだろうかと思いました。
camelCaseを意識できているか気になってのコメントです。
把握はできていれば、後は好みなのでどちらでも良いと思います。

Copy link
Author

Choose a reason for hiding this comment

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

fizzbuzzで固有名詞のつもりでした 👀
javaとかswiftとか書いてたのでcamelCaseは割となじんでます(たまに忘れる・・・

if req.Method != "POST" {
fmt.Fprint(w, fmt.Sprintf("Only POST method is permitted\n"))
return
}

body := req.Body
defer body.Close()

buf := new(bytes.Buffer)
io.Copy(buf, body)

var fizzbuzzRequest fizzbuzzRequest
json.Unmarshal(buf.Bytes(), &fizzbuzzRequest)

var fizzbuzz string
num := fizzbuzzRequest.Num

switch {
case num%15 == 0:
fizzbuzz = "FIZZ BUZZ!"
case num%3 == 0:
fizzbuzz = "FIZZ!"
case num%5 == 0:
fizzbuzz = "BUZZ!"
default:
fizzbuzz = strconv.Itoa(num)
}

fmt.Fprint(w, fmt.Sprintf("%s\n", fizzbuzz))
}

type fizzbuzzRequest struct {
Num int `json:"num"`
}

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