Skip to content
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

Kadai4 kimuson13 #54

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
15 changes: 15 additions & 0 deletions kadai4/kimuson13/.gitignore
Original file line number Diff line number Diff line change
@@ -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/
6 changes: 6 additions & 0 deletions kadai4/kimuson13/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# おみくじAPI
## 使用方法
kimuson13のディレクトリに移動する。
そして、`go run main.go`を実行する。
その後、http://localhost:8080にアクセスするとおみくじの結果と今日の日付が返ってくる。
正月にアクセスすると、必ず「大吉」が返ってくる。
3 changes: 3 additions & 0 deletions kadai4/kimuson13/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/kimuson13/gopherdojo-studyroom/kimuson13

go 1.16
9 changes: 9 additions & 0 deletions kadai4/kimuson13/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/kimuson13/gopherdojo-studyroom/kimuson13/omikuji"
)

func main() {
omikuji.Run()
}
63 changes: 63 additions & 0 deletions kadai4/kimuson13/omikuji/omikuji.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
62 changes: 62 additions & 0 deletions kadai4/kimuson13/omikuji/omikuji_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}