diff --git a/kadai4/execjosh/internal/jinja/jinja.go b/kadai4/execjosh/internal/jinja/jinja.go new file mode 100644 index 0000000..6267531 --- /dev/null +++ b/kadai4/execjosh/internal/jinja/jinja.go @@ -0,0 +1,52 @@ +package jinja + +import ( + "math/rand" + "sync" + "time" +) + +var blessings = []string{ + "大吉", + "吉", + "中吉", + "小吉", + "半吉", + "末吉", + "末小吉", + "平", + "凶", + "小凶", + "半凶", + "末凶", + "大凶", +} +var blessingCount = len(blessings) + +// A Jinja that provides random omikuji +type Jinja struct { + lk sync.Mutex + rng *rand.Rand +} + +// New creates and initializes a new Jinja +func New(seed int64) *Jinja { + return &Jinja{ + rng: rand.New(rand.NewSource(seed)), + } +} + +// GetBlessing returns an blessing based on the current time. +// For 1/1-1/3, it returns always "大吉". +// For all other dates, it returns a random blessing. +func (m *Jinja) GetBlessing(now time.Time) string { + if now.YearDay() <= 3 { + return "大吉" + } + + m.lk.Lock() + n := m.rng.Intn(blessingCount) + m.lk.Unlock() + + return blessings[n] +} diff --git a/kadai4/execjosh/internal/jinja/jinja_test.go b/kadai4/execjosh/internal/jinja/jinja_test.go new file mode 100644 index 0000000..493a1df --- /dev/null +++ b/kadai4/execjosh/internal/jinja/jinja_test.go @@ -0,0 +1,46 @@ +package jinja_test + +import ( + "fmt" + "testing" + "time" + + "github.com/gopherdojo/dojo1/kadai4/execjosh/internal/jinja" +) + +func TestGetBlessing(t *testing.T) { + expectedValues := []struct { + now time.Time + seed int64 + expected []string + }{ + {time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC), 1, []string{"大吉", "大吉", "大吉"}}, + {time.Date(2018, time.January, 2, 0, 0, 0, 0, time.UTC), 1, []string{"大吉", "大吉", "大吉"}}, + {time.Date(2018, time.January, 3, 0, 0, 0, 0, time.UTC), 1, []string{"大吉", "大吉", "大吉"}}, + + {time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC), 2, []string{"大吉", "大吉", "大吉"}}, + {time.Date(2018, time.January, 2, 0, 0, 0, 0, time.UTC), 2, []string{"大吉", "大吉", "大吉"}}, + {time.Date(2018, time.January, 3, 0, 0, 0, 0, time.UTC), 2, []string{"大吉", "大吉", "大吉"}}, + + {time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC), time.Now().UnixNano(), []string{"大吉", "大吉", "大吉"}}, + {time.Date(2018, time.January, 2, 0, 0, 0, 0, time.UTC), time.Now().UnixNano(), []string{"大吉", "大吉", "大吉"}}, + {time.Date(2018, time.January, 3, 0, 0, 0, 0, time.UTC), time.Now().UnixNano(), []string{"大吉", "大吉", "大吉"}}, + + {time.Date(2018, time.May, 4, 0, 0, 0, 0, time.UTC), 1234567890, []string{"末小吉", "末吉", "末小吉", "末凶"}}, + {time.Date(2018, time.May, 17, 0, 0, 0, 0, time.UTC), 123567890, []string{"平", "平", "半吉", "末吉"}}, + } + + for _, tc := range expectedValues { + tc := tc + j := jinja.New(tc.seed) + for _, expected := range tc.expected { + expected := expected + j := j + actual := j.GetBlessing(tc.now) + if expected != actual { + fmt.Printf("expected '%v' but got '%v'\n", expected, actual) + t.Fail() + } + } + } +} diff --git a/kadai4/execjosh/internal/jinja/omikuji.go b/kadai4/execjosh/internal/jinja/omikuji.go new file mode 100644 index 0000000..6fbd72f --- /dev/null +++ b/kadai4/execjosh/internal/jinja/omikuji.go @@ -0,0 +1,6 @@ +package jinja + +// An Omikuji holds a blessing +type Omikuji struct { + Blessing string `json:"blessing"` +} diff --git a/kadai4/execjosh/main.go b/kadai4/execjosh/main.go new file mode 100644 index 0000000..e4807a6 --- /dev/null +++ b/kadai4/execjosh/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "encoding/json" + "net/http" + "time" + + "github.com/gopherdojo/dojo1/kadai4/execjosh/internal/jinja" +) + +func main() { + j := jinja.New(time.Now().UnixNano()) + handler := func(w http.ResponseWriter, r *http.Request) { + b := j.GetBlessing(time.Now()) + mkj := &jinja.Omikuji{Blessing: b} + json.NewEncoder(w).Encode(mkj) + } + + http.HandleFunc("/omikuji", handler) + http.ListenAndServe(":8080", nil) +}