-
Notifications
You must be signed in to change notification settings - Fork 25
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
base: master
Are you sure you want to change the base?
Changes from all commits
c516e15
0945566
da926ba
4bcf231
efa27e2
eb7c2eb
ca651ae
de65ae4
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,8 @@ func main() { | |
// POST Bodyの読み込み | ||
http.HandleFunc("/incr", incrementHandler) | ||
|
||
http.HandleFunc("/fizzbuzz", fizzbuzzHandler) | ||
|
||
// 8080ポートで起動 | ||
http.ListenAndServe(":8080", nil) | ||
} | ||
|
@@ -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 | ||
} | ||
body := req.Body | ||
// bodyの読み込みに開いたio Readerを最後にCloseする | ||
defer body.Close() | ||
|
@@ -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) { | ||
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. fizzbuzzで固有名詞のつもりでした 👀 |
||
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である必要がある | ||
|
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.
これだと200ステータスで帰ってしまうので、squareHandler同様にステータス書き込みが必要です。
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.
あぁ、確かにそうですね
追記しました!