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 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 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..176a76d6 --- /dev/null +++ b/kadai4/kimuson13/omikuji/omikuji.go @@ -0,0 +1,63 @@ +package omikuji + +import ( + "bytes" + "encoding/json" + "fmt" + "log" + "math/rand" + "net/http" + "time" +) + +var result = []string{"大凶", "凶", "吉", "中吉", "大吉"} + +type Omikuji struct { + Result string `json:"result"` + Today string `json:"today"` +} + +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") + today := time.Now() + todayStr := today.Format(Layout) + result := PickOmikuji(today) + omikuji := &Omikuji{Result: result, Today: todayStr} + + 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() + _, err := fmt.Fprintln(w, str) + if err != nil { + log.Fatal(err) + } +} + +func Run() { + rand.Seed(time.Now().UnixNano()) + 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) + } + }) + } +}