-
Notifications
You must be signed in to change notification settings - Fork 17
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
bakisunsan
wants to merge
46
commits into
gopherdojo:master
Choose a base branch
from
bakisunsan:kadai4-tomoyuki.kobayashi
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 43 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
8a7dcf1
add kadai1 files :lol:
ab31386
add todos
ef36126
add kadai2 files
c9587ee
Delete .gitignore
bakisunsan e69b664
Delete Makefile
bakisunsan 9e5a807
Delete README.md
bakisunsan f9f7c4e
Delete main.go
bakisunsan 4bf0dd9
Delete file_finder.go
bakisunsan 71569f9
Delete formats.go
bakisunsan 4cec624
Delete formats_test.go
bakisunsan 4458643
Delete image_converter.go
bakisunsan d7df320
Update main.go
bakisunsan e5de4ee
add load question logics
06ad2ec
とりあえず一通り動くバージョン
2abbf61
コメント修正
3e212f5
Delete Makefile
bakisunsan 8bfcc95
Delete README.md
bakisunsan 1b37ffa
Delete file_finder.go
bakisunsan 788ceb5
Delete file_finder_test.go
bakisunsan bf5cb5e
Delete formats.go
bakisunsan 26db9b9
Delete formats_test.go
bakisunsan 74dd45a
Delete image_converter.go
bakisunsan c074309
Delete image_converter_test.go
bakisunsan d8e8288
Delete main.go
bakisunsan 06c5785
Delete main_test.go
bakisunsan 00001a7
Update Makefile
bakisunsan 4f8b436
refactor game
89975a8
リファクタリングとテスト追加
feb6bd9
Merge branch 'kadai3-tomoyuki.kobayashi' of https://github.com/bakisu…
4b10c87
rename
3dbe9fb
add test
5decc01
rangeの動作確認ができてる版
1a4401c
rangeダウンロードできる版
d4abbe7
work version
ce761c1
work version
7a6cd63
Delete file4.txt
bakisunsan 28b3f22
Delete invalid.jpg
bakisunsan acefdc7
ファイルクローズが漏れていたので追加
847d7c4
Merge branch 'kadai3-tomoyuki.kobayashi' of https://github.com/bakisu…
e06f9a4
add defer
67258fe
fix for review comments
5c48aaa
機能が動作するバージョン
cdce16e
コミットもれ
220b53d
テスト追加、レビュー対応
c86c39d
一応エラー処理追加
b1ec077
ステータスコードを定数見るように
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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で立ち上がるため下記などのようにしてリクエストしてください | ||
``` | ||
curl localhost:8080 | ||
``` |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
でも同じ結果になります。 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
僕のPCは諸般の事情で 8080 ポート埋まっているんですがなんとかなりませんか?
ヒント
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
ヒントでいただいているように、引数で与えられるようにするか、
":0"で指定して確保できたポートを出力するのが良さそうですね。