From 9d7f1795355dc103b820584789f15e72995c09f7 Mon Sep 17 00:00:00 2001 From: kimuson Date: Mon, 3 May 2021 18:10:42 +0900 Subject: [PATCH 1/6] Added omikuji package --- kadai4/kimuson13/go.mod | 3 +++ kadai4/kimuson13/main.go | 9 +++++++++ kadai4/kimuson13/omikuji/omikuji.go | 17 +++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 kadai4/kimuson13/go.mod create mode 100644 kadai4/kimuson13/main.go create mode 100644 kadai4/kimuson13/omikuji/omikuji.go diff --git a/kadai4/kimuson13/go.mod b/kadai4/kimuson13/go.mod new file mode 100644 index 00000000..3941372d --- /dev/null +++ b/kadai4/kimuson13/go.mod @@ -0,0 +1,3 @@ +module github.com/kimuson13/gopherdojo-studyroom/kimuson13 + +go 1.16 diff --git a/kadai4/kimuson13/main.go b/kadai4/kimuson13/main.go new file mode 100644 index 00000000..5c5cd926 --- /dev/null +++ b/kadai4/kimuson13/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "github.com/kimuson13/gopherdojo-studyroom/kimuson13/omikuji" +) + +func main() { + omikuji.Run() +} diff --git a/kadai4/kimuson13/omikuji/omikuji.go b/kadai4/kimuson13/omikuji/omikuji.go new file mode 100644 index 00000000..cdb145d2 --- /dev/null +++ b/kadai4/kimuson13/omikuji/omikuji.go @@ -0,0 +1,17 @@ +package omikuji + +import ( + "fmt" + "math/rand" + "time" +) + +var result = []string{"大凶", "凶", "吉", "中吉", "大吉"} + +func Run() { + fmt.Println("start omikuji") + rand.Seed(time.Now().UnixNano()) + i := rand.Intn(len(result)) + r := result[i] + fmt.Println(r) +} From c3a165a3689d7674c5bd9d68d9b63a5b7b85f855 Mon Sep 17 00:00:00 2001 From: kimuson Date: Mon, 3 May 2021 18:12:42 +0900 Subject: [PATCH 2/6] Added .gitignore --- kadai4/kimuson13/.gitignore | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 kadai4/kimuson13/.gitignore diff --git a/kadai4/kimuson13/.gitignore b/kadai4/kimuson13/.gitignore new file mode 100644 index 00000000..f7b34922 --- /dev/null +++ b/kadai4/kimuson13/.gitignore @@ -0,0 +1,15 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ \ No newline at end of file From becdfdde5419b8f523a183f0d2d62e9875661b48 Mon Sep 17 00:00:00 2001 From: kimuson Date: Mon, 3 May 2021 20:27:00 +0900 Subject: [PATCH 3/6] Added omikuji API handler --- kadai4/kimuson13/omikuji/omikuji.go | 35 +++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/kadai4/kimuson13/omikuji/omikuji.go b/kadai4/kimuson13/omikuji/omikuji.go index cdb145d2..c52f6793 100644 --- a/kadai4/kimuson13/omikuji/omikuji.go +++ b/kadai4/kimuson13/omikuji/omikuji.go @@ -1,17 +1,44 @@ package omikuji import ( + "bytes" + "encoding/json" "fmt" + "log" "math/rand" + "net/http" "time" ) var result = []string{"大凶", "凶", "吉", "中吉", "大吉"} -func Run() { - fmt.Println("start omikuji") - rand.Seed(time.Now().UnixNano()) +type Omikuji struct { + Result string `json:"result"` +} + +func pickOmikuji() string { i := rand.Intn(len(result)) r := result[i] - fmt.Println(r) + return r +} + +func handler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + result := pickOmikuji() + omikuji := &Omikuji{Result: result} + + var buf bytes.Buffer + enc := json.NewEncoder(&buf) + if err := enc.Encode(omikuji); err != nil { + log.Print(err) + http.Error(w, "error happen while processing", http.StatusInternalServerError) + } + str := buf.String() + fmt.Fprintln(w, str) +} + +func Run() { + rand.Seed(time.Now().UnixNano()) + http.HandleFunc("/", handler) + http.ListenAndServe(":8080", nil) } From 948a6fb4e2efabfaf8a18cfa07db07244b42f359 Mon Sep 17 00:00:00 2001 From: kimuson Date: Mon, 3 May 2021 21:18:57 +0900 Subject: [PATCH 4/6] Added new year edition --- kadai4/kimuson13/omikuji/omikuji.go | 31 +++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/kadai4/kimuson13/omikuji/omikuji.go b/kadai4/kimuson13/omikuji/omikuji.go index c52f6793..51d61b94 100644 --- a/kadai4/kimuson13/omikuji/omikuji.go +++ b/kadai4/kimuson13/omikuji/omikuji.go @@ -14,18 +14,31 @@ var result = []string{"大凶", "凶", "吉", "中吉", "大吉"} type Omikuji struct { Result string `json:"result"` + Today string `json:"today"` } -func pickOmikuji() string { - i := rand.Intn(len(result)) +func pickOmikuji(t time.Time) string { + var i int + _, month, date := t.Date() + if month == time.January { + if date == 1 || date == 2 || date == 3 { + i = 4 + } + } else { + i = rand.Intn(len(result)) + } r := result[i] return r } +var layout = "2006-01-02" + func handler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=utf-8") - result := pickOmikuji() - omikuji := &Omikuji{Result: result} + today := time.Now() + todayStr := today.Format(layout) + result := pickOmikuji(today) + omikuji := &Omikuji{Result: result, Today: todayStr} var buf bytes.Buffer enc := json.NewEncoder(&buf) @@ -34,11 +47,17 @@ func handler(w http.ResponseWriter, r *http.Request) { http.Error(w, "error happen while processing", http.StatusInternalServerError) } str := buf.String() - fmt.Fprintln(w, str) + _, err := fmt.Fprintln(w, str) + if err != nil { + log.Fatal(err) + } } func Run() { rand.Seed(time.Now().UnixNano()) http.HandleFunc("/", handler) - http.ListenAndServe(":8080", nil) + err := http.ListenAndServe(":8080", nil) + if err != nil { + log.Fatal(err) + } } From e4b49ec76d0da6c29c426ca42960c092bbbf7788 Mon Sep 17 00:00:00 2001 From: kimuson Date: Tue, 4 May 2021 13:38:19 +0900 Subject: [PATCH 5/6] Added omikuji test and refactoring --- kadai4/kimuson13/omikuji/omikuji.go | 12 ++--- kadai4/kimuson13/omikuji/omikuji_test.go | 62 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 kadai4/kimuson13/omikuji/omikuji_test.go diff --git a/kadai4/kimuson13/omikuji/omikuji.go b/kadai4/kimuson13/omikuji/omikuji.go index 51d61b94..176a76d6 100644 --- a/kadai4/kimuson13/omikuji/omikuji.go +++ b/kadai4/kimuson13/omikuji/omikuji.go @@ -17,7 +17,7 @@ type Omikuji struct { Today string `json:"today"` } -func pickOmikuji(t time.Time) string { +func PickOmikuji(t time.Time) string { var i int _, month, date := t.Date() if month == time.January { @@ -31,13 +31,13 @@ func pickOmikuji(t time.Time) string { return r } -var layout = "2006-01-02" +var Layout = "2006-01-02" -func handler(w http.ResponseWriter, r *http.Request) { +func Handler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json; charset=utf-8") today := time.Now() - todayStr := today.Format(layout) - result := pickOmikuji(today) + todayStr := today.Format(Layout) + result := PickOmikuji(today) omikuji := &Omikuji{Result: result, Today: todayStr} var buf bytes.Buffer @@ -55,7 +55,7 @@ func handler(w http.ResponseWriter, r *http.Request) { func Run() { rand.Seed(time.Now().UnixNano()) - http.HandleFunc("/", handler) + http.HandleFunc("/", Handler) err := http.ListenAndServe(":8080", nil) if err != nil { log.Fatal(err) diff --git a/kadai4/kimuson13/omikuji/omikuji_test.go b/kadai4/kimuson13/omikuji/omikuji_test.go new file mode 100644 index 00000000..1074a1e9 --- /dev/null +++ b/kadai4/kimuson13/omikuji/omikuji_test.go @@ -0,0 +1,62 @@ +package omikuji_test + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/kimuson13/gopherdojo-studyroom/kimuson13/omikuji" +) + +func TestHandler(t *testing.T) { + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + omikuji.Handler(w, r) + rw := w.Result() + defer rw.Body.Close() + + if rw.StatusCode != http.StatusOK { + t.Fatal("unexpected status code") + } +} + +func TestOmikujiInNewYear(t *testing.T) { + cases := map[string]struct { + date string + expected string + }{ + "january1": { + "2021-01-01", + "大吉", + }, + "january2": { + "2021-01-02", + "大吉", + }, + "january3": { + "2021-01-03", + "大吉", + }, + "otherYear": { + "2020-01-01", + "大吉", + }, + } + + for n, c := range cases { + c := c + t.Run(n, func(t *testing.T) { + time, err := time.Parse(omikuji.Layout, c.date) + if err != nil { + t.Fatal("time parse error") + } + + actual := omikuji.PickOmikuji(time) + + if c.expected != actual { + t.Fatalf("expected = %s, but got %s", c.expected, actual) + } + }) + } +} From e2c78c27b7ac675c592491c4e48cec16b2b1aee7 Mon Sep 17 00:00:00 2001 From: kimuson Date: Tue, 4 May 2021 13:52:18 +0900 Subject: [PATCH 6/6] Added README --- kadai4/kimuson13/README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 kadai4/kimuson13/README.md diff --git a/kadai4/kimuson13/README.md b/kadai4/kimuson13/README.md new file mode 100644 index 00000000..35448e03 --- /dev/null +++ b/kadai4/kimuson13/README.md @@ -0,0 +1,6 @@ +# おみくじAPI +## 使用方法 +kimuson13のディレクトリに移動する。 +そして、`go run main.go`を実行する。 +その後、http://localhost:8080にアクセスするとおみくじの結果と今日の日付が返ってくる。 +正月にアクセスすると、必ず「大吉」が返ってくる。 \ No newline at end of file