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 tomoyuki.kobayashi #52

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
8a7dcf1
add kadai1 files :lol:
Nov 8, 2018
ab31386
add todos
Nov 8, 2018
ef36126
add kadai2 files
Nov 17, 2018
c9587ee
Delete .gitignore
bakisunsan Nov 17, 2018
e69b664
Delete Makefile
bakisunsan Nov 17, 2018
9e5a807
Delete README.md
bakisunsan Nov 17, 2018
f9f7c4e
Delete main.go
bakisunsan Nov 17, 2018
4bf0dd9
Delete file_finder.go
bakisunsan Nov 17, 2018
71569f9
Delete formats.go
bakisunsan Nov 17, 2018
4cec624
Delete formats_test.go
bakisunsan Nov 17, 2018
4458643
Delete image_converter.go
bakisunsan Nov 17, 2018
d7df320
Update main.go
bakisunsan Nov 18, 2018
e5de4ee
add load question logics
Nov 21, 2018
06ad2ec
とりあえず一通り動くバージョン
Nov 22, 2018
2abbf61
コメント修正
Nov 22, 2018
3e212f5
Delete Makefile
bakisunsan Nov 22, 2018
8bfcc95
Delete README.md
bakisunsan Nov 22, 2018
1b37ffa
Delete file_finder.go
bakisunsan Nov 22, 2018
788ceb5
Delete file_finder_test.go
bakisunsan Nov 22, 2018
bf5cb5e
Delete formats.go
bakisunsan Nov 22, 2018
26db9b9
Delete formats_test.go
bakisunsan Nov 22, 2018
74dd45a
Delete image_converter.go
bakisunsan Nov 22, 2018
c074309
Delete image_converter_test.go
bakisunsan Nov 22, 2018
d8e8288
Delete main.go
bakisunsan Nov 22, 2018
06c5785
Delete main_test.go
bakisunsan Nov 22, 2018
00001a7
Update Makefile
bakisunsan Nov 22, 2018
4f8b436
refactor game
Nov 22, 2018
89975a8
リファクタリングとテスト追加
Nov 22, 2018
feb6bd9
Merge branch 'kadai3-tomoyuki.kobayashi' of https://github.com/bakisu…
Nov 22, 2018
4b10c87
rename
Nov 22, 2018
3dbe9fb
add test
Nov 22, 2018
5decc01
rangeの動作確認ができてる版
Nov 23, 2018
1a4401c
rangeダウンロードできる版
Nov 24, 2018
d4abbe7
work version
Nov 24, 2018
ce761c1
work version
Nov 24, 2018
7a6cd63
Delete file4.txt
bakisunsan Nov 24, 2018
28b3f22
Delete invalid.jpg
bakisunsan Nov 24, 2018
acefdc7
ファイルクローズが漏れていたので追加
Nov 24, 2018
847d7c4
Merge branch 'kadai3-tomoyuki.kobayashi' of https://github.com/bakisu…
Nov 24, 2018
e06f9a4
add defer
Nov 25, 2018
67258fe
fix for review comments
Nov 27, 2018
5c48aaa
機能が動作するバージョン
Nov 27, 2018
cdce16e
コミットもれ
Nov 27, 2018
220b53d
テスト追加、レビュー対応
Dec 2, 2018
c86c39d
一応エラー処理追加
Dec 2, 2018
b1ec077
ステータスコードを定数見るように
Dec 2, 2018
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
25 changes: 25 additions & 0 deletions kadai4/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.PHONY: deps
deps:
go get github.com/golang/lint/golint

.PHONY: build
build:
go build -o serve tomoyukikobayashi

.PHONY: test
test:
go test -v -cover ./...

.PHONY: cover
cover:
go test -coverprofile=mainprof tomoyukikobayashi
go tool cover -html=mainprof

.PHONY: lint
lint: deps
go vet ./...
golint ./...

.PHONY: fmt
fmt: deps
go fmt *.go
22 changes: 22 additions & 0 deletions kadai4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
OMIKUJI
=====

# Overview

OMIKUJI DAISUKI

# SetUp

下記のようにコマンドを叩くと、実行形式のserveファイルが生成されます
```
make build
```

# Usage
```
./serve
```
でポート8080で立ち上がるため下記などのようにしてリクエストしてください
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

僕のPCは諸般の事情で 8080 ポート埋まっているんですがなんとかなりませんか?
ヒント

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。
ヒントでいただいているように、引数で与えられるようにするか、
":0"で指定して確保できたポートを出力するのが良さそうですね。

```
curl localhost:8080
```
Binary file added kadai4/serve
Binary file not shown.
108 changes: 108 additions & 0 deletions kadai4/src/tomoyukikobayashi/handler/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package handler

import (
"encoding/json"
"io"
"math/rand"
"net/http"
"time"
)

// TOOD ハンドラのテストを書いてみる

func init() {
rand.Seed(time.Now().UnixNano())
}

// Fortune おみくじ機能を提供する
func Fortune(w http.ResponseWriter, r *http.Request) {
f := fourtunes.Omikuji()
// TOOD 1個しかないのでここでやるが、レスポンス毎ではなく、middlewareなどで横断的にやらせたい
w.Header().Set("Content-Type", "application/json; charset=utf-8")
f.WriteJSON(w)
}

// Fourtune おみくじ用データとロジックを格納する
type Fourtune struct {
Luck string `json:"luck"`
Message string `json:"message"`
}

// WriteJSON 与えられたライターに自信をjson化したものを書き込む
func (f *Fourtune) WriteJSON(w io.Writer) error {
enc := json.NewEncoder(w)
if err := enc.Encode(f); err != nil {
return err
}
return nil
}

const (
// DAIKICHI 大吉
DAIKICHI = 0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const (
  DAIKICHI = iota
  CYUKICHI
  ...
)

でも同じ結果になります。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。確かに、iotaは省略可能でしたね。

// CYUKICHI 中吉
CYUKICHI = iota
// KICHI 吉
KICHI = iota
// SYOKICHI 小吉
SYOKICHI = iota
// KYO 凶
KYO = iota
// DAIKYO 大凶
DAIKYO = iota
)

// Fourtunes 全てのおみくじデータを保持して、おみくじを引くロジックを提供
type Fourtunes struct {
data map[int]Fourtune
// 時間をモックできるようにClockを生やしている
Clock
}

// Clock 任意の時間を返却するインターフェイス
type Clock interface {
Now() time.Time
}

// HACK 絶対書き換えられないようにしたいが、閉じ込めて毎回コピーするのも今回はやりすぎなのでしない
var fourtunes = Fourtunes{
data: map[int]Fourtune{
DAIKICHI: Fourtune{Luck: "大吉", Message: "最高やでー"},
CYUKICHI: Fourtune{Luck: "中吉", Message: "ついてんなー"},
KICHI: Fourtune{Luck: "吉", Message: "まずまずやね"},
SYOKICHI: Fourtune{Luck: "小吉", Message: "ぼちぼちやね"},
KYO: Fourtune{Luck: "凶", Message: "気を落とすなよ"},
DAIKYO: Fourtune{Luck: "大凶", Message: "ウケるwwww"},
},
}

// Omikuji ランダムにおみくじ結果を返す
func (fs *Fourtunes) Omikuji() Fourtune {
// 正月期間は必ず大吉
if fs.shoudBeHappy() {
return fourtunes.data[DAIKICHI]
}
rand := rand.Intn(len(fourtunes.data))
// fourtunesの要素のポインタ渡したくないので値で返してる
return fourtunes.data[rand]
}

func (fs *Fourtunes) shoudBeHappy() bool {
now := fs.now()
if now.Month() != time.Month(1) {
return false
}
if now.Day() > 3 {
return false
}
return true
}

func (fs *Fourtunes) now() time.Time {
if fs.Clock == nil {
// TOOD デバッグ用 消す
//return time.Date(2018, time.Month(1), 1, 0, 0, 0, 0, time.UTC)
return time.Now()
}
return fs.Clock.Now()
}
12 changes: 12 additions & 0 deletions kadai4/src/tomoyukikobayashi/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"net/http"

"tomoyukikobayashi/handler"
)

func main() {
http.HandleFunc("/", handler.Fortune)
http.ListenAndServe(":8080", nil)
}