Skip to content

Practice0326 #8

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 6 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
35 changes: 33 additions & 2 deletions http_server/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

var (
counter = 0
counter = 0
sleeping = false
)

func main() {
Expand All @@ -25,23 +26,38 @@ func main() {
// POST Bodyの読み込み
e.POST("/incr", incrementHandler)

e.POST("/goodbye", goodbyeHandler)

// 8080ポートで起動
e.Logger.Fatal(e.Start(":8080"))
}

// レスポンスに`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")
}

// Headerから数字を取得して、その二乗を返すハンドラー
func squareHandler(c echo.Context) error {
if sleeping {
return echo.NewHTTPError(http.StatusFailedDependency)
}

// Headerの読み込み
numStr := c.Request().Header.Get("num")
// String -> Intの変換
Expand All @@ -50,23 +66,38 @@ 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))
}

// 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 {
// jsonタグをつける事でjsonのunmarshalが出来る
// jsonパッケージに渡すので、Publicである必要がある
Num int `json:"num"`
}

type incrResponse struct {
Counter int `json:"counter"`
}
42 changes: 41 additions & 1 deletion http_server/net_http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

var (
counter = 0
counter = 0
sleeping = false
)

func main() {
Expand All @@ -24,16 +25,34 @@ func main() {
// POST Bodyの読み込み
http.HandleFunc("/incr", incrementHandler)

http.HandleFunc("/goodbye", goodbyeHandler)

// 8080ポートで起動
http.ListenAndServe(":8080", nil)
}

// レスポンスに`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)
Expand All @@ -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の変換
Expand All @@ -52,13 +76,29 @@ 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))
}

// 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()
Expand Down