diff --git a/http_server/echo/main.go b/http_server/echo/main.go index f9533fd..0d6c32c 100644 --- a/http_server/echo/main.go +++ b/http_server/echo/main.go @@ -10,7 +10,8 @@ import ( ) var ( - counter = 0 + counter = 0 + sleeping = false ) func main() { @@ -25,6 +26,8 @@ func main() { // POST Bodyの読み込み e.POST("/incr", incrementHandler) + e.POST("/goodbye", goodbyeHandler) + // 8080ポートで起動 e.Logger.Fatal(e.Start(":8080")) } @@ -32,9 +35,18 @@ func main() { // レスポンスに`Hello World`を書き込むハンドラー // 引数をこの形にするのはechoの仕様から決まっている func helloHandler(c echo.Context) error { + if sleeping { + return echo.NewHTTPError(http.StatusFailedDependency) + } + return c.String(http.StatusOK, "Hello World from Go.") } +func goodbyeHandler(c echo.Context) error { + sleeping = true + return c.String(http.StatusOK, "Goodbye! See you next time!") +} + // 200以外のHTTP Statusを返すハンドラー func unAuthorizedHandler(c echo.Context) error { return echo.NewHTTPError(http.StatusUnauthorized, "UnAuthorized") @@ -42,6 +54,10 @@ func unAuthorizedHandler(c echo.Context) error { // Headerから数字を取得して、その二乗を返すハンドラー func squareHandler(c echo.Context) error { + if sleeping { + return echo.NewHTTPError(http.StatusFailedDependency) + } + // Headerの読み込み numStr := c.Request().Header.Get("num") // String -> Intの変換 @@ -50,6 +66,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 over 100") + } // fmt.Sprintfでフォーマットに沿った文字列を生成できる。 return c.String(http.StatusOK, fmt.Sprintf("Square of %d is equal to %d", num, num*num)) } @@ -57,12 +76,20 @@ func squareHandler(c echo.Context) error { // Bodyから数字を取得してその数字だけCounterをIncrementするハンドラー // DBがまだないので簡易的なもの func incrementHandler(c echo.Context) error { + if sleeping { + return echo.NewHTTPError(http.StatusFailedDependency) + } + incrRequest := incrRequest{} if err := c.Bind(&incrRequest); err != nil { 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) } type incrRequest struct { @@ -70,3 +97,7 @@ type incrRequest struct { // jsonパッケージに渡すので、Publicである必要がある Num int `json:"num"` } + +type incrResponse struct { + Counter int `json:"counter"` +} diff --git a/http_server/net_http/main.go b/http_server/net_http/main.go index 15c9f10..418c1c1 100644 --- a/http_server/net_http/main.go +++ b/http_server/net_http/main.go @@ -11,7 +11,8 @@ import ( ) var ( - counter = 0 + counter = 0 + sleeping = false ) func main() { @@ -24,6 +25,8 @@ func main() { // POST Bodyの読み込み http.HandleFunc("/incr", incrementHandler) + http.HandleFunc("/goodbye", goodbyeHandler) + // 8080ポートで起動 http.ListenAndServe(":8080", nil) } @@ -31,9 +34,25 @@ func main() { // レスポンスに`Hello World`を書き込むハンドラー // 引数をこの形にするのはnet/httpの仕様から決まっている func helloHandler(w http.ResponseWriter, req *http.Request) { + if sleeping { + w.WriteHeader(http.StatusFailedDependency) + return + } + fmt.Fprint(w, "Hello World from Go.") } +func goodbyeHandler(w http.ResponseWriter, req *http.Request) { + if req.Method != "POST" { + w.WriteHeader(http.StatusMethodNotAllowed) + fmt.Fprint(w, "invalid http method") + return + } + + fmt.Fprint(w, "Goodbye! See you next time!") + sleeping = true +} + // 200以外のHTTP Statusを返すハンドラー func unAuthorizedHandler(w http.ResponseWriter, req *http.Request) { w.WriteHeader(http.StatusUnauthorized) @@ -42,6 +61,11 @@ func unAuthorizedHandler(w http.ResponseWriter, req *http.Request) { // Headerから数字を取得して、その二乗を返すハンドラー func squareHandler(w http.ResponseWriter, req *http.Request) { + if sleeping { + w.WriteHeader(http.StatusFailedDependency) + return + } + // Headerの読み込み numStr := req.Header.Get("num") // String -> Intの変換 @@ -52,6 +76,11 @@ 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 over 100") + return + } // fmt.Sprintfでフォーマットに沿った文字列を生成できる。 fmt.Fprint(w, fmt.Sprintf("Square of %d is equal to %d", num, num*num)) } @@ -59,6 +88,17 @@ func squareHandler(w http.ResponseWriter, req *http.Request) { // Bodyから数字を取得してその数字だけCounterをIncrementするハンドラー // DBがまだないので簡易的なもの func incrementHandler(w http.ResponseWriter, req *http.Request) { + if sleeping { + w.WriteHeader(http.StatusFailedDependency) + return + } + + if req.Method != "POST" { + w.WriteHeader(http.StatusMethodNotAllowed) + fmt.Fprint(w, "invalid http method") + return + } + body := req.Body // bodyの読み込みに開いたio Readerを最後にCloseする defer body.Close()