From 60fbd44b336e2d558d47d843a95d4f04994de6a4 Mon Sep 17 00:00:00 2001 From: ramenjuniti Date: Sat, 29 Jun 2019 04:19:57 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=81=8A=E3=81=BF=E3=81=8F=E3=81=98API?= =?UTF-8?q?=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/ramenjuniti/Makefile | 14 +++++++ kadai4/ramenjuniti/README.md | 23 +++++++++++ kadai4/ramenjuniti/main.go | 23 +++++++++++ kadai4/ramenjuniti/omikuji/omikuji.go | 55 +++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 kadai4/ramenjuniti/Makefile create mode 100644 kadai4/ramenjuniti/README.md create mode 100644 kadai4/ramenjuniti/main.go create mode 100644 kadai4/ramenjuniti/omikuji/omikuji.go diff --git a/kadai4/ramenjuniti/Makefile b/kadai4/ramenjuniti/Makefile new file mode 100644 index 0000000..4f55b43 --- /dev/null +++ b/kadai4/ramenjuniti/Makefile @@ -0,0 +1,14 @@ +BIN := gokuji + +.PHONY: test +test: + go test -cover -v ./... + +.PHONY: build +build: test + go build -o $(BIN) + +.PHONY: clean +clean: + rm $(BIN) + go clean diff --git a/kadai4/ramenjuniti/README.md b/kadai4/ramenjuniti/README.md new file mode 100644 index 0000000..f845d25 --- /dev/null +++ b/kadai4/ramenjuniti/README.md @@ -0,0 +1,23 @@ +# gokuji + +## Description + +おみくじ API です。1/1 - 1/3 は大吉だけが出ます。 + +## Usage + +```bash +./gokuji -p {ポート番号(デフォルトは8080)} +``` + +## Build + +```bash +make build +``` + +## Test + +```bash +make test +``` diff --git a/kadai4/ramenjuniti/main.go b/kadai4/ramenjuniti/main.go new file mode 100644 index 0000000..934e5e2 --- /dev/null +++ b/kadai4/ramenjuniti/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "flag" + "fmt" + "net/http" + "time" + + "github.com/gopherdojo/dojo5/kadai4/ramenjuniti/omikuji" +) + +var port int + +func init() { + flag.IntVar(&port, "p", 8080, "port number") +} + +func main() { + flag.Parse() + o := omikuji.New(time.Now()) + http.HandleFunc("/", o.Handler) + http.ListenAndServe(fmt.Sprintf(":%d", port), nil) +} diff --git a/kadai4/ramenjuniti/omikuji/omikuji.go b/kadai4/ramenjuniti/omikuji/omikuji.go new file mode 100644 index 0000000..7a4438d --- /dev/null +++ b/kadai4/ramenjuniti/omikuji/omikuji.go @@ -0,0 +1,55 @@ +package omikuji + +import ( + "encoding/json" + "log" + "math/rand" + "net/http" + "time" +) + +// Omikuji contains time +type Omikuji struct { + time time.Time +} + +// Result contains type of omikuji result +type Result struct { + Type string `json:"type"` +} + +var types = []string{ + "大吉", + "吉", + "中吉", + "小吉", + "末吉", + "凶", + "大凶", +} + +// New returns Omikuji instance +func New(t time.Time) *Omikuji { + rand.Seed(t.UnixNano()) + return &Omikuji{time: t} +} + +// Handler returns omikuji result +func (o *Omikuji) Handler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + + result := draw(o.time) + if err := json.NewEncoder(w).Encode(result); err != nil { + log.Println("Error:", err) + } +} + +func draw(t time.Time) *Result { + yd := t.YearDay() + + if yd == 1 || yd == 2 || yd == 3 { + return &Result{Type: types[0]} + } + + return &Result{Type: types[rand.Intn(len(types))]} +} From d6f3c011a2cb3803271e2ddee6f5d15abd9df4fb Mon Sep 17 00:00:00 2001 From: ramenjuniti Date: Sat, 29 Jun 2019 20:17:56 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai4/ramenjuniti/omikuji/omikuji_test.go | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 kadai4/ramenjuniti/omikuji/omikuji_test.go diff --git a/kadai4/ramenjuniti/omikuji/omikuji_test.go b/kadai4/ramenjuniti/omikuji/omikuji_test.go new file mode 100644 index 0000000..a4e4223 --- /dev/null +++ b/kadai4/ramenjuniti/omikuji/omikuji_test.go @@ -0,0 +1,77 @@ +package omikuji + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" + "time" +) + +var cases = []struct { + name string + time time.Time +}{ + { + name: "case1", + time: time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC), + }, + { + name: "case2", + time: time.Date(2019, 1, 1, 23, 59, 59, 0, time.UTC), + }, + { + name: "case3", + time: time.Date(2019, 1, 2, 0, 0, 0, 0, time.UTC), + }, + { + name: "case4", + time: time.Date(2019, 1, 2, 23, 59, 59, 0, time.UTC), + }, + { + name: "case5", + time: time.Date(2019, 1, 3, 0, 0, 0, 0, time.UTC), + }, + { + name: "case6", + time: time.Date(2019, 1, 3, 23, 59, 59, 0, time.UTC), + }, +} + +func getResult(time time.Time, t *testing.T) *Result { + t.Helper() + o := New(time) + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + o.Handler(w, r) + rw := w.Result() + defer rw.Body.Close() + + if rw.StatusCode != http.StatusOK { + t.Fatal("unexpected status code") + } + + b, err := ioutil.ReadAll(rw.Body) + if err != nil { + t.Fatal("unexpected error") + } + + re := &Result{} + if err := json.Unmarshal(b, &re); err != nil { + t.Fatal("failed json unmarshal") + } + + return re +} + +func TestOmikuji(t *testing.T) { + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + re := getResult(c.time, t) + if re.Type != "大吉" { + t.Errorf("got %v, want %v", re.Type, "大吉") + } + }) + } +}